All checks were successful
check / check (push) Successful in 2m3s
Replace .golangci.yml with the canonical v2-schema config (default: all minus six disabled linters, lll 88, tests included) and bump every golangci-lint pin to v2.12.2: - Dockerfile: golangci/golangci-lint:v2.12.2-alpine (hash-pinned) - script/bootstrap: GOLANGCI_LINT_VERSION 2.12.2 with new linux-amd64/arm64 release-archive sha256 pins Fix all 747 findings the stricter config surfaces, with no behavior changes: t.Parallel() throughout the test suite, static sentinel errors and errors.Is comparisons, checked error returns, context propagation (contextcheck/noctx), 88-column wrapping, extracted constants and helpers for goconst/dupl/funlen/cyclop, exhaustive switch cases replicating existing defaults, and white-box test files renamed to *_internal_test.go for testpackage. Three nolint:tagliatelle directives preserve the existing snake_case JSON wire and on-disk metadata formats.
160 lines
3.2 KiB
Go
160 lines
3.2 KiB
Go
// Package server provides the HTTP server and lifecycle management.
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/fx"
|
|
"sneak.berlin/go/pixa/internal/config"
|
|
"sneak.berlin/go/pixa/internal/globals"
|
|
"sneak.berlin/go/pixa/internal/handlers"
|
|
"sneak.berlin/go/pixa/internal/logger"
|
|
"sneak.berlin/go/pixa/internal/middleware"
|
|
)
|
|
|
|
// Shutdown configuration constants.
|
|
const (
|
|
ShutdownTimeout = 5 * time.Second
|
|
SentryFlushTimeout = 2 * time.Second
|
|
)
|
|
|
|
// Params defines dependencies for Server.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Logger *logger.Logger
|
|
Globals *globals.Globals
|
|
Config *config.Config
|
|
Middleware *middleware.Middleware
|
|
Handlers *handlers.Handlers
|
|
}
|
|
|
|
// Server is the main HTTP server.
|
|
type Server struct {
|
|
log *slog.Logger
|
|
config *config.Config
|
|
globals *globals.Globals
|
|
mw *middleware.Middleware
|
|
h *handlers.Handlers
|
|
startupTime time.Time
|
|
exitCode int
|
|
sentryEnabled bool
|
|
cancelFunc context.CancelFunc
|
|
httpServer *http.Server
|
|
router *chi.Mux
|
|
}
|
|
|
|
// New creates a new Server instance.
|
|
func New(lc fx.Lifecycle, params Params) (*Server, error) {
|
|
s := &Server{
|
|
log: params.Logger.Get(),
|
|
config: params.Config,
|
|
globals: params.Globals,
|
|
mw: params.Middleware,
|
|
h: params.Handlers,
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(ctx context.Context) error {
|
|
s.startupTime = time.Now()
|
|
go s.Run(context.WithoutCancel(ctx))
|
|
|
|
return nil
|
|
},
|
|
OnStop: func(_ context.Context) error {
|
|
if s.cancelFunc != nil {
|
|
s.cancelFunc()
|
|
}
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
return s, nil
|
|
}
|
|
|
|
// Run starts the server.
|
|
func (s *Server) Run(ctx context.Context) {
|
|
s.enableSentry()
|
|
s.serve(ctx)
|
|
}
|
|
|
|
// MaintenanceMode returns whether maintenance mode is enabled.
|
|
func (s *Server) MaintenanceMode() bool {
|
|
return s.config.MaintenanceMode
|
|
}
|
|
|
|
func (s *Server) enableSentry() {
|
|
s.sentryEnabled = false
|
|
|
|
if s.config.SentryDSN == "" {
|
|
return
|
|
}
|
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
Dsn: s.config.SentryDSN,
|
|
Release: fmt.Sprintf("%s-%s", s.globals.Appname, s.globals.Version),
|
|
})
|
|
if err != nil {
|
|
s.log.Error("sentry init failure", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
s.log.Info("sentry error reporting activated")
|
|
s.sentryEnabled = true
|
|
}
|
|
|
|
func (s *Server) serve(ctx context.Context) int {
|
|
ctx, cancelFunc := context.WithCancel(ctx)
|
|
s.cancelFunc = cancelFunc
|
|
|
|
go func() {
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Ignore(syscall.SIGPIPE)
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
sig := <-c
|
|
s.log.Info("signal received", "signal", sig)
|
|
|
|
if s.cancelFunc != nil {
|
|
s.cancelFunc()
|
|
}
|
|
}()
|
|
|
|
go s.serveUntilShutdown()
|
|
|
|
<-ctx.Done()
|
|
s.cleanShutdown(ctx)
|
|
|
|
return s.exitCode
|
|
}
|
|
|
|
func (s *Server) cleanShutdown(ctx context.Context) {
|
|
s.exitCode = 0
|
|
|
|
ctxShutdown, shutdownCancel := context.WithTimeout(
|
|
context.WithoutCancel(ctx), ShutdownTimeout)
|
|
defer shutdownCancel()
|
|
|
|
if s.httpServer != nil {
|
|
err := s.httpServer.Shutdown(ctxShutdown)
|
|
if err != nil {
|
|
s.log.Error("server clean shutdown failed", "error", err)
|
|
}
|
|
}
|
|
|
|
if s.sentryEnabled {
|
|
sentry.Flush(SentryFlushTimeout)
|
|
}
|
|
}
|