126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/config/customized"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/pkg/utils"
|
|
"github.com/spf13/viper"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
// The valid auth strategies and values for cookie handling
|
|
const (
|
|
// These constants are used for external services auth (Prometheus ...) ;
|
|
AuthTypeBasic = "basic"
|
|
AuthTypeBearer = "bearer"
|
|
AuthTypeNone = "none"
|
|
)
|
|
|
|
// Conf 配置项主结构体
|
|
// mapstructure(配置文件属性名称无法与类属性名称直接对应)
|
|
type Conf struct {
|
|
AppConfig `yaml:"app" mapstructure:"app"`
|
|
Server ServerConfig `yaml:"server" mapstructure:"server"`
|
|
WebEmbed bool `yaml:"web_embed" mapstructure:"web_embed"`
|
|
DatabaseConfig DatabaseConfig `yaml:"database" mapstructure:"database"`
|
|
Redis RedisConfig `yaml:"redis" mapstructure:"redis"`
|
|
Logger LoggerConfig `yaml:"logger" mapstructure:"logger"`
|
|
Minio MinioConfig `yaml:"minio" mapstructure:"minio"`
|
|
Nats NatsConfig `yaml:"nats" mapstructure:"nats"`
|
|
Prometheus PrometheusConfig `yaml:"prometheus" mapstructure:"prometheus"`
|
|
XxlJob XxlJobConfig `yaml:"xxljob" mapstructure:"xxljob"`
|
|
SchemeService SchemeServiceConfig `yaml:"scheme_service" mapstructure:"scheme_service"`
|
|
Auth AuthConfig `yaml:"auth" mapstructure:"auth"`
|
|
CfnConfig customized.CfnConfig `yaml:"cfn_config" mapstructure:"cfn_config"`
|
|
}
|
|
|
|
var Config = &Conf{
|
|
AppConfig: App,
|
|
Server: Server,
|
|
DatabaseConfig: Database,
|
|
Redis: Redis,
|
|
Logger: Logger,
|
|
Minio: Minio,
|
|
Nats: Nats,
|
|
Prometheus: Prometheus,
|
|
XxlJob: XxlJob,
|
|
SchemeService: SchemeService,
|
|
Auth: Auth,
|
|
CfnConfig: customized.Cfn,
|
|
}
|
|
|
|
var once sync.Once
|
|
|
|
func InitConfig(configPath string) {
|
|
once.Do(func() {
|
|
// 加载 .yaml 配置
|
|
loadYaml(configPath)
|
|
})
|
|
}
|
|
|
|
// todo 环境变量注入配置 or 与nacos集成
|
|
func loadYaml(configPath string) {
|
|
var yamlConfig string
|
|
if configPath == "" {
|
|
yamlConfig = filepath.Join(utils.GetRunPath(), "config/config.yaml")
|
|
} else {
|
|
yamlConfig = filepath.Join(configPath)
|
|
}
|
|
|
|
viper.SetConfigFile(yamlConfig)
|
|
viper.SetConfigType("yaml")
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
panic("Failed to read configuration file:" + err.Error())
|
|
}
|
|
for _, key := range viper.AllKeys() {
|
|
value := viper.GetString(key)
|
|
realValue := expandValueEnv(value)
|
|
if value != realValue {
|
|
viper.Set(key, realValue)
|
|
}
|
|
}
|
|
|
|
err = viper.Unmarshal(Config)
|
|
if err != nil {
|
|
panic("Failed to load configuration:" + err.Error())
|
|
}
|
|
|
|
}
|
|
|
|
func expandValueEnv(value string) (realValue string) {
|
|
realValue = value
|
|
|
|
vLen := len(value)
|
|
// 3 = ${}
|
|
if vLen < 3 {
|
|
return
|
|
}
|
|
// Need start with "${" and end with "}", then return.
|
|
if value[0] != '$' || value[1] != '{' || value[vLen-1] != '}' {
|
|
return
|
|
}
|
|
|
|
key := ""
|
|
defaultV := ""
|
|
// value start with "${"
|
|
for i := 2; i < vLen; i++ {
|
|
if value[i] == '|' && (i+1 < vLen && value[i+1] == '|') {
|
|
key = value[2:i]
|
|
defaultV = value[i+2 : vLen-1] // other string is default value.
|
|
break
|
|
} else if value[i] == '}' {
|
|
key = value[2:i]
|
|
break
|
|
}
|
|
}
|
|
|
|
realValue = os.Getenv(key)
|
|
if realValue == "" {
|
|
realValue = defaultV
|
|
}
|
|
|
|
return
|
|
}
|