package router import ( "bytes" "context" "log" "net/http" "strings" "bookhoard/internal/setupstatus" "bookhoard/templates" "github.com/labstack/echo/v5" ) func isSetupComplete(cfg *Config) bool { return setupstatus.IsSetupComplete(context.Background(), cfg.Queries) } func setupRedirectMiddleware(cfg *Config) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c *echo.Context) error { path := c.Request().URL.Path if path == "/setup" || path == "/setup/" { return next(c) } if strings.HasPrefix(path, "/api/") { return next(c) } if strings.HasPrefix(path, "/static/") || path == "/health" || path == "/favicon.ico" { return next(c) } if !isSetupComplete(cfg) { return c.Redirect(http.StatusFound, "/setup") } return next(c) } } } func registerSetupRoutes(cfg *Config) { e := cfg.Echo e.GET("/setup", func(c *echo.Context) error { if isSetupComplete(cfg) { return c.Redirect(http.StatusFound, "/") } var buf bytes.Buffer if err := templates.Setup().Render(c.Request().Context(), &buf); err != nil { log.Printf("Failed to render setup template: %v", err) return c.HTML(http.StatusInternalServerError, "Failed to render setup page") } return c.HTML(http.StatusOK, buf.String()) }) }