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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user