-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
35 lines (30 loc) · 883 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"strings"
)
// initConfig 用于初始化配置
func initConfig() {
setConfigDefaults()
// Parse env config
viper.SetEnvPrefix("pixiv_fetcher") // like: PIXIV_FETCHER_PORT=8000
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// Set default viper information
viper.SetConfigName("config")
viper.SetConfigType("toml") // Toml is the best!
// Parse path etc > home > localPath
viper.AddConfigPath("/etc/.pixiv_fetcher")
viper.AddConfigPath("$HOME/.pixiv_fetcher")
viper.AddConfigPath(".")
viper.AddConfigPath("../conf")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("[config] Fatal error while reading config file: %s \n", err)
}
}
// setConfigDefaults 用于设置初始值
func setConfigDefaults() {
viper.SetDefault("server.port", 8000)
}