blasphem/pkg/server/server.go

118 lines
2.3 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
"dynatron.me/x/blasphem/pkg/auth"
2022-10-02 08:52:48 -04:00
"dynatron.me/x/blasphem/pkg/blas"
2022-09-25 11:42:36 -04:00
"dynatron.me/x/blasphem/pkg/config"
2022-09-25 14:07:47 -04:00
"dynatron.me/x/blasphem/pkg/frontend"
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-10-02 08:52:48 -04:00
*blas.Blas
2022-09-30 14:37:40 -04:00
*echo.Echo
auth.Authenticator
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() {
2022-10-29 10:39:06 -04:00
s.GET("/*", frontend.FSHandler)
2022-10-02 08:52:48 -04:00
s.GET("/api/websocket", s.wsHandler)
2022-09-25 21:24:35 -04:00
}
func New(cfg *config.Config) (s *Server, err error) {
2022-10-02 14:39:55 -04:00
b, err := blas.New(cfg)
if err != nil {
return nil, err
}
2022-10-02 08:52:48 -04:00
2022-09-25 21:24:35 -04:00
s = &Server{
2022-10-02 08:52:48 -04:00
Blas: b,
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
s.Authenticator, err = auth.New(s.Echo, b.Store)
2022-10-25 00:16:29 -04:00
if err != nil {
return s, err
}
2022-09-30 23:54:21 -04: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
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-19 19:24:01 -05:00
err := s.Blas.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-19 19:24:01 -05:00
log.Info().Str("version", s.Version()).Str("bind", s.Config.Server.Bind).Msg("Server listening")
2022-10-02 08:52:48 -04:00
err := s.Start(s.Config.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
}