2022-09-25 11:42:36 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-10-25 00:16:29 -04:00
|
|
|
"context"
|
2022-09-25 14:07:47 -04:00
|
|
|
"io/fs"
|
2022-10-25 00:16:29 -04:00
|
|
|
"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 00:16:29 -04:00
|
|
|
"dynatron.me/x/blasphem/internal/common"
|
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-09-25 11:42:36 -04:00
|
|
|
)
|
|
|
|
|
2022-10-25 00:16:29 -04:00
|
|
|
const LogHeader = `${time_rfc3339} ${level} ${prefix} ${short_file}:${line}`
|
|
|
|
|
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-09-25 21:24:35 -04:00
|
|
|
rootFS fs.FS
|
2022-09-26 15:00:21 -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("/", echo.WrapHandler(frontend.FSHandler))
|
|
|
|
s.GET("/api/websocket", s.wsHandler)
|
|
|
|
s.GET("/auth/authorize", s.AuthorizeHandler)
|
|
|
|
s.GET("/auth/providers", s.ProvidersHandler)
|
2022-09-25 21:24:35 -04:00
|
|
|
|
2022-10-02 08:52:48 -04:00
|
|
|
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)
|
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-10-25 00:16:29 -04:00
|
|
|
err = s.InitAuth(b.Store)
|
|
|
|
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 00:16:29 -04:00
|
|
|
s.Echo.Logger.SetPrefix(common.AppName)
|
|
|
|
s.Echo.Logger.SetHeader(LogHeader)
|
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 {
|
|
|
|
err := s.Blas.Shutdown(ctx)
|
|
|
|
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-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 00:16:29 -04:00
|
|
|
s.Logger.Info("shutdown complete")
|
2022-09-25 11:42:36 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|