61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package k8s_manager
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
)
|
|
|
|
type Config struct {
|
|
JwtKey string
|
|
AseKey string
|
|
DBHost string
|
|
DBUser string
|
|
DBPassword string
|
|
NFSHost string
|
|
NFSPath string
|
|
}
|
|
|
|
var config *Config
|
|
|
|
func getField(v *Config, field string) string {
|
|
r := reflect.ValueOf(v)
|
|
f := reflect.Indirect(r).FieldByName(field)
|
|
return f.String()
|
|
}
|
|
|
|
func setField(v *Config, field string, value string) {
|
|
r := reflect.ValueOf(v)
|
|
f := reflect.Indirect(r).FieldByName(field)
|
|
f.SetString(value)
|
|
}
|
|
|
|
func InitConfig() {
|
|
config = &Config{}
|
|
config.JwtKey = os.Getenv("JWT_KEY")
|
|
config.AseKey = os.Getenv("ASE_KEY")
|
|
config.DBHost = os.Getenv("DB_HOST")
|
|
config.DBUser = os.Getenv("DB_USER")
|
|
config.DBPassword = os.Getenv("DB_PASS")
|
|
config.NFSHost = os.Getenv("NFS_HOST")
|
|
config.NFSPath = os.Getenv("NFS_PATH")
|
|
|
|
mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword", "NFSHost", "NFSPath"}
|
|
optionalFields := map[string]string{}
|
|
for _, f := range mustFields {
|
|
if getField(config, f) == "" {
|
|
panic(fmt.Sprintf("field %s must be set.", f))
|
|
}
|
|
}
|
|
for f, v := range optionalFields {
|
|
if getField(config, f) == "" {
|
|
setField(config, f, v)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func GetConfig() *Config {
|
|
return config
|
|
}
|