update:Save default app info to config file when change was made in the UI

This commit is contained in:
hoangvv 2024-11-25 10:32:15 +07:00
parent b2ddd6f395
commit 941c4765f8
5 changed files with 66 additions and 4 deletions

1
.gitignore vendored
View File

@ -44,3 +44,4 @@ dist/casaos-user-service-amd64_linux_amd64_v1/build/sysroot/usr/bin/casaos-user-
.env
.vscode/launch.json
dist/metadata.json
*.gz

View File

@ -11,8 +11,7 @@ UserDataPath = /var/lib/casaos
[oidc]
AuthServer = https://account.nextzenvn.com
ClientID = WzN5QB9e0LfCSAYTB542RLpIGKcAWNNZgVbeTLaz
ClientSecret = D1mbEz1VHkPnhvMGPfj5aAmjOuZ1ZIYGm7qAReMCivdXwiQ60BJoa4cpdX5m9Z5aKgtR8d56xgmYAy7TR86MEV6zJXfjxy2lf0TTAPXc8ftEcst8fP
i6B9IFe3aDBo8x
ClientSecret = D1mbEz1VHkPnhvMGPfj5aAmjOuZ1ZIYGm7qAReMCivdXwiQ60BJoa4cpdX5m9Z5aKgtR8d56xgmYAy7TR86MEV6zJXfjxy2lf0TTAPXc8ftEcst8fPi6B9IFe3aDBo8x
AuthURL = https://account.nextzenvn.com/application/o/nextzenos/
CallbackURL = https://home.nextzenvn.com/v1/users/oidc/callback

View File

@ -0,0 +1,34 @@
package config
import (
"fmt"
"github.com/KaySar12/NextZen-UserService/pkg/config"
"gopkg.in/ini.v1"
)
var (
Cfg *ini.File
)
func init() {
var err error
Cfg, err = ini.Load(config.UserServiceConfigFilePath)
if err != nil {
panic(err)
}
}
func WriteMapToConfig(configData map[string]string, section string) {
sectionData := Cfg.Section(section)
for key, value := range sectionData.KeysHash() {
if value != configData[key] {
sectionData.Key(key).SetValue(configData[key])
}
}
err := Cfg.SaveTo(config.UserServiceConfigFilePath)
if err != nil {
fmt.Println("Failed to save file:", err)
return
}
}

View File

@ -31,6 +31,7 @@ import (
"github.com/KaySar12/NextZen-UserService/model"
"github.com/KaySar12/NextZen-UserService/model/system_model"
"github.com/KaySar12/NextZen-UserService/pkg/config"
cfg "github.com/KaySar12/NextZen-UserService/pkg/utils/config"
"github.com/KaySar12/NextZen-UserService/pkg/utils/encryption"
"github.com/KaySar12/NextZen-UserService/pkg/utils/file"
"github.com/KaySar12/NextZen-UserService/service"
@ -1626,8 +1627,27 @@ func PostUserCustomConf(c *gin.Context) {
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
return
}
data, _ := io.ReadAll(c.Request.Body)
data, err := io.ReadAll(c.Request.Body)
if err != nil {
logger.Error("Failed to read request body", zap.Error(err))
c.JSON(common_err.SERVICE_ERROR, model.Result{
Success: common_err.SERVICE_ERROR,
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
})
return
}
if name == "default-app" {
var appInfos []model.AppInfo
err := json.Unmarshal([]byte(data), &appInfos)
if err != nil {
c.JSON(common_err.SERVICE_ERROR,
model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
return
}
go func() {
configData := formatAppInfosToMap(appInfos)
cfg.WriteMapToConfig(configData, name)
}()
filePath := config.AppInfo.UserDataPath + "/default"
if err := file.IsNotExistMkDir(filePath); err != nil {
c.JSON(common_err.SERVICE_ERROR,
@ -1664,11 +1684,19 @@ func PostUserCustomConf(c *gin.Context) {
if response.StatusCode() != http.StatusOK {
logger.Error("failed to publish event to message bus", zap.String("status", response.Status()), zap.Any("response", response))
}
}
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json2.RawMessage(string(data))})
}
func formatAppInfosToMap(appInfos []model.AppInfo) map[string]string {
formattedMap := make(map[string]string)
for _, appInfo := range appInfos {
formattedString := fmt.Sprintf("%s-%s-%s-%s-%s",
appInfo.Name, appInfo.Hostname, appInfo.Icon, appInfo.AppType, appInfo.Status)
formattedMap[appInfo.Name] = formattedString
}
return formattedMap
}
/**
* @description: delete user custom config