package config import ( "fmt" "log" "os" "strings" "github.com/KaySar12/NextZen-Common/utils/constants" "github.com/KaySar12/NextZen-UserService/model" "gopkg.in/ini.v1" ) // models with default values var ( CommonInfo = &model.CommonModel{ RuntimePath: constants.DefaultRuntimePath, } AppInfo = &model.APPModel{ DBPath: constants.DefaultDataPath, UserDataPath: constants.DefaultDataPath, LogPath: constants.DefaultLogPath, LogSaveName: "user", LogFileExt: "log", } OIDCInfo = &model.OIDCModel{ AuthServer: "", ClientID: "", ClientSecret: "", AuthURL: "", CallbackURL: "", } NextWebInfo = &model.NextWebModel{ Server: "", UserName: "", Password: "", EntranceCode: "", } DefaultAppInfo = &model.DefaultModel{ Apps: map[string]model.AppInfo{}, } Cfg *ini.File ConfigFilePath string ) func InitSetup(config string, sample string) { ConfigFilePath = UserServiceConfigFilePath if len(config) > 0 { ConfigFilePath = config } // create default config file if not exist if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { fmt.Println("config file not exist, create it") // create config file file, err := os.Create(ConfigFilePath) if err != nil { panic(err) } defer file.Close() // write default config _, err = file.WriteString(sample) if err != nil { panic(err) } } var err error Cfg, err = ini.Load(ConfigFilePath) if err != nil { fmt.Printf("Fail to read file: %v", err) os.Exit(1) } mapTo("oidc", OIDCInfo) mapTo("nextweb", NextWebInfo) mapTo("common", CommonInfo) mapTo("app", AppInfo) mapToDefaultApp("default-app", DefaultAppInfo) fmt.Println(DefaultAppInfo) } func mapToDefaultApp(section string, v interface{}) { sectionData := Cfg.Section(section) dm, ok := v.(*model.DefaultModel) if !ok { log.Fatal("Invalid type passed to mapToDefaultApp") } // Iterate through all keys in the section for key, value := range sectionData.KeysHash() { // Split the value string into components (Name, URL, Icon) parts := strings.Split(value, "-") if len(parts) == 5 { dm.Apps[key] = model.AppInfo{ Name: parts[0], Hostname: parts[1], Icon: parts[2], AppType: parts[3], Status: parts[4], } } else { log.Printf("Invalid app info format for key: %s, value: %s", key, value) } } } func SaveSetup(config string) { reflectFrom("common", CommonInfo) reflectFrom("app", AppInfo) configFilePath := UserServiceConfigFilePath if len(config) > 0 { configFilePath = config } if err := Cfg.SaveTo(configFilePath); err != nil { fmt.Printf("Fail to save file: %v", err) os.Exit(1) } } func mapTo(section string, v interface{}) { err := Cfg.Section(section).MapTo(v) if err != nil { log.Fatalf("Cfg.MapTo %s err: %v", section, err) } } func reflectFrom(section string, v interface{}) { err := Cfg.Section(section).ReflectFrom(v) if err != nil { log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err) } }