Update dependencies and fix main.go issues

This commit is contained in:
2026-01-23 21:27:18 -05:00
parent 5fe1c299b8
commit 8a951fb242
3 changed files with 56 additions and 21 deletions
+53
View File
@@ -2,12 +2,16 @@ 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"
)
@@ -24,6 +28,16 @@ func (cv *CustomValidator) Validate(i interface{}) error {
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)
e := echo.New()
// Set up validator
@@ -34,6 +48,45 @@ func main() {
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"])
},
})
// Protected routes
protected := e.Group("/api", jwtMiddleware)
protected.GET("/auth/profile", authHandler.GetProfile)
protected.PUT("/auth/profile", authHandler.UpdateProfile)
protected.GET("/auth/users", authHandler.ListUsers)
protected.POST("/auth/ebook-folders", authHandler.AddEbookFolder)
protected.GET("/auth/ebook-folders", authHandler.GetEbookFolders)
protected.DELETE("/auth/ebook-folders", authHandler.DeleteEbookFolder)
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
// Routes
handlers.SetupRoutes(protected, queries)
dummyUser := templates.User{ID: "", Username: "Admin", Email: "admin@example.com"}
// Routes