blasphem/pkg/server/server.go

116 lines
2.5 KiB
Go

package server
import (
"context"
"io/fs"
"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
rootFS fs.FS
wg sync.WaitGroup
}
func (s *Server) installRoutes() {
s.GET("/", echo.WrapHandler(frontend.FSHandler))
s.GET("/api/websocket", s.wsHandler)
s.GET("/auth/authorize", s.AuthorizeHandler)
s.GET("/auth/providers", s.ProvidersHandler)
s.POST("/auth/login_flow", s.LoginFlowHandler)
s.POST("/auth/login_flow/:flow_id", s.LoginFlowHandler)
s.DELETE("/auth/login_flow/:flow_id", s.LoginFlowDeleteHandler)
}
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
s.Echo.Logger = lecho.From(log.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
}