refactor(handlers): consume port interfaces, wire media+upload assembly (Task 25+26)
- H now holds only small port interfaces (UserStore/LibraryStore/BookStore/ ProgressStore/BookmarkStore/RateLimiter/Scanner/Media/UploadSessions); NewRouter is the composition root distributing *store.Store and *redispkg.R - ports.Media gains EnsurePage; ChaptersOf returns ports.Chapter (media's local duplicate dropped); *media.M now provably satisfies ports.Media; ports.UploadSessions gains LibraryID for root validation before Complete - content.go: duplicated page-index cache + cover self-heal + page extract logic removed in favor of media service — same redis keys, same contract; path traversal check stays in handler (403 semantics preserved) - getLibrary/getLibRow merged into getLib(c, id); idParam helper dedupes :id parsing; isUnique replaced by ports.IsUniqueViolation (Task 28 partial) - main.go assembles media + upload and passes upload.U as scanner Sweeper Full gate green: gofmt, vet, go test -p 1 (real PG+Redis, 0 skip)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
@@ -15,28 +16,45 @@ import (
|
||||
|
||||
"booklib/internal/auth"
|
||||
"booklib/internal/config"
|
||||
"booklib/internal/redispkg"
|
||||
"booklib/internal/scanner"
|
||||
"booklib/internal/store"
|
||||
"booklib/internal/upload"
|
||||
"booklib/internal/ports"
|
||||
)
|
||||
|
||||
// H 只依赖 ports 里的小口径接口(Task 25):具体实现由 main.go 装配,
|
||||
// 测试可注入手写 fake(portsfake),无需 PG/Redis。
|
||||
type H struct {
|
||||
cfg *config.Config
|
||||
st *store.Store
|
||||
rdb *redispkg.R
|
||||
sc *scanner.Scanner
|
||||
up *upload.U
|
||||
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
|
||||
}
|
||||
|
||||
func New(cfg *config.Config, st *store.Store, rdb *redispkg.R, sc *scanner.Scanner, up *upload.U) *H {
|
||||
return &H{cfg: cfg, st: st, rdb: rdb, sc: sc, up: up}
|
||||
func New(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) *H {
|
||||
return &H{cfg: cfg, users: users, libs: libs, books: books, progress: progress,
|
||||
bookmarks: bookmarks, rl: rl, sc: sc, med: med, up: up}
|
||||
}
|
||||
|
||||
func err(c *gin.Context, status int, code, msg string) {
|
||||
c.AbortWithStatusJSON(status, gin.H{"error": gin.H{"code": code, "message": msg}})
|
||||
}
|
||||
|
||||
// idParam 解析 :id 路径参数,失败已回复 400。
|
||||
func idParam(c *gin.Context) (int64, bool) {
|
||||
id, e := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if e != nil {
|
||||
err(c, http.StatusBadRequest, "bad_request", "bad id")
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
// dbErr 统一处理 store 层失败:记日志;连接类错误 503(Service Unavailable),其余 500
|
||||
// 注:brief 里的 pgxpool.ErrClosedPool 在 pgx v5 不存在,实际由 puddle 原样透出,用它替代;
|
||||
// PG 停机时池内连接先收到 SQLSTATE 57P01(administrator shutdown),故把 08xx/57Pxx 也归为 503
|
||||
|
||||
Reference in New Issue
Block a user