LoadConfig 加载配置
()
| 20 | |
| 21 | // LoadConfig 加载配置 |
| 22 | func LoadConfig() *Configuration { |
| 23 | once.Do(func() { |
| 24 | // 从文件中读取 |
| 25 | config = &Configuration{} |
| 26 | f, err := os.Open("config.json") |
| 27 | if err != nil { |
| 28 | log.Fatalf("open config err: %v", err) |
| 29 | return |
| 30 | } |
| 31 | defer f.Close() |
| 32 | encoder := json.NewDecoder(f) |
| 33 | err = encoder.Decode(config) |
| 34 | if err != nil { |
| 35 | log.Fatalf("decode config err: %v", err) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | // 如果环境变量有配置,读取环境变量 |
| 40 | ApiKey := os.Getenv("ApiKey") |
| 41 | AutoPass := os.Getenv("AutoPass") |
| 42 | if ApiKey != "" { |
| 43 | config.ApiKey = ApiKey |
| 44 | } |
| 45 | if AutoPass == "true" { |
| 46 | config.AutoPass = true |
| 47 | } |
| 48 | }) |
| 49 | return config |
| 50 | } |
no outgoing calls
no test coverage detected