feat(backend): jwt middleware, login with redis rate limit, /auth/me
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user