mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 20:50:06 +00:00
refactor: rename monitor -> service
This commit is contained in:
@@ -43,121 +43,6 @@ func (ma *memberAPI) serve() {
|
||||
mr.POST("/setting", ma.updateSetting)
|
||||
mr.DELETE("/:model/:id", ma.delete)
|
||||
mr.POST("/logout", ma.logout)
|
||||
mr.GET("/token", ma.getToken)
|
||||
mr.POST("/token", ma.issueNewToken)
|
||||
mr.DELETE("/token/:token", ma.deleteToken)
|
||||
}
|
||||
|
||||
type apiResult struct {
|
||||
Token string `json:"token"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
// getToken 获取 Token
|
||||
func (ma *memberAPI) getToken(c *gin.Context) {
|
||||
u := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
|
||||
singleton.ApiLock.RLock()
|
||||
defer singleton.ApiLock.RUnlock()
|
||||
|
||||
tokenList := singleton.UserIDToApiTokenList[u.ID]
|
||||
res := make([]*apiResult, len(tokenList))
|
||||
for i, token := range tokenList {
|
||||
res[i] = &apiResult{
|
||||
Token: token,
|
||||
Note: singleton.ApiTokenList[token].Note,
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"result": res,
|
||||
})
|
||||
}
|
||||
|
||||
type TokenForm struct {
|
||||
Note string
|
||||
}
|
||||
|
||||
// issueNewToken 生成新的 token
|
||||
func (ma *memberAPI) issueNewToken(c *gin.Context) {
|
||||
u := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
|
||||
tf := &TokenForm{}
|
||||
err := c.ShouldBindJSON(tf)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: fmt.Sprintf("请求错误:%s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
secureToken, err := utils.GenerateRandomString(32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: fmt.Sprintf("请求错误:%s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
token := &model.ApiToken{
|
||||
UserID: u.ID,
|
||||
Token: secureToken,
|
||||
Note: tf.Note,
|
||||
}
|
||||
singleton.DB.Create(token)
|
||||
|
||||
singleton.ApiLock.Lock()
|
||||
singleton.ApiTokenList[token.Token] = token
|
||||
singleton.UserIDToApiTokenList[u.ID] = append(singleton.UserIDToApiTokenList[u.ID], token.Token)
|
||||
singleton.ApiLock.Unlock()
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "success",
|
||||
Result: map[string]string{
|
||||
"token": token.Token,
|
||||
"note": token.Note,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// deleteToken 删除 token
|
||||
func (ma *memberAPI) deleteToken(c *gin.Context) {
|
||||
token := c.Param("token")
|
||||
if token == "" {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "token 不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
singleton.ApiLock.Lock()
|
||||
defer singleton.ApiLock.Unlock()
|
||||
if _, ok := singleton.ApiTokenList[token]; !ok {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "token 不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 在数据库中删除该Token
|
||||
singleton.DB.Unscoped().Delete(&model.ApiToken{}, "token = ?", token)
|
||||
|
||||
// 在UserIDToApiTokenList中删除该Token
|
||||
for i, t := range singleton.UserIDToApiTokenList[singleton.ApiTokenList[token].UserID] {
|
||||
if t == token {
|
||||
singleton.UserIDToApiTokenList[singleton.ApiTokenList[token].UserID] = append(singleton.UserIDToApiTokenList[singleton.ApiTokenList[token].UserID][:i], singleton.UserIDToApiTokenList[singleton.ApiTokenList[token].UserID][i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(singleton.UserIDToApiTokenList[singleton.ApiTokenList[token].UserID]) == 0 {
|
||||
delete(singleton.UserIDToApiTokenList, singleton.ApiTokenList[token].UserID)
|
||||
}
|
||||
// 在ApiTokenList中删除该Token
|
||||
delete(singleton.ApiTokenList, token)
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
Message: "success",
|
||||
})
|
||||
}
|
||||
|
||||
func (ma *memberAPI) delete(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user