66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"io/fs"
|
|
"log"
|
|
"sync"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"dynatron.me/x/blasphem/pkg/auth"
|
|
"dynatron.me/x/blasphem/pkg/blas"
|
|
"dynatron.me/x/blasphem/pkg/config"
|
|
"dynatron.me/x/blasphem/pkg/frontend"
|
|
)
|
|
|
|
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 := blas.New(cfg)
|
|
|
|
s = &Server{
|
|
Blas: b,
|
|
Echo: echo.New(),
|
|
}
|
|
s.InitAuth()
|
|
|
|
s.Echo.Debug = true
|
|
s.Echo.HideBanner = true
|
|
|
|
s.installRoutes()
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Server) Go() error {
|
|
s.wg.Add(1)
|
|
go func() {
|
|
err := s.Start(s.Config.Server.Bind)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
s.wg.Done()
|
|
}()
|
|
|
|
s.wg.Wait()
|
|
|
|
return nil
|
|
}
|