blasphem/pkg/server/server.go

105 lines
2 KiB
Go
Raw Normal View History

2022-09-25 11:42:36 -04:00
package server
import (
2022-10-25 00:16:29 -04:00
"context"
"net/http"
"os"
"os/signal"
2022-09-25 11:42:36 -04:00
"sync"
2022-10-25 00:16:29 -04:00
"time"
2022-09-25 11:42:36 -04:00
2022-09-30 14:37:40 -04:00
"github.com/labstack/echo/v4"
2022-10-25 19:03:23 -04:00
"github.com/rs/zerolog/log"
"github.com/ziflex/lecho/v3"
2022-09-30 14:37:40 -04:00
2022-10-02 08:52:48 -04:00
"dynatron.me/x/blasphem/pkg/blas"
2022-10-25 19:03:23 -04:00
conf "dynatron.me/x/blasphem/pkg/server/config"
2022-09-25 11:42:36 -04:00
)
type Server struct {
2022-12-20 13:16:30 -05:00
blas.Core
2022-09-30 14:37:40 -04:00
*echo.Echo
2022-10-25 21:18:50 -04:00
wg sync.WaitGroup
2022-09-25 11:42:36 -04:00
}
2022-10-02 08:52:48 -04:00
func (s *Server) installRoutes() {
s.GET("/api/websocket", s.wsHandler)
2022-09-25 21:24:35 -04:00
}
2022-12-20 13:16:30 -05:00
func New(core blas.Core) (s *Server, err error) {
2022-09-25 21:24:35 -04:00
s = &Server{
2022-12-20 13:16:30 -05:00
Core: core,
2022-09-30 14:37:40 -04:00
Echo: echo.New(),
2022-09-25 14:07:47 -04:00
}
2022-12-19 19:24:01 -05:00
2022-09-30 14:37:40 -04:00
s.Echo.Debug = true
s.Echo.HideBanner = true
2022-10-25 21:18:50 -04:00
logger := lecho.From(log.Logger)
s.Echo.Logger = logger
2022-12-20 13:16:30 -05:00
cfg := s.Conf()
2022-10-25 21:18:50 -04:00
if cfg.Server.LogRequestErrors {
s.Echo.Use(lecho.Middleware(lecho.Config{
Logger: logger,
}))
}
2022-10-25 19:03:23 -04:00
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]
2022-09-25 14:07:47 -04:00
2022-10-02 08:52:48 -04:00
s.installRoutes()
2022-09-25 11:42:36 -04:00
return s, nil
}
2022-10-25 00:16:29 -04:00
func (s *Server) Shutdown(ctx context.Context) error {
2022-12-20 13:16:30 -05:00
err := s.ShutdownBlas(ctx)
2022-10-25 00:16:29 -04:00
if err != nil {
return err
}
return s.Echo.Shutdown(ctx)
}
2022-09-25 11:42:36 -04:00
func (s *Server) Go() error {
s.wg.Add(1)
go func() {
2022-12-20 13:16:30 -05:00
log.Info().Str("version", s.Version()).Str("bind", s.Conf().Server.Bind).Msg("Server listening")
err := s.Start(s.Conf().Server.Bind)
2022-10-25 00:16:29 -04:00
if err != nil && err != http.ErrServerClosed {
s.Logger.Fatal(err)
2022-09-25 11:42:36 -04:00
}
s.wg.Done()
}()
2022-10-25 00:16:29 -04:00
// 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)
}
2022-09-25 11:42:36 -04:00
s.wg.Wait()
2022-10-25 19:03:23 -04:00
s.Logger.Info("Shutdown complete")
2022-09-25 11:42:36 -04:00
return nil
}