more progress
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-11-28 05:33:52 +01:00
parent 5fc22c36b0
commit 46b67f8a6e
5 changed files with 55 additions and 23 deletions

View File

@@ -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 {

View File

@@ -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))
})
}

View File

@@ -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{