mirror of
https://github.com/komari-monitor/komari.git
synced 2025-11-02 21:13:27 +00:00
fix: # 26add client 可以指定名称
fix: 修改日志记录方式,避免在读取index.html失败时导致程序崩溃
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
@@ -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 获取所有客户端配置
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user