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
This commit is contained in:
2026-02-06 17:04:17 -05:00
parent aee7fb4960
commit a75cd7e51a
4 changed files with 11 additions and 18 deletions
+3 -16
View File
@@ -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)
+5
View File
@@ -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)
+2 -2
View File
@@ -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")