From 46b67f8a6eb86ada4c1d6edefa8383a831b0ba22 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 28 Nov 2022 05:33:52 +0100 Subject: [PATCH] more progress --- internal/handlers/handlehealthcheck.go | 8 ++--- .../middleware.go} | 36 +++++++++++++++---- internal/server/http.go | 2 +- internal/server/routes.go | 18 +++++----- internal/server/server.go | 14 ++++++-- 5 files changed, 55 insertions(+), 23 deletions(-) rename internal/{middlewares/middlewares.go => middleware/middleware.go} (77%) diff --git a/internal/handlers/handlehealthcheck.go b/internal/handlers/handlehealthcheck.go index 479e340..9067d0a 100644 --- a/internal/handlers/handlehealthcheck.go +++ b/internal/handlers/handlehealthcheck.go @@ -20,10 +20,10 @@ func (s *Handlers) handleHealthCheck() http.HandlerFunc { Status: "ok", Now: time.Now().UTC().Format(time.RFC3339Nano), UptimeSeconds: int64(s.uptime().Seconds()), - UptimeHuman: s.uptime().String(), - Maintenance: s.maintenance(), - Appname: s.appname, - Version: s.version, + UptimeHuman: s.params.Server.uptime().String(), + Maintenance: s.params.Server.MaintenanceMode(), + Appname: s.params.Globals.Appname, + Version: s.params.Globals.Version, } s.respondJSON(w, req, resp, 200) } diff --git a/internal/middlewares/middlewares.go b/internal/middleware/middleware.go similarity index 77% rename from internal/middlewares/middlewares.go rename to internal/middleware/middleware.go index 4b35c96..ba189ff 100644 --- a/internal/middlewares/middlewares.go +++ b/internal/middleware/middleware.go @@ -1,19 +1,43 @@ -package server +package middleware import ( "net" "net/http" "time" + "git.eeqj.de/sneak/gohttpserver/internal/config" + "git.eeqj.de/sneak/gohttpserver/internal/globals" + "git.eeqj.de/sneak/gohttpserver/internal/logger" basicauth "github.com/99designs/basicauth-go" "github.com/go-chi/chi/middleware" "github.com/go-chi/cors" + "github.com/rs/zerolog" metrics "github.com/slok/go-http-metrics/metrics/prometheus" ghmm "github.com/slok/go-http-metrics/middleware" "github.com/slok/go-http-metrics/middleware/std" "github.com/spf13/viper" + "go.uber.org/fx" ) +type MiddlewareParams struct { + fx.In + Logger logger.Logger + Globals globals.Globals + Config config.Config +} + +type Middleware struct { + log *zerolog.Logger + params MiddlewareParams +} + +func New(lc fx.Lifecycle, params MiddlewareParams) (*Middleware, error) { + s := new(Middleware) + s.params = params + s.log = params.Logger.Get() + return s, nil +} + // the following is from // https://learning-cloud-native-go.github.io/docs/a6.adding_zerolog_logger/ @@ -45,7 +69,7 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) { // type Middleware func(http.Handler) http.Handler // this returns a Middleware that is designed to do every request through the // mux, note the signature: -func (s *Server) LoggingMiddleware() func(http.Handler) http.Handler { +func (s *Middleware) LoggingMiddleware() func(http.Handler) http.Handler { // FIXME this should use https://github.com/google/go-cloud/blob/master/server/requestlog/requestlog.go return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -73,7 +97,7 @@ func (s *Server) LoggingMiddleware() func(http.Handler) http.Handler { } } -func (s *Server) CORSMiddleware() func(http.Handler) http.Handler { +func (s *Middleware) CORSMiddleware() func(http.Handler) http.Handler { return cors.Handler(cors.Options{ // CHANGEME! these are defaults, change them to suit your needs or // read from environment/viper. @@ -88,7 +112,7 @@ func (s *Server) CORSMiddleware() func(http.Handler) http.Handler { }) } -func (s *Server) AuthMiddleware() func(http.Handler) http.Handler { +func (s *Middleware) AuthMiddleware() func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // CHANGEME you'll want to change this to do stuff. @@ -98,7 +122,7 @@ func (s *Server) AuthMiddleware() func(http.Handler) http.Handler { } } -func (s *Server) MetricsMiddleware() func(http.Handler) http.Handler { +func (s *Middleware) MetricsMiddleware() func(http.Handler) http.Handler { mdlw := ghmm.New(ghmm.Config{ Recorder: metrics.NewRecorder(metrics.Config{}), }) @@ -107,7 +131,7 @@ func (s *Server) MetricsMiddleware() func(http.Handler) http.Handler { } } -func (s *Server) MetricsAuthMiddleware() func(http.Handler) http.Handler { +func (s *Middleware) MetricsAuthMiddleware() func(http.Handler) http.Handler { return basicauth.New( "metrics", map[string][]string{ diff --git a/internal/server/http.go b/internal/server/http.go index 7e814b1..07efa29 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -18,7 +18,7 @@ func (s *Server) serveUntilShutdown() { // add routes // this does any necessary setup in each handler - s.routes() + s.SetupRoutes() s.log.Info().Str("listenaddr", listenAddr).Msg("http begin listen") if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { diff --git a/internal/server/routes.go b/internal/server/routes.go index 27cb874..1fb69d1 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -23,16 +23,16 @@ func (s *Server) SetupRoutes() { s.router.Use(middleware.Recoverer) s.router.Use(middleware.RequestID) - s.router.Use(s.LoggingMiddleware()) + s.router.Use(s.mw.LoggingMiddleware()) // add metrics middleware only if we can serve them behind auth if viper.GetString("METRICS_USERNAME") != "" { - s.router.Use(s.MetricsMiddleware()) + s.router.Use(s.mw.MetricsMiddleware()) } // set up CORS headers. you'll probably want to configure that // in middlewares.go. - s.router.Use(s.CORSMiddleware()) + s.router.Use(s.mw.CORSMiddleware()) // CHANGEME to suit your needs, or pull from config. // timeout for request context; your handlers must finish within @@ -57,12 +57,12 @@ func (s *Server) SetupRoutes() { // complete docs: https://github.com/go-chi/chi //////////////////////////////////////////////////////////////////////// - s.router.Get("/", s.handleIndex()) + s.router.Get("/", s.h.handleIndex()) s.router.Mount("/s", http.StripPrefix("/s", http.FileServer(http.FS(static.Static)))) s.router.Route("/api/v1", func(r chi.Router) { - r.Get("/now", s.handleNow()) + r.Get("/now", s.h.handleNow()) }) // if you want to use a general purpose middleware (http.Handler @@ -71,25 +71,25 @@ func (s *Server) SetupRoutes() { authMiddleware := s.AuthMiddleware() s.router.Get( "/login", - authMiddleware(s.handleLogin()).ServeHTTP, + authMiddleware(s.h.handleLogin()).ServeHTTP, ) // route that panics for testing // CHANGEME remove this s.router.Get( "/panic", - s.handlePanic(), + s.h.handlePanic(), ) s.router.Get( "/.well-known/healthcheck.json", - s.handleHealthCheck(), + s.h.handleHealthCheck(), ) // set up authenticated /metrics route: if viper.GetString("METRICS_USERNAME") != "" { s.router.Group(func(r chi.Router) { - r.Use(s.MetricsAuthMiddleware()) + r.Use(s.mw.MetricsAuthMiddleware()) r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP)) }) } diff --git a/internal/server/server.go b/internal/server/server.go index 72675c3..8bf7350 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -12,7 +12,9 @@ import ( "git.eeqj.de/sneak/gohttpserver/internal/config" "git.eeqj.de/sneak/gohttpserver/internal/globals" + "git.eeqj.de/sneak/gohttpserver/internal/handlers" "git.eeqj.de/sneak/gohttpserver/internal/logger" + "git.eeqj.de/sneak/gohttpserver/internal/middleware" "github.com/rs/zerolog" "github.com/spf13/viper" "go.uber.org/fx" @@ -32,9 +34,11 @@ import ( type ServerParams struct { fx.In - Logger logger.Logger - Globals globals.Globals - Config config.Config + Logger logger.Logger + Globals globals.Globals + Config config.Config + Middleware middleware.Middleware + Handlers handlers.Handlers } type Server struct { @@ -51,11 +55,15 @@ type Server struct { httpServer *http.Server router *chi.Mux params ServerParams + mw middleware.Middleware + h handlers.Handlers } func New(lc fx.Lifecycle, params ServerParams) (*Server, error) { s := new(Server) s.params = params + s.mw = params.Middleware + s.h = params.Handlers s.log = params.Logger.Get() lc.Append(fx.Hook{