feat: 服务器状态API

This commit is contained in:
Akkia
2022-05-17 11:21:27 +08:00
parent 5fca0a52ef
commit 76928b71d9
6 changed files with 172 additions and 5 deletions

View File

@@ -34,7 +34,6 @@ func Authorize(opt AuthorizeOption) func(*gin.Context) {
Link: opt.Redirect,
Btn: opt.Btn,
}
var isLogin bool
// 用户鉴权
@@ -50,6 +49,19 @@ func Authorize(opt AuthorizeOption) func(*gin.Context) {
}
}
// API鉴权
apiToken := c.GetHeader("Authorization")
if apiToken != "" {
var t model.ApiToken
// TODO: 需要有缓存机制 减少数据库查询次数
if err := singleton.DB.Where("token = ?", apiToken).First(&t).Error; err == nil {
isLogin = t.TokenExpired.After(time.Now())
}
if isLogin {
c.Set(model.CtxKeyAuthorizedUser, &t)
}
}
// 已登录且只能游客访问
if isLogin && opt.Guest {
ShowErrorPage(c, commonErr, opt.IsPage)