feat(backend): jwt middleware, login with redis rate limit, /auth/me

This commit is contained in:
2026-09-04 23:58:26 +08:00
parent a2f202ada6
commit bc88cabc47
9 changed files with 341 additions and 8 deletions
+10 -7
View File
@@ -6,19 +6,22 @@ import (
"github.com/gin-gonic/gin"
"booklib/internal/config"
"booklib/internal/redispkg"
"booklib/internal/store"
)
type api struct {
cfg *config.Config
// st, rdb, sc 字段在 Task 4/9 加入
}
func NewRouter(cfg *config.Config) *gin.Engine {
func NewRouter(cfg *config.Config, st *store.Store, rdb *redispkg.R) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
_ = &api{cfg: cfg} // Task 4/9 起此处为 a := &api{...},handler 挂在 a 上
a := &api{cfg: cfg, st: st, rdb: rdb}
r := gin.New()
r.Use(gin.Recovery())
g := r.Group("/api")
g.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
g.POST("/auth/login", a.login)
p := g.Group("", a.authMw())
p.GET("/auth/me", a.me)
// ponytail: 501 垫片,Task 5 换成 a.createUser;无此路由则 member 403 测不到
p.POST("/users", a.adminOnly(), func(c *gin.Context) { c.Status(http.StatusNotImplemented) })
return r
}