builds and runs! not sure if it works, needs testing
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2022-11-28 06:00:44 +01:00
parent 46b67f8a6e
commit 3f49d528e7
11 changed files with 123 additions and 68 deletions

View File

@@ -57,33 +57,33 @@ func (s *Server) SetupRoutes() {
// complete docs: https://github.com/go-chi/chi
////////////////////////////////////////////////////////////////////////
s.router.Get("/", s.h.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.h.handleNow())
r.Get("/now", s.h.HandleNow())
})
// if you want to use a general purpose middleware (http.Handler
// wrapper) on a specific HandleFunc route, you need to take the
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
authMiddleware := s.AuthMiddleware()
authMiddleware := s.mw.AuthMiddleware()
s.router.Get(
"/login",
authMiddleware(s.h.handleLogin()).ServeHTTP,
authMiddleware(s.h.HandleLogin()).ServeHTTP,
)
// route that panics for testing
// CHANGEME remove this
s.router.Get(
"/panic",
s.h.handlePanic(),
s.h.HandlePanic(),
)
s.router.Get(
"/.well-known/healthcheck.json",
s.h.handleHealthCheck(),
s.h.HandleHealthCheck(),
)
// set up authenticated /metrics route:

View File

@@ -3,7 +3,6 @@ package server
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
@@ -16,7 +15,6 @@ import (
"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"
"github.com/getsentry/sentry-go"
@@ -34,17 +32,14 @@ import (
type ServerParams struct {
fx.In
Logger logger.Logger
Globals globals.Globals
Config config.Config
Middleware middleware.Middleware
Handlers handlers.Handlers
Logger *logger.Logger
Globals *globals.Globals
Config *config.Config
Middleware *middleware.Middleware
Handlers *handlers.Handlers
}
type Server struct {
appname string
version string
buildarch string
startupTime time.Time
port int
exitCode int
@@ -55,8 +50,8 @@ type Server struct {
httpServer *http.Server
router *chi.Mux
params ServerParams
mw middleware.Middleware
h handlers.Handlers
mw *middleware.Middleware
h *handlers.Handlers
}
func New(lc fx.Lifecycle, params ServerParams) (*Server, error) {
@@ -69,11 +64,12 @@ func New(lc fx.Lifecycle, params ServerParams) (*Server, error) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.startupTime = time.Now()
s.version = params.Globals.Version
s.Run()
s.Run() // background FIXME
return nil
},
OnStop: func(ctx context.Context) error {
// FIXME do server shutdown here
return nil
},
})
return s, nil
@@ -93,22 +89,22 @@ func (s *Server) Run() {
// logging before sentry, because sentry logs
s.enableSentry()
return s.serve()
s.serve() // FIXME deal with return value
}
func (s *Server) enableSentry() {
s.sentryEnabled = false
if s.Config.SentryDSN == "" {
if s.params.Config.SentryDSN == "" {
return
}
err := sentry.Init(sentry.ClientOptions{
Dsn: viper.GetString("SENTRY_DSN"),
Dsn: s.params.Config.SentryDSN,
Release: fmt.Sprintf("%s-%s", s.params.Globals.Appname, s.params.Globals.Version),
})
if err != nil {
log.Fatal().Err(err).Msg("sentry init failure")
s.log.Fatal().Err(err).Msg("sentry init failure")
return
}
s.log.Info().Msg("sentry error reporting activated")
@@ -170,11 +166,7 @@ func (s *Server) cleanShutdown() {
}
}
func (s *Server) uptime() time.Duration {
return time.Since(s.startupTime)
}
func (s *Server) maintenance() bool {
func (s *Server) MaintenanceMode() bool {
return s.params.Config.MaintenanceMode
}