package server import ( "context" "net/http" "os" "os/signal" "sync" "time" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" "github.com/ziflex/lecho/v3" "dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/blas" "dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/frontend" conf "dynatron.me/x/blasphem/pkg/server/config" ) type Server struct { *blas.Blas *echo.Echo auth.Authenticator wg sync.WaitGroup } func (s *Server) installRoutes() { s.GET("/", echo.WrapHandler(frontend.FSHandler)) s.GET("/api/websocket", s.wsHandler) s.Authenticator.InstallRoutes(s.Echo) } func New(cfg *config.Config) (s *Server, err error) { b, err := blas.New(cfg) if err != nil { return nil, err } s = &Server{ Blas: b, Echo: echo.New(), } err = s.InitAuth(b.Store) if err != nil { return s, err } s.Echo.Debug = true s.Echo.HideBanner = true logger := lecho.From(log.Logger) s.Echo.Logger = logger if cfg.Server.LogRequestErrors { s.Echo.Use(lecho.Middleware(lecho.Config{ Logger: logger, })) } s.Echo.HidePort = true ipext := map[conf.IPSource]echo.IPExtractor{ conf.Legacy: nil, conf.Direct: echo.ExtractIPDirect(), conf.XForwardedFor: echo.ExtractIPFromXFFHeader(), conf.XRealIP: echo.ExtractIPFromRealIPHeader(), } if ipext[cfg.Server.IPSource] == nil { log.Warn().Msg("ip_source not explicitly set") } s.Echo.IPExtractor = ipext[cfg.Server.IPSource] s.installRoutes() return s, nil } func (s *Server) Shutdown(ctx context.Context) error { err := s.Blas.Shutdown(ctx) if err != nil { return err } return s.Echo.Shutdown(ctx) } func (s *Server) Go() error { s.wg.Add(1) go func() { log.Info().Str("bind", s.Config.Server.Bind).Msg("Server listening") err := s.Start(s.Config.Server.Bind) if err != nil && err != http.ErrServerClosed { s.Logger.Fatal(err) } s.wg.Done() }() // Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds. // Use a buffered channel to avoid missing signals as recommended for signal.Notify quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt) <-quit ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := s.Shutdown(ctx); err != nil { s.Logger.Fatal(err) } s.wg.Wait() s.Logger.Info("Shutdown complete") return nil }