1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package config
- import (
- "fmt"
- "git.sxidc.com/go-framework/baize/framework/core/application"
- "git.sxidc.com/go-tools/utils/strutils"
- "git.sxidc.com/service-supports/fslog"
- "git.sxidc.com/service-supports/scm-sdk"
- "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)
- }
- applicationConfig, err := application.LoadFromYaml(info.Content)
- if err != nil {
- panic(err)
- }
- conf.ApplicationConfig = applicationConfig
- 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
- }
|