|
|
@@ -0,0 +1,71 @@
|
|
|
+package config
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "git.sxidc.com/go-framework/baize/application"
|
|
|
+ "git.sxidc.com/go-tools/utils/strutils"
|
|
|
+ "git.sxidc.com/service-supports/fslog"
|
|
|
+ "git.sxidc.com/service-supports/scm-sdk"
|
|
|
+ "gopkg.in/yaml.v3"
|
|
|
+ "os"
|
|
|
+)
|
|
|
+
|
|
|
+type Config struct {
|
|
|
+ ApplicationConfig application.Config
|
|
|
+}
|
|
|
+
|
|
|
+var conf Config
|
|
|
+
|
|
|
+func init() {
|
|
|
+ useSCMEnv := os.Getenv("USE_SCM")
|
|
|
+ if useSCMEnv == "true" {
|
|
|
+ initConfigFromSCM()
|
|
|
+ } else {
|
|
|
+ initConfigFromConfigFile()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func GetConfig() Config {
|
|
|
+ return conf
|
|
|
+}
|
|
|
+
|
|
|
+func initConfigFromSCM() {
|
|
|
+ scmBaseUrl := loadEnvOrPanic("SCM_BASE_URL")
|
|
|
+ scmNamespace := loadEnvOrPanic("SCM_NAMESPACE")
|
|
|
+ scmName := loadEnvOrPanic("SCM_NAME")
|
|
|
+
|
|
|
+ info, err := scm_sdk.GetCurrentConfiguration(scmBaseUrl, scmNamespace, scmName)
|
|
|
+ if err != nil {
|
|
|
+ fslog.Error(err)
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = yaml.Unmarshal([]byte(info.Content), conf)
|
|
|
+ if err != nil {
|
|
|
+ fslog.Error(err)
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ fslog.Info("Load config from scm finish")
|
|
|
+}
|
|
|
+
|
|
|
+func initConfigFromConfigFile() {
|
|
|
+ configFilePath := os.Getenv("CONFIG_FILE_PATH")
|
|
|
+ applicationConfig, err := application.LoadFromYamlFile(configFilePath)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ conf.ApplicationConfig = applicationConfig
|
|
|
+
|
|
|
+ fmt.Println("Load config file finish")
|
|
|
+}
|
|
|
+
|
|
|
+func loadEnvOrPanic(envName string) string {
|
|
|
+ envValue := os.Getenv(envName)
|
|
|
+ if strutils.IsStringEmpty(envValue) {
|
|
|
+ panic("请配置" + envName)
|
|
|
+ }
|
|
|
+
|
|
|
+ return envValue
|
|
|
+}
|