40 lines
835 B
Go
40 lines
835 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
)
|
|
|
|
// Logger 中间件
|
|
func Logger(r *ghttp.Request) {
|
|
startTime := gtime.TimestampMilli()
|
|
r.Middleware.Next()
|
|
endTime := gtime.TimestampMilli()
|
|
g.Log().Infof(r.GetCtx(),
|
|
"request: %s %s | status: %d | time: %dms",
|
|
r.Method,
|
|
r.URL.Path,
|
|
r.Response.Status,
|
|
endTime-startTime,
|
|
)
|
|
}
|
|
|
|
func Auth(r *ghttp.Request) {
|
|
//utils.GetUserInfo(r.GetCtx())
|
|
token := r.Header.Get("Authorization")
|
|
if token == "" || !gstr.HasPrefix(token, "Bearer ") {
|
|
r.Response.WriteStatusExit(401, "Unauthorized")
|
|
return
|
|
}
|
|
|
|
// 验证 token
|
|
if !validateToken(gstr.SubStrFrom(token, "7")) {
|
|
r.Response.WriteStatusExit(401, "Unauthorized")
|
|
return
|
|
}
|
|
|
|
r.Middleware.Next()
|
|
}
|