Add stub implementations for: - library.go: Library management routes (admin + user visibility) - device.go: Device registration and management routes - router.go: Updated to import jwt package Router package structure is complete with all route groups defined. Next step: Incrementally migrate routes from main.go by calling router.RegisterRoutes() and removing duplicate definitions. All verification checks pass (26/26).
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package router
|
|
|
|
import (
|
|
"bookhoard/internal/handlers"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo-jwt/v4"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
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"])
|
|
},
|
|
})
|
|
|
|
// Protected routes group
|
|
protected := e.Group("/api", jwtMiddleware)
|
|
|
|
// Setup ebook handler routes
|
|
h := handlers.SetupRoutes(protected, cfg.Queries, cfg.ConnManager)
|
|
|
|
// Public library types endpoint
|
|
e.GET("/api/libraries/types", cfg.LibraryHandler.GetLibraryTypes)
|
|
|
|
// Library management routes
|
|
library := protected.Group("/libraries")
|
|
|
|
// Admin-only library routes
|
|
adminLibrary := library.Group("", handlers.AdminMiddleware)
|
|
adminLibrary.POST("", cfg.LibraryHandler.CreateLibrary)
|
|
adminLibrary.GET("", cfg.LibraryHandler.ListLibraries)
|
|
adminLibrary.GET("/:id", cfg.LibraryHandler.GetLibrary)
|
|
adminLibrary.PUT("/:id", cfg.LibraryHandler.UpdateLibrary)
|
|
adminLibrary.DELETE("/:id", cfg.LibraryHandler.DeleteLibrary)
|
|
adminLibrary.POST("/:id/folders", cfg.LibraryHandler.AddLibraryFolder)
|
|
adminLibrary.GET("/:id/folders", cfg.LibraryHandler.GetLibraryFolders)
|
|
adminLibrary.DELETE("/:id/folders", cfg.LibraryHandler.DeleteLibraryFolder)
|
|
adminLibrary.GET("/:id/stats", cfg.LibraryHandler.GetLibraryStats)
|
|
adminLibrary.POST("/:id/scan", func(c echo.Context) error {
|
|
libraryID := c.Param("id")
|
|
scanReq := map[string]interface{}{
|
|
"library_id": libraryID,
|
|
}
|
|
c.Set("scan_request", scanReq)
|
|
return h.ScanEbooks(c)
|
|
})
|
|
adminLibrary.GET("/:id/media-items", func(c echo.Context) error {
|
|
libraryID := c.Param("id")
|
|
c.QueryParams().Set("library_id", libraryID)
|
|
return h.ListMediaItems(c)
|
|
})
|
|
|
|
// User library visibility control
|
|
userLibrary := library.Group("/visibility")
|
|
userLibrary.GET("", cfg.LibraryHandler.GetUserVisibleLibraries)
|
|
userLibrary.POST("", cfg.LibraryHandler.SetLibraryVisibility)
|
|
// Note: Remove endpoint may not exist - check handlers
|
|
}
|