路由中间件
路由中间件统一写在 middleware
目录下,如果路由指定了中间件,那么访问路由之前会先经过这里
package middleware
// Authorization 验证中间件
func Authorization() tg.HandlerFunc {
return func(ctx *tg.Context) {
authorization := ctx.Request.Header.Get("Authorization")
token := tgtoken.GetAuthorization(authorization)
tgtoken.ParseToken(token, tgcfg.Config.Get("jwtKey").String())
ctx.Next()
}
}
package middleware
// Authorization 验证中间件
func Authorization() tg.HandlerFunc {
return func(ctx *tg.Context) {
authorization := ctx.Request.Header.Get("Authorization")
token := tgtoken.GetAuthorization(authorization)
tgtoken.ParseToken(token, tgcfg.Config.Get("jwtKey").String())
ctx.Next()
}
}
ctx
代表上下文,可从中获取 Response
Request
等
Next()
该函数代表向下执行不阻拦