blasphem/pkg/server/server.go

67 lines
1.1 KiB
Go
Raw Normal View History

2022-09-25 11:42:36 -04:00
package server
import (
2022-09-25 14:07:47 -04:00
"io/fs"
2022-09-25 11:42:36 -04:00
"log"
"sync"
2022-09-30 14:37:40 -04:00
"github.com/labstack/echo/v4"
"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
)
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 08:52:48 -04:00
b := blas.New(cfg)
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-09-30 23:54:21 -04:00
s.InitAuth()
2022-09-30 14:37:40 -04:00
s.Echo.Debug = true
s.Echo.HideBanner = true
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
}
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-09-25 11:42:36 -04:00
if err != nil {
log.Fatal(err)
}
s.wg.Done()
}()
s.wg.Wait()
return nil
}