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/blas" "dynatron.me/x/blasphem/pkg/frontend" conf "dynatron.me/x/blasphem/pkg/server/config" ) type Server struct { blas.Core *echo.Echo wg sync.WaitGroup } type RouteHaver interface { InstallRoutes(e *echo.Echo) } func (s *Server) installRoutes() { s.GET("/api/websocket", s.wsHandler) s.Component(frontend.FrontendKey).(RouteHaver).InstallRoutes(s.Echo) for _, c := range s.Components() { if rh, ok := c.(RouteHaver); ok { rh.InstallRoutes(s.Echo) } } } func New(core blas.Core) (s *Server, err error) { s = &Server{ Core: core, Echo: echo.New(), } s.Echo.Debug = true s.Echo.HideBanner = true logger := lecho.From(log.Logger) s.Echo.Logger = logger cfg := s.Conf() 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.ShutdownBlas(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("version", s.Version()).Str("bind", s.Conf().Server.Bind).Msg("Server listening") err := s.Start(s.Conf().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 }