rano/cmd/server/main.go

48 lines
1.1 KiB
Go
Raw Normal View History

2024-11-01 19:55:30 +05:30
package main
import (
"fmt"
2024-11-01 19:55:30 +05:30
"net/http"
"gitserver.in/patialtech/mux"
"gitserver.in/patialtech/mux/middleware"
2024-11-17 22:28:29 +05:30
"gitserver.in/patialtech/rano/cmd/server/handler"
"gitserver.in/patialtech/rano/config"
2024-11-01 19:55:30 +05:30
"gitserver.in/patialtech/rano/graph"
"gitserver.in/patialtech/rano/util/logger"
2024-11-01 19:55:30 +05:30
)
func main() {
r := mux.NewRouter()
2024-11-17 22:28:29 +05:30
r.Use(handler.Request())
// 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 19:55:30 +05:30
// graphiql
r.GET("/graphiql", graph.GraphiQL("/query"))
2024-11-01 19:55:30 +05:30
// graph query
r.POST("/query", graph.Query)
2024-11-01 19:55:30 +05:30
// catch all
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
2024-11-01 19:55:30 +05:30
w.Write([]byte("hello there"))
})
r.Serve(func(srv *http.Server) error {
srv.Addr = fmt.Sprintf(":%d", config.Read().GraphPort)
2024-11-01 19:55:30 +05:30
logger.Info("graph server listening on %s", srv.Addr)
return srv.ListenAndServe()
})
}