chore: use go routine to create default token for new user

This commit is contained in:
JustSong 2024-05-28 01:44:21 +08:00
parent aa4b1145ea
commit 1814f1a3a0

View File

@ -6,6 +6,8 @@ import (
"github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/model" "github.com/songquanpeng/one-api/model"
"net/http" "net/http"
@ -109,6 +111,7 @@ func Logout(c *gin.Context) {
} }
func Register(c *gin.Context) { func Register(c *gin.Context) {
ctx := c.Request.Context()
if !config.RegisterEnabled { if !config.RegisterEnabled {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了新用户注册", "message": "管理员关闭了新用户注册",
@ -173,18 +176,28 @@ func Register(c *gin.Context) {
}) })
return return
} }
user.ValidateAndFill() go func() {
cleanToken := model.Token{ err := user.ValidateAndFill()
UserId: user.Id, if err != nil {
Name: "default", logger.Errorf(ctx, "user.ValidateAndFill failed: %w", err)
Key: random.GenerateKey(), return
CreatedTime: 0, }
AccessedTime: 0, cleanToken := model.Token{
ExpiredTime: -1, UserId: user.Id,
RemainQuota: -1, Name: "default",
UnlimitedQuota: true, Key: random.GenerateKey(),
} CreatedTime: helper.GetTimestamp(),
cleanToken.Insert() AccessedTime: helper.GetTimestamp(),
ExpiredTime: -1,
RemainQuota: -1,
UnlimitedQuota: true,
}
err = cleanToken.Insert()
if err != nil {
logger.Errorf(ctx, "cleanToken.Insert failed: %w", err)
return
}
}()
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "", "message": "",