2021-07-08 12:15:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 18:43:19 +00:00
|
|
|
"context"
|
2021-07-20 22:04:03 +00:00
|
|
|
"log"
|
2021-07-08 12:15:19 +00:00
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-17 16:29:50 +00:00
|
|
|
"github.com/yauthdev/yauth/server/enum"
|
|
|
|
"github.com/yauthdev/yauth/server/handlers"
|
|
|
|
"github.com/yauthdev/yauth/server/oauth"
|
2021-07-08 12:15:19 +00:00
|
|
|
)
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
func GinContextToContextMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
ctx := context.WithValue(c.Request.Context(), "GinContextKey", c)
|
|
|
|
c.Request = c.Request.WithContext(ctx)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2021-07-20 22:04:03 +00:00
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
origin := c.Request.Header.Get("Origin")
|
|
|
|
log.Println("-> origin", origin)
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
|
|
|
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
|
|
c.AbortWithStatus(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
func main() {
|
|
|
|
r := gin.Default()
|
|
|
|
r.Use(GinContextToContextMiddleware())
|
2021-07-20 22:04:03 +00:00
|
|
|
r.Use(CORSMiddleware())
|
2021-07-17 16:29:50 +00:00
|
|
|
r.GET("/", handlers.PlaygroundHandler())
|
|
|
|
r.POST("/graphql", handlers.GraphqlHandler())
|
2021-07-20 22:04:03 +00:00
|
|
|
r.GET("/verify_email", handlers.VerifyEmail())
|
2021-07-17 16:29:50 +00:00
|
|
|
if oauth.OAuthProvider.GoogleConfig != nil {
|
|
|
|
r.GET("/login/google", handlers.HandleOAuthLogin(enum.GoogleProvider))
|
|
|
|
r.GET("/callback/google", handlers.HandleOAuthCallback(enum.GoogleProvider))
|
|
|
|
}
|
2021-07-17 23:18:42 +00:00
|
|
|
if oauth.OAuthProvider.GithubConfig != nil {
|
|
|
|
r.GET("/login/github", handlers.HandleOAuthLogin(enum.GithubProvider))
|
|
|
|
r.GET("/callback/github", handlers.HandleOAuthCallback(enum.GithubProvider))
|
|
|
|
}
|
2021-07-14 18:43:19 +00:00
|
|
|
r.Run()
|
2021-07-08 12:15:19 +00:00
|
|
|
}
|