mirror of
https://github.com/Unkn0wnCat/calapi.git
synced 2025-07-23 06:27:11 +02:00
Add authentication and metrics
This commit is contained in:
parent
7667ea7b90
commit
6abea91d7c
16 changed files with 894 additions and 31 deletions
|
@ -1,28 +1,69 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
chiprometheus "github.com/766b/chi-prometheus"
|
||||
"github.com/99designs/gqlgen/graphql/handler"
|
||||
"github.com/99designs/gqlgen/graphql/playground"
|
||||
"github.com/Unkn0wnCat/calapi/graph"
|
||||
"log"
|
||||
"github.com/Unkn0wnCat/calapi/internal/auth"
|
||||
"github.com/Unkn0wnCat/calapi/internal/logger"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultPort = "8080"
|
||||
|
||||
func logMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
wrappedResponse := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
latencyStart := time.Now()
|
||||
|
||||
defer func() {
|
||||
latency := time.Since(latencyStart)
|
||||
|
||||
logger.Logger.Info("HTTP request finished",
|
||||
zap.String("proto", r.Proto),
|
||||
zap.String("uri", r.RequestURI),
|
||||
zap.String("path", r.URL.Path),
|
||||
zap.String("method", r.Method),
|
||||
zap.String("remote", r.RemoteAddr),
|
||||
zap.Int("status", wrappedResponse.Status()),
|
||||
zap.Int("size", wrappedResponse.BytesWritten()),
|
||||
zap.Duration("latency", latency),
|
||||
zap.String("requestId", middleware.GetReqID(r.Context())),
|
||||
)
|
||||
}()
|
||||
|
||||
next.ServeHTTP(wrappedResponse, r)
|
||||
})
|
||||
}
|
||||
|
||||
func Serve() {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = defaultPort
|
||||
}
|
||||
|
||||
router := chi.NewRouter()
|
||||
m := chiprometheus.NewMiddleware("calapi")
|
||||
router.Use(m)
|
||||
router.Use(middleware.RequestID)
|
||||
router.Use(logMiddleware)
|
||||
router.Use(middleware.Recoverer)
|
||||
router.Use(auth.Middleware())
|
||||
|
||||
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
|
||||
|
||||
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
|
||||
http.Handle("/query", srv)
|
||||
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
|
||||
router.Handle("/query", srv)
|
||||
router.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
logger.Sugar.Infof("Now serving at http://localhost:%s/", port)
|
||||
logger.Sugar.Fatal(http.ListenAndServe(":"+port, router))
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue