package config import ( "fmt" "github.com/spf13/viper" ) var ( ecpmView int ecpmValue int appId string secret string ) func init() { viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath("./") // call multiple times to add many search paths viper.AddConfigPath(".") // optionally look for config in the working directory err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file panic(fmt.Errorf("fatal error config file: %w", err)) } } func GetEcpmView() int { if ecpmView == 0 { ecpmView = viper.GetInt("ecpm.view") } return ecpmView } func GetEcpmValue() int { if ecpmValue == 0 { ecpmValue = viper.GetInt("ecpm.value") } return ecpmValue } func GetAppId() string { if appId == "" { appId = viper.GetString("douyin.app_id") } return appId } func GetSecret() string { if secret == "" { secret = viper.GetString("douyin.secret") } return secret } func GetPort() int { return viper.GetInt("port") }