fix: # 26add client 可以指定名称

fix: 修改日志记录方式,避免在读取index.html失败时导致程序崩溃
This commit is contained in:
Akizon77
2025-05-12 16:09:47 +08:00
parent cc3cda12cf
commit 8535b4ba7a
3 changed files with 43 additions and 4 deletions

View File

@@ -13,13 +13,23 @@ import (
)
func AddClient(c *gin.Context) {
uuid, token, err := clients.CreateClient()
var req struct {
name string
}
if err := c.ShouldBindJSON(&req); err != nil {
uuid, token, err := clients.CreateClient()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token})
return
}
uuid, token, err := clients.CreateClientWithName(req.name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token})
}

View File

@@ -159,6 +159,35 @@ func CreateClient() (clientUUID, token string, err error) {
return clientUUID, token, nil
}
func CreateClientWithName(name string) (clientUUID, token string, err error) {
if name == "" {
return CreateClient()
}
db := dbcore.GetDBInstance()
token = utils.GenerateToken()
clientUUID = uuid.New().String()
client := models.Client{
UUID: clientUUID,
Token: token,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
err = db.Create(&client).Error
if err != nil {
return "", "", err
}
clientInfo := common.ClientInfo{
UUID: clientUUID,
Name: name,
}
err = db.Create(&clientInfo).Error
if err != nil {
return "", "", err
}
return clientUUID, token, nil
}
/*
// GetAllClients 获取所有客户端配置

View File

@@ -41,7 +41,7 @@ func initIndex() {
}()
index, err := io.ReadAll(indexFile)
if err != nil {
log.Fatalln("Failed to read index.html:", err)
log.Println("Failed to read index.html:", err)
}
RawIndexFile = string(index)
}