102 lines
2 KiB
Go
102 lines
2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"dynatron.me/x/blasphem/internal/common"
|
|
"dynatron.me/x/blasphem/pkg/auth"
|
|
"dynatron.me/x/blasphem/pkg/blas"
|
|
"dynatron.me/x/blasphem/pkg/config"
|
|
"dynatron.me/x/blasphem/pkg/frontend"
|
|
)
|
|
|
|
const LogHeader = `${time_rfc3339} ${level} ${prefix} ${short_file}:${line}`
|
|
|
|
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.SetPrefix(common.AppName)
|
|
s.Echo.Logger.SetHeader(LogHeader)
|
|
|
|
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() {
|
|
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
|
|
}
|