2020-07-10 18:07:41 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2020-07-13 18:49:44 +02:00
|
|
|
"net/http"
|
2020-07-23 08:57:49 +02:00
|
|
|
"path"
|
2020-07-13 18:49:44 +02:00
|
|
|
"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-09-04 10:10:32 +02:00
|
|
|
gqlConfig := generated.Config{
|
|
|
|
Resolvers: &graph.Resolver{},
|
|
|
|
}
|
|
|
|
|
2020-07-13 18:49:44 +02:00
|
|
|
gql := handler.New(
|
2020-09-04 10:10:32 +02:00
|
|
|
generated.NewExecutableSchema(gqlConfig),
|
2020-07-13 09:20:14 +02:00
|
|
|
)
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2020-07-23 08:57:49 +02:00
|
|
|
clientIndex := path.Join(config.HTTP.PublicDir, "index.html")
|
|
|
|
|
|
|
|
serveClientIndex := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, clientIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of paths handled directly by the client
|
|
|
|
r.Get("/workgroups/*", serveClientIndex)
|
|
|
|
r.Get("/profile", serveClientIndex)
|
2020-08-31 16:13:10 +02:00
|
|
|
r.Get("/dashboard", serveClientIndex)
|
|
|
|
r.Get("/decisions/*", serveClientIndex)
|
2020-07-23 08:57:49 +02:00
|
|
|
|
|
|
|
// Serve static files
|
2020-07-10 18:07:41 +02:00
|
|
|
notFoundHandler := r.NotFoundHandler()
|
|
|
|
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|