rano/graph/server/main.go

44 lines
1006 B
Go
Raw Normal View History

2024-11-01 14:25:30 +00:00
package main
import (
"fmt"
2024-11-01 14:25:30 +00:00
"net/http"
"gitserver.in/patialtech/mux"
"gitserver.in/patialtech/mux/middleware"
"gitserver.in/patialtech/rano/config"
2024-11-01 14:25:30 +00:00
"gitserver.in/patialtech/rano/graph"
"gitserver.in/patialtech/rano/pkg/logger"
)
func main() {
r := mux.NewRouter()
// CORS
r.Use(middleware.CORS(middleware.CORSOption{
AllowedHeaders: []string{"Content-Type"},
MaxAge: 60,
}))
// Secure Headers
r.Use(middleware.Helmet(middleware.HelmetOption{
ContentSecurityPolicy: middleware.CSP{
ScriptSrc: []string{"self", "https://cdn.jsdelivr.net", "unsafe-inline"},
},
}))
2024-11-01 14:25:30 +00:00
// graphiql
r.GET("/graphiql", graph.GraphiQL("/query"))
2024-11-01 14:25:30 +00:00
// graph query
r.POST("/query", graph.Query)
2024-11-01 14:25:30 +00:00
// catch all
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
2024-11-01 14:25:30 +00:00
w.Write([]byte("hello there"))
})
r.Serve(func(srv *http.Server) error {
srv.Addr = fmt.Sprintf(":%d", config.Read().GraphPort)
2024-11-01 14:25:30 +00:00
logger.Info("graph server listening on %s", srv.Addr)
return srv.ListenAndServe()
})
}