2024-11-18 15:23:13 +00:00
|
|
|
// Copyright 2024 Patial Tech. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-11-01 14:25:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-04 06:05:43 +00:00
|
|
|
"fmt"
|
2024-11-01 14:25:30 +00:00
|
|
|
"net/http"
|
2024-11-19 11:46:36 +00:00
|
|
|
"strings"
|
2024-11-01 14:25:30 +00:00
|
|
|
|
|
|
|
"gitserver.in/patialtech/mux"
|
2024-11-04 06:05:43 +00:00
|
|
|
"gitserver.in/patialtech/mux/middleware"
|
2024-11-19 11:46:36 +00:00
|
|
|
"gitserver.in/patialtech/rano/bin/server/handler"
|
2024-11-04 06:05:43 +00:00
|
|
|
"gitserver.in/patialtech/rano/config"
|
2024-11-01 14:25:30 +00:00
|
|
|
"gitserver.in/patialtech/rano/graph"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/util/logger"
|
2024-11-19 11:46:36 +00:00
|
|
|
"gitserver.in/patialtech/rano/web"
|
2024-11-01 14:25:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
2024-11-17 16:58:29 +00:00
|
|
|
|
|
|
|
r.Use(handler.Request())
|
|
|
|
|
2024-11-04 06:05:43 +00:00
|
|
|
// CORS
|
|
|
|
r.Use(middleware.CORS(middleware.CORSOption{
|
|
|
|
AllowedHeaders: []string{"Content-Type"},
|
2024-11-19 11:46:36 +00:00
|
|
|
AllowedOrigins: []string{config.Read().WebURL},
|
2024-11-04 06:05:43 +00:00
|
|
|
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
|
2024-11-04 06:05:43 +00:00
|
|
|
r.GET("/graphiql", graph.GraphiQL("/query"))
|
2024-11-01 14:25:30 +00:00
|
|
|
// graph query
|
2024-11-04 06:05:43 +00:00
|
|
|
r.POST("/query", graph.Query)
|
2024-11-01 14:25:30 +00:00
|
|
|
|
|
|
|
// catch all
|
2024-11-04 06:05:43 +00:00
|
|
|
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
2024-11-19 11:46:36 +00:00
|
|
|
var (
|
|
|
|
resourcePath string
|
|
|
|
path = r.URL.Path
|
|
|
|
)
|
|
|
|
|
|
|
|
if strings.HasPrefix(path, "/_app") {
|
|
|
|
resourcePath = "public/build" + path
|
|
|
|
} else if strings.HasSuffix(path, ".png") ||
|
|
|
|
strings.HasSuffix(path, ".ico") ||
|
|
|
|
strings.HasSuffix(path, ".svg") ||
|
|
|
|
strings.HasSuffix(path, "robot.txt") ||
|
|
|
|
strings.HasSuffix(path, "site.webmanifest") {
|
|
|
|
resourcePath = "public" + path
|
|
|
|
} else {
|
|
|
|
resourcePath = "public/build/fallback.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b, err := web.Public.ReadFile(resourcePath); err != nil {
|
|
|
|
_, _ = w.Write([]byte("hello there"))
|
|
|
|
} else {
|
|
|
|
if strings.HasSuffix(path, ".js") {
|
|
|
|
w.Header().Set("Content-Type", "text/javascript")
|
|
|
|
} else if strings.HasSuffix(path, ".css") {
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", http.DetectContentType(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write(b)
|
|
|
|
}
|
2024-11-01 14:25:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
r.Serve(func(srv *http.Server) error {
|
2024-11-04 06:05:43 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|