- internal/ports/portsfake: hand-written in-memory fakes for all 9 ports
(Users/Libraries/Books/Progress/Bookmarks/RateLimiter/Scanner/Media/Uploads);
error semantics mirror the real store exactly — pgx.ErrNoRows for misses,
store.ErrLastAdmin guard, and *pgconn.PgError{Code:23505} for unique
violations (ports.IsUniqueViolation only accepts the PgError shape, so the
fakes must produce it to exercise the 409 branch without touching prod code)
- Media/Uploads fakes are hook-programmable: one field per error branch, so a
test can force e.g. CompleteErr=ErrIncomplete without stubbing the rest
- Scanner fake is mutex-guarded + WaitForScan: handler fires ScanLibraryByID
in a goroutine, tests stay deterministic
- router_test.go: TestRouterContract pins all 27 routes — any route table
change now fails the test explicitly
- handler unit tests (~30 cases, no PG/Redis): users CRUD branches (self-delete
400, last-admin 400, dup 409, 204 ok), library reserved names (contract:
code=bad_request message=reserved_name, per original impl), upload sentinel
mapping (413/400/404 per branch)
- NewRouter takes pure port interfaces; main.go distributes *store.Store
across the 5 store ports at the composition root
Full gate green: gofmt, vet, go test -p 1 (real PG+Redis, 0 skip)
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"booklib/cmd/webui/handlers"
|
|
"booklib/internal/config"
|
|
"booklib/internal/ports"
|
|
)
|
|
|
|
// NewRouter 只依赖 port 接口:main.go 传具体实现(*store.Store 满足 5 个 store
|
|
// 接口,*redispkg.R 满足 RateLimiter),测试传 portsfake。
|
|
func NewRouter(cfg *config.Config, users ports.UserStore, libs ports.LibraryStore, books ports.BookStore,
|
|
progress ports.ProgressStore, bookmarks ports.BookmarkStore, rl ports.RateLimiter,
|
|
sc ports.Scanner, med ports.Media, up ports.UploadSessions) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
h := handlers.New(cfg, users, libs, books, progress, bookmarks, rl, sc, med, 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)
|
|
|
|
usersGrp := p.Group("/users", h.AdminOnly())
|
|
usersGrp.GET("", h.ListUsers)
|
|
usersGrp.POST("", h.CreateUser)
|
|
usersGrp.DELETE("/:id", h.DeleteUser)
|
|
|
|
libsGrp := p.Group("/libraries")
|
|
libsGrp.GET("", h.ListLibraries)
|
|
libsGrp.POST("", h.AdminOnly(), h.CreateLibrary)
|
|
libsGrp.POST("/:id/scan", h.AdminOnly(), h.ScanLibrary)
|
|
libsGrp.POST("/:id/upload", h.AdminOnly(), h.Upload)
|
|
libsGrp.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
|
|
}
|