This commit is contained in:
Daniel Ponte 2022-10-02 08:52:48 -04:00
parent 8cb09bb609
commit f3fd61643d
2 changed files with 27 additions and 31 deletions

View file

@ -141,6 +141,18 @@ func (f *Flow) progress(c echo.Context) error {
return nil return nil
} }
func (a *Authenticator) LoginFlowDeleteHandler(c echo.Context) error {
flowID := c.Param("flow_id")
if flowID == "" {
return c.String(http.StatusBadRequest, "empty flow ID")
}
delete(a.Flows, FlowID(flowID))
return c.String(http.StatusOK, "deleted")
}
func (a *Authenticator) LoginFlowHandler(c echo.Context) error { func (a *Authenticator) LoginFlowHandler(c echo.Context) error {
if c.Request().Method == http.MethodPost && strings.HasPrefix(c.Request().Header.Get(echo.HeaderContentType), "text/plain") { if c.Request().Method == http.MethodPost && strings.HasPrefix(c.Request().Header.Get(echo.HeaderContentType), "text/plain") {
// hack around the content-type, Context.JSON refuses to work otherwise // hack around the content-type, Context.JSON refuses to work otherwise

View file

@ -3,64 +3,48 @@ package server
import ( import (
"io/fs" "io/fs"
"log" "log"
"net/http"
"sync" "sync"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/bus" "dynatron.me/x/blasphem/pkg/blas"
"dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/frontend" "dynatron.me/x/blasphem/pkg/frontend"
) )
type Server struct { type Server struct {
*blas.Blas
*echo.Echo *echo.Echo
*bus.Bus
auth.Authenticator auth.Authenticator
rootFS fs.FS rootFS fs.FS
wg sync.WaitGroup wg sync.WaitGroup
cfg *config.Config
} }
type StatusWriter struct { func (s *Server) installRoutes() {
http.ResponseWriter s.GET("/", echo.WrapHandler(frontend.FSHandler))
status int s.GET("/api/websocket", s.wsHandler)
} s.GET("/auth/authorize", s.AuthorizeHandler)
s.GET("/auth/providers", s.ProvidersHandler)
func (w *StatusWriter) WriteHeader(status int) { s.POST("/auth/login_flow", s.LoginFlowHandler)
w.status = status s.POST("/auth/login_flow/:flow_id", s.LoginFlowHandler)
w.ResponseWriter.WriteHeader(status) s.DELETE("/auth/login_flow/:flow_id", s.LoginFlowDeleteHandler)
}
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) { func New(cfg *config.Config) (s *Server, err error) {
b := blas.New(cfg)
s = &Server{ s = &Server{
Blas: b,
Echo: echo.New(), Echo: echo.New(),
cfg: cfg,
} }
s.InitAuth() s.InitAuth()
s.Echo.Debug = true s.Echo.Debug = true
s.Echo.HideBanner = true s.Echo.HideBanner = true
s.GET("/", echo.WrapHandler(frontend.FSHandler)) s.installRoutes()
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 return s, nil
} }
@ -68,7 +52,7 @@ func New(cfg *config.Config) (s *Server, err error) {
func (s *Server) Go() error { func (s *Server) Go() error {
s.wg.Add(1) s.wg.Add(1)
go func() { go func() {
err := s.Start(s.cfg.Server.Bind) err := s.Start(s.Config.Server.Bind)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }