diff --git a/config/config.go b/config/config.go index 5c9bcf9..4c67eac 100644 --- a/config/config.go +++ b/config/config.go @@ -227,7 +227,9 @@ func LoadConf(confPath string) (ConfYaml, error) { return conf, err } - viper.ReadConfig(bytes.NewBuffer(content)) + if err := viper.ReadConfig(bytes.NewBuffer(content)); err != nil { + return conf, err + } } else { // Search config in home directory with name ".gorush" (without extension). viper.AddConfigPath("/etc/gorush/") @@ -240,7 +242,9 @@ func LoadConf(confPath string) (ConfYaml, error) { fmt.Println("Using config file:", viper.ConfigFileUsed()) } else { // load default config - viper.ReadConfig(bytes.NewBuffer(defaultConf)) + if err := viper.ReadConfig(bytes.NewBuffer(defaultConf)); err != nil { + return conf, err + } } } diff --git a/config/config_test.go b/config/config_test.go index b8a84bf..cd019b6 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -29,7 +29,7 @@ func (suite *ConfigTestSuite) SetupTest() { if err != nil { panic("failed to load default config.yml") } - suite.ConfGorush, err = LoadConf("config.yml") + suite.ConfGorush, err = LoadConf("testdata/config.yml") if err != nil { panic("failed to load config.yml from file") } @@ -75,7 +75,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() { // iOS assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Enabled) - assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Ios.KeyPath) + assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.KeyPath) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.KeyBase64) assert.Equal(suite.T(), "pem", suite.ConfGorushDefault.Ios.KeyType) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.Password) @@ -191,7 +191,7 @@ func TestLoadConfigFromEnv(t *testing.T) { os.Setenv("GORUSH_IOS_KEY_ID", "ABC123DEFG") os.Setenv("GORUSH_IOS_TEAM_ID", "DEF123GHIJ") os.Setenv("GORUSH_API_HEALTH_URI", "/healthz") - ConfGorush, err := LoadConf("config.yml") + ConfGorush, err := LoadConf("testdata/config.yml") if err != nil { panic("failed to load config.yml from file") } @@ -202,3 +202,14 @@ func TestLoadConfigFromEnv(t *testing.T) { assert.Equal(t, "DEF123GHIJ", ConfGorush.Ios.TeamID) assert.Equal(t, "/healthz", ConfGorush.API.HealthURI) } + +func TestLoadWrongYAMLConfig(t *testing.T) { + _, err := LoadConf("testdata/wrong.yml") + assert.Error(t, err) +} + +func TestLoadWrongDefaultYAMLConfig(t *testing.T) { + defaultConf = []byte(`a`) + _, err := LoadConf("") + assert.Error(t, err) +} diff --git a/config/config.yml b/config/testdata/config.yml similarity index 100% rename from config/config.yml rename to config/testdata/config.yml diff --git a/config/testdata/wrong.yml b/config/testdata/wrong.yml new file mode 100644 index 0000000..32e57a2 --- /dev/null +++ b/config/testdata/wrong.yml @@ -0,0 +1,76 @@ +# wrong format +#core: + core: + enabled: true # enabale httpd server + address: "" # ip address to bind (default: any) + port: "8088" # ignore this port number if auto_tls is enabled (listen 443). + worker_num: 0 # default worker number is runtime.NumCPU() + queue_num: 0 # default queue number is 8192 + max_notification: 100 + sync: false # set true if you need get error message from fail push notification in API response. + mode: "release" + ssl: false + cert_path: "cert.pem" + key_path: "key.pem" + cert_base64: "" + key_base64: "" + http_proxy: "" # only working for FCM server + pid: + enabled: false + path: "gorush.pid" + override: true + auto_tls: + enabled: false # Automatically install TLS certificates from Let's Encrypt. + folder: ".cache" # folder for storing TLS certificates + host: "" # which domains the Let's Encrypt will attempt + +grpc: + enabled: false # enabale gRPC server + port: 9000 + +api: + push_uri: "/api/push" + stat_go_uri: "/api/stat/go" + stat_app_uri: "/api/stat/app" + config_uri: "/api/config" + sys_stat_uri: "/sys/stats" + metric_uri: "/metrics" + health_uri: "/healthz" + +android: + enabled: true + apikey: "YOUR_API_KEY" + max_retry: 0 # resend fail notification, default value zero is disabled + +ios: + enabled: false + key_path: "key.pem" + key_base64: "" # load iOS key from base64 input + key_type: "pem" # could be pem, p12 or p8 type + password: "" # certificate password, default as empty string. + production: false + max_retry: 0 # resend fail notification, default value zero is disabled + key_id: "" # KeyID from developer account (Certificates, Identifiers & Profiles -> Keys) + team_id: "" # TeamID from developer account (View Account -> Membership) + +log: + format: "string" # string or json + access_log: "stdout" # stdout: output to console, or define log path like "log/access_log" + access_level: "debug" + error_log: "stderr" # stderr: output to console, or define log path like "log/error_log" + error_level: "error" + hide_token: true + +stat: + engine: "memory" # support memory, redis, boltdb, buntdb or leveldb + redis: + addr: "localhost:6379" + password: "" + db: 0 + boltdb: + path: "bolt.db" + bucket: "gorush" + buntdb: + path: "bunt.db" + leveldb: + path: "level.db"