- Add library routes to main router configuration - Implement media items API endpoints for library content - Update existing ebook handlers to use new schema - Add media rating and progress tracking - Maintain backward compatibility with existing endpoints - Support library-specific media item queries Updates application to support new multi-library architecture
210 lines
5.9 KiB
Go
210 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bookmann/internal/config"
|
|
"bookmann/internal/database"
|
|
"bookmann/internal/handlers"
|
|
"bookmann/templates"
|
|
"bytes"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo-jwt/v4"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
)
|
|
|
|
// CustomValidator wraps the go-playground validator
|
|
type CustomValidator struct {
|
|
validator *validator.Validate
|
|
}
|
|
|
|
func (cv *CustomValidator) Validate(i interface{}) error {
|
|
return cv.validator.Struct(i)
|
|
}
|
|
|
|
func main() {
|
|
cfg := config.LoadConfig()
|
|
|
|
dbPool, err := database.NewConnection(cfg.DatabaseURL())
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to database:", err)
|
|
}
|
|
defer dbPool.Close()
|
|
|
|
queries := database.New(dbPool)
|
|
|
|
authHandler := handlers.NewAuthHandler(queries, cfg.JWTSecret)
|
|
libraryHandler := handlers.NewLibraryHandler(queries)
|
|
|
|
e := echo.New()
|
|
|
|
// Set up validator
|
|
e.Validator = &CustomValidator{validator: validator.New()}
|
|
|
|
// Middleware
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
e.Use(middleware.CORS())
|
|
|
|
// Auth routes (no auth required)
|
|
e.POST("/api/auth/register", authHandler.Register)
|
|
e.POST("/api/auth/login", authHandler.Login)
|
|
|
|
// JWT middleware for protected routes
|
|
jwtMiddleware := echojwt.WithConfig(echojwt.Config{
|
|
SigningKey: []byte(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
|
|
protected := e.Group("/api", jwtMiddleware)
|
|
protected.GET("/auth/profile", authHandler.GetProfile)
|
|
protected.PUT("/auth/profile", authHandler.UpdateProfile)
|
|
|
|
// Admin-only routes for user and folder management
|
|
admin := protected.Group("/auth", handlers.AdminMiddleware)
|
|
admin.GET("/users", authHandler.ListUsers)
|
|
|
|
// Library management routes
|
|
library := protected.Group("/libraries")
|
|
library.GET("/types", libraryHandler.GetLibraryTypes)
|
|
|
|
// Admin-only library routes
|
|
adminLibrary := library.Group("", handlers.AdminMiddleware)
|
|
adminLibrary.POST("", libraryHandler.CreateLibrary)
|
|
adminLibrary.GET("", libraryHandler.ListLibraries)
|
|
adminLibrary.GET("/:id", libraryHandler.GetLibrary)
|
|
adminLibrary.PUT("/:id", libraryHandler.UpdateLibrary)
|
|
adminLibrary.DELETE("/:id", libraryHandler.DeleteLibrary)
|
|
adminLibrary.POST("/:id/folders", libraryHandler.AddLibraryFolder)
|
|
adminLibrary.GET("/:id/folders", libraryHandler.GetLibraryFolders)
|
|
adminLibrary.DELETE("/:id/folders", libraryHandler.DeleteLibraryFolder)
|
|
adminLibrary.GET("/:id/stats", libraryHandler.GetLibraryStats)
|
|
|
|
// User library visibility control
|
|
protected.POST("/libraries/visibility", libraryHandler.SetLibraryVisibility)
|
|
protected.GET("/libraries/visible", libraryHandler.GetUserVisibleLibraries)
|
|
|
|
protected.DELETE("/auth/account", authHandler.DeleteAccount)
|
|
protected.PUT("/library/scan-settings", authHandler.UpdateScanSettings)
|
|
protected.GET("/library/scan-settings", authHandler.GetScanSettings)
|
|
|
|
// Auth update routes
|
|
authGroup := e.Group("/api/auth", jwtMiddleware)
|
|
authGroup.PUT("/email", authHandler.UpdateEmail)
|
|
authGroup.PUT("/username", authHandler.UpdateUsername)
|
|
authGroup.PUT("/password", authHandler.UpdatePassword)
|
|
authGroup.PUT("/theme", authHandler.UpdateTheme)
|
|
// force rebuild
|
|
|
|
// Static files
|
|
e.Static("/static", "static")
|
|
|
|
// Routes
|
|
handlers.SetupRoutes(protected, queries)
|
|
|
|
// Dashboard route (protected)
|
|
protected.GET("/dashboard", func(c echo.Context) error {
|
|
userID := c.Get("user_id").(string)
|
|
userEmail := c.Get("user_email").(string)
|
|
userUsername := c.Get("user_username").(string)
|
|
userRole := c.Get("user_role").(string)
|
|
|
|
user := templates.User{
|
|
ID: userID,
|
|
Email: userEmail,
|
|
Username: userUsername,
|
|
Role: userRole,
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
err := templates.Dashboard(user).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
dummyUser := templates.User{ID: "", Username: "Admin", Email: "admin@example.com"}
|
|
|
|
// Routes
|
|
e.GET("/", func(c echo.Context) error {
|
|
loggedIn := false
|
|
var buf bytes.Buffer
|
|
err := templates.Index(loggedIn).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/login", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.Login().Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/register", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.Register().Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/admin", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.Admin(dummyUser).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/admin/", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.Admin(dummyUser).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/admin/profile", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.AdminProfile(dummyUser).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
e.GET("/admin/library", func(c echo.Context) error {
|
|
var buf bytes.Buffer
|
|
err := templates.AdminLibrary(dummyUser).Render(c.Request().Context(), &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.HTML(http.StatusOK, buf.String())
|
|
})
|
|
|
|
// Start server
|
|
log.Printf("Starting server on port %s", cfg.ServerPort)
|
|
e.Logger.Fatal(e.Start(":" + cfg.ServerPort))
|
|
}
|