- internal/upload owns chunked-upload domain logic (fingerprint resume, part tmp+rename, assemble, session sweep) and the shared UniquePath helper used by both single-file and chunked completion paths - handlers/uploads.go is now HTTP-only: bind params, call upload.U, map sentinel errors to the unchanged status/code/message contract - B16: session sweep moved out of the UploadInit request path onto the scanner ticker via a Sweeper hook (upload.U satisfies it) - unit tests for the package without PG/Redis; contract pinned by the existing handler integration tests (all green)
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"booklib/cmd/webui/handlers"
|
|
"booklib/internal/config"
|
|
"booklib/internal/redispkg"
|
|
"booklib/internal/scanner"
|
|
"booklib/internal/store"
|
|
"booklib/internal/upload"
|
|
)
|
|
|
|
func NewRouter(cfg *config.Config, st *store.Store, rdb *redispkg.R, sc *scanner.Scanner, up *upload.U) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
h := handlers.New(cfg, st, rdb, sc, up)
|
|
r := gin.New()
|
|
if e := r.SetTrustedProxies(cfg.TrustedProxies); e != nil {
|
|
panic(e)
|
|
}
|
|
r.Use(gin.Recovery())
|
|
g := r.Group("/api")
|
|
g.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
|
|
g.POST("/auth/login", h.Login)
|
|
|
|
p := g.Group("", h.AuthMw())
|
|
p.GET("/auth/me", h.Me)
|
|
|
|
users := p.Group("/users", h.AdminOnly())
|
|
users.GET("", h.ListUsers)
|
|
users.POST("", h.CreateUser)
|
|
users.DELETE("/:id", h.DeleteUser)
|
|
|
|
libs := p.Group("/libraries")
|
|
libs.GET("", h.ListLibraries)
|
|
libs.POST("", h.AdminOnly(), h.CreateLibrary)
|
|
libs.POST("/:id/scan", h.AdminOnly(), h.ScanLibrary)
|
|
libs.POST("/:id/upload", h.AdminOnly(), h.Upload)
|
|
libs.POST("/:id/upload/init", h.AdminOnly(), h.UploadInit)
|
|
|
|
uploads := p.Group("/uploads", h.AdminOnly())
|
|
uploads.GET("/:uid", h.UploadStatus)
|
|
uploads.POST("/:uid/complete", h.UploadComplete)
|
|
uploads.PUT("/:uid/parts/:index", h.UploadPart)
|
|
|
|
p.GET("/books", h.ListBooks)
|
|
p.GET("/books/:id", h.GetBook)
|
|
p.DELETE("/books/:id", h.AdminOnly(), h.DeleteBook)
|
|
p.GET("/books/:id/cover", h.ServeCover)
|
|
p.GET("/books/:id/file", h.ServeFile)
|
|
p.GET("/books/:id/pages", h.PagesCount)
|
|
p.GET("/books/:id/pages/:n", h.Page)
|
|
p.PUT("/books/:id/progress", h.PutProgress)
|
|
p.GET("/progress", h.ListProgress)
|
|
p.GET("/books/:id/bookmarks", h.ListBookmarks)
|
|
p.POST("/books/:id/bookmarks", h.CreateBookmark)
|
|
p.PATCH("/bookmarks/:id", h.PatchBookmark)
|
|
p.DELETE("/bookmarks/:id", h.DeleteBookmark)
|
|
return r
|
|
}
|