From a75cd7e51ab8f5bb56a951c0a07ea0daa609bf31 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Fri, 6 Feb 2026 17:04:17 -0500 Subject: [PATCH] refactor: simplify router configuration and handler setup - Move JWT middleware creation to shared function - Simplify library route registration - Add bulk-add-books endpoint to collections - Clean up duplicate handler setup code - Improve route organization and maintainability --- internal/handlers/ebook.go | 1 + internal/router/library.go | 19 +++---------------- internal/router/router.go | 5 +++++ internal/router/sync.go | 4 ++-- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/internal/handlers/ebook.go b/internal/handlers/ebook.go index ec1ffca..6e34453 100644 --- a/internal/handlers/ebook.go +++ b/internal/handlers/ebook.go @@ -82,6 +82,7 @@ func SetupRoutes(g *echo.Group, db *database.Queries, connManager *wsync.Connect collections.POST("/:id/books", collectionHandler.AddBooks) collections.DELETE("/:id/books/:bookId", collectionHandler.RemoveBook) collections.POST("/:id/books/bulk-remove", collectionHandler.BulkRemoveBooks) + collections.POST("/bulk-add-books", collectionHandler.HandleBulkAddBooks) collections.POST("/test-rules", collectionHandler.TestRules) // Device shelf mapping routes diff --git a/internal/router/library.go b/internal/router/library.go index b0fe1cd..6d56872 100644 --- a/internal/router/library.go +++ b/internal/router/library.go @@ -3,8 +3,6 @@ package router import ( "bookhoard/internal/handlers" - "github.com/golang-jwt/jwt/v5" - "github.com/labstack/echo-jwt/v4" "github.com/labstack/echo/v4" ) @@ -12,24 +10,13 @@ func registerLibraryRoutes(cfg *Config) { e := cfg.Echo // JWT middleware for protected routes - jwtMiddleware := echojwt.WithConfig(echojwt.Config{ - SigningKey: []byte(cfg.Cfg.JWTSecret), - ContextKey: "user", - SuccessHandler: func(c echo.Context) { - token := c.Get("user").(*jwt.Token) - claims := token.Claims.(jwt.MapClaims) - c.Set("user_id", claims["user_id"]) - c.Set("user_role", claims["user_role"]) - c.Set("user_email", claims["user_email"]) - c.Set("user_username", claims["user_username"]) - }, - }) + jwtMiddleware := createJWTMiddleware(cfg) // Protected routes group protected := e.Group("/api", jwtMiddleware) - // Setup ebook handler routes - h := handlers.SetupRoutes(protected, cfg.Queries, cfg.ConnManager) + // Create handler for library-specific convenience routes + h := handlers.NewHandler(cfg.Queries, cfg.ConnManager) // Public library types endpoint e.GET("/api/libraries/types", cfg.LibraryHandler.GetLibraryTypes) diff --git a/internal/router/router.go b/internal/router/router.go index 2fe6296..c25b819 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -108,6 +108,11 @@ func RegisterRoutes(cfg *Config) { rateLimiter := ratelimit.NewRateLimiter(rateLimiterConfig) rateLimitMiddleware := ratelimit.RateLimiterMiddleware(rateLimiter) + // Register core application routes (collections, devices, media, etc.) - ONCE + jwtMiddleware := createJWTMiddleware(cfg) + protected := e.Group("/api", jwtMiddleware) + handlers.SetupRoutes(protected, cfg.Queries, cfg.ConnManager) + // Register route groups registerAuthRoutes(cfg, rateLimitMiddleware) registerLibraryRoutes(cfg) diff --git a/internal/router/sync.go b/internal/router/sync.go index 1a26a37..d7e6f04 100644 --- a/internal/router/sync.go +++ b/internal/router/sync.go @@ -12,8 +12,8 @@ func registerSyncRoutes(cfg *Config) { protected := e.Group("/api", jwtMiddleware) - // Setup ebook handler routes first - h := handlers.SetupRoutes(protected, cfg.Queries, cfg.ConnManager) + // Create handler for sync-specific routes + h := handlers.NewHandler(cfg.Queries, cfg.ConnManager) // Book matching and unlinked book resolution routes sync := protected.Group("/sync")