package server import ( "io/fs" "log" "net/http" "sync" "github.com/labstack/echo/v4" "dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/bus" "dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/frontend" ) type Server struct { *echo.Echo *bus.Bus auth.Authenticator rootFS fs.FS wg sync.WaitGroup cfg *config.Config } type StatusWriter struct { http.ResponseWriter status int } func (w *StatusWriter) WriteHeader(status int) { w.status = status w.ResponseWriter.WriteHeader(status) } func logHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rw := &StatusWriter{ResponseWriter: w} h.ServeHTTP(rw, r) logRequest(rw.status, r) }) } func logRequest(status int, r *http.Request) { log.Println(r.Method, status, r.URL.Path) } func New(cfg *config.Config) (s *Server, err error) { s = &Server{ Echo: echo.New(), cfg: cfg, } s.InitAuth() s.Echo.Debug = true s.Echo.HideBanner = true 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) return s, nil } func (s *Server) Go() error { s.wg.Add(1) go func() { err := s.Start(s.cfg.Server.Bind) if err != nil { log.Fatal(err) } s.wg.Done() }() s.wg.Wait() return nil }