mc-client-updater-server/internal/api/v1/handler/admin.go

80 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import (
"github.com/gin-gonic/gin"
"mc-client-updater-server/internal/service"
"mc-client-updater-server/pkg/param"
"mc-client-updater-server/pkg/result"
"mc-client-updater-server/pkg/util"
"time"
)
func HandleLogin(c *gin.Context) {
res := result.NewResult(c)
loginParam := param.LoginParam{}
err := c.ShouldBindJSON(&loginParam)
if err != nil {
res.BadRequest()
return
}
srv := service.NewUserService(c)
srv.Login(loginParam.Username, loginParam.Password)
}
func HandleGrantAdd(c *gin.Context) {
res := result.NewResult(c)
p := param.NewGrantTokenParam{}
err := c.ShouldBindJSON(&p)
if err != nil {
res.BadRequest()
return
}
if p.ExpireAt != "" {
isValid := util.IsSQLTimeFormat(p.ExpireAt)
if !isValid {
res.BadRequestWithMsg("请求参数错误时间格式错误应为2006-01-02 15:04:05格式")
return
}
} else {
if p.TTL == 0 {
res.BadRequestWithMsg("请求参数错误至少提供expire_at和ttl中的一项且不为0")
return
}
p.ExpireAt = util.ToSQLTimeFormat(time.Now().Add(time.Duration(p.TTL) * time.Second))
}
srv := service.NewInstanceService(c)
// 验证 target -> instance(name) 是否存在
_, err = srv.GetInstanceByName(p.Target)
if err != nil {
res.InvalidInstance(p.Target)
return
}
grantEntity, err := srv.NewGrantToken(p.Target, p.ExpireAt)
if err != nil {
res.InternalServerError("生成授权码失败")
return
}
res.Success(grantEntity)
}
func HandleNewInstance(c *gin.Context) {
res := result.NewResult(c)
p := param.AddInstanceParam{}
err := c.ShouldBindJSON(&p)
if err != nil {
res.BadRequest()
return
}
srv := service.NewInstanceService(c)
inst, err := srv.AddInstance(p.Name, p.UpdateURL)
if err != nil {
res.DuplicatedValue("实例名称已存在")
return
}
res.Success(inst)
}