2020-07-10 18:07:41 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2020-07-13 18:49:44 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/config"
|
2020-07-13 09:20:14 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/graph"
|
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/graph/generated"
|
2020-07-13 14:44:05 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/session"
|
2020-07-10 18:07:41 +02:00
|
|
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
2020-07-13 09:20:14 +02:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
2020-07-13 18:49:44 +02:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler/extension"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler/transport"
|
2020-07-13 09:20:14 +02:00
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2020-07-13 18:49:44 +02:00
|
|
|
"github.com/gorilla/websocket"
|
2020-07-10 18:07:41 +02:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2020-07-13 18:49:44 +02:00
|
|
|
"github.com/rs/cors"
|
2020-07-10 18:07:41 +02:00
|
|
|
"gitlab.com/wpetit/goweb/static"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Mount(r *chi.Mux, config *config.Config) error {
|
|
|
|
|
|
|
|
r.With(oidc.HandleCallback).Get("/oauth2/callback", handleLoginCallback)
|
|
|
|
r.Get("/logout", handleLogout)
|
|
|
|
r.Get("/login", handleLogin)
|
|
|
|
r.Get("/logout/redirect", handleLogoutRedirect)
|
|
|
|
|
|
|
|
r.Route("/api", func(r chi.Router) {
|
2020-07-13 18:49:44 +02:00
|
|
|
r.Use(cors.New(cors.Options{
|
|
|
|
AllowedOrigins: config.HTTP.CORS.AllowedOrigins,
|
|
|
|
AllowCredentials: config.HTTP.CORS.AllowCredentials,
|
|
|
|
Debug: config.Debug,
|
|
|
|
}).Handler)
|
2020-07-13 14:44:05 +02:00
|
|
|
r.Use(session.UserEmailMiddleware)
|
2020-07-10 18:07:41 +02:00
|
|
|
|
2020-07-13 18:49:44 +02:00
|
|
|
gql := handler.New(
|
2020-07-13 09:20:14 +02:00
|
|
|
generated.NewExecutableSchema(generated.Config{
|
|
|
|
Resolvers: &graph.Resolver{},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2020-07-13 18:49:44 +02:00
|
|
|
gql.AddTransport(transport.POST{})
|
|
|
|
gql.AddTransport(&transport.Websocket{
|
|
|
|
KeepAlivePingInterval: 10 * time.Second,
|
|
|
|
Upgrader: websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
// TODO Check WS connection origin
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-07-13 09:20:14 +02:00
|
|
|
if config.Debug {
|
2020-07-13 18:49:44 +02:00
|
|
|
gql.Use(extension.Introspection{})
|
|
|
|
r.Get("/v1/playground", playground.Handler("GraphQL playground", "/api/v1/graphql"))
|
2020-07-13 09:20:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 18:49:44 +02:00
|
|
|
r.Handle("/v1/graphql", gql)
|
2020-07-10 18:07:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
notFoundHandler := r.NotFoundHandler()
|
|
|
|
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|