refactor: code cleanup

This commit is contained in:
Marvin Zhang
2024-10-20 17:17:08 +08:00
parent 9b5ed188a6
commit 52e16badb7
28 changed files with 80 additions and 5849 deletions

View File

@@ -41,6 +41,9 @@ func (c *Config) WatchConfig() {
}
func (c *Config) Init() (err error) {
// Set default values
c.setDefaults()
// config
if c.Name != "" {
viper.SetConfigFile(c.Name) // if config file is set, load it accordingly
@@ -50,13 +53,13 @@ func (c *Config) Init() (err error) {
}
// config type as yaml
viper.SetConfigType("yaml") // default yaml
viper.SetConfigType("yaml")
// auto env
viper.AutomaticEnv() // load matched environment variables
viper.AutomaticEnv()
// env prefix
viper.SetEnvPrefix("CRAWLAB") // environment variable prefix as CRAWLAB
viper.SetEnvPrefix("CRAWLAB")
// replacer
replacer := strings.NewReplacer(".", "_")
@@ -64,13 +67,39 @@ func (c *Config) Init() (err error) {
// read in config
if err := viper.ReadInConfig(); err != nil {
log.Errorf("Error reading config file, %s", err)
return err
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Warn("No config file found. Using default values.")
} else {
log.Errorf("Error reading config file: %s", err)
return err
}
}
return nil
}
func (c *Config) setDefaults() {
viper.SetDefault("edition", "global.edition.community")
viper.SetDefault("mongo.host", "localhost")
viper.SetDefault("mongo.port", 27017)
viper.SetDefault("mongo.db", "crawlab_test")
viper.SetDefault("mongo.username", "")
viper.SetDefault("mongo.password", "")
viper.SetDefault("mongo.authSource", "admin")
viper.SetDefault("server.host", "0.0.0.0")
viper.SetDefault("server.port", 8000)
viper.SetDefault("grpc.address", "localhost:9666")
viper.SetDefault("grpc.server.address", "0.0.0.0:9666")
viper.SetDefault("grpc.authKey", "Crawlab2021!")
viper.SetDefault("api.endpoint", "http://localhost:8000")
viper.SetDefault("log.path", "/var/log/crawlab")
}
func (c *Config) initLogLevel() {
// set log level
logLevel := viper.GetString("log.level")

View File

@@ -19,47 +19,58 @@ func init() {
}
func TestInitConfig(t *testing.T) {
// Create a temporary directory for the test
// Create a new Config instance
c := Config{Name: ""}
// Initialize the config
err := c.Init()
require.NoError(t, err, "Failed to initialize config")
// Test default values
assert.Equal(t, "global.edition.community", viper.GetString("edition"), "Unexpected default value for edition")
assert.Equal(t, "localhost", viper.GetString("mongo.host"), "Unexpected default value for mongo.host")
assert.Equal(t, 27017, viper.GetInt("mongo.port"), "Unexpected default value for mongo.port")
assert.Equal(t, "crawlab_test", viper.GetString("mongo.db"), "Unexpected default value for mongo.db")
assert.Equal(t, "0.0.0.0", viper.GetString("server.host"), "Unexpected default value for server.host")
assert.Equal(t, 8000, viper.GetInt("server.port"), "Unexpected default value for server.port")
assert.Equal(t, "localhost:9666", viper.GetString("grpc.address"), "Unexpected default value for grpc.address")
assert.Equal(t, "Crawlab2021!", viper.GetString("grpc.authKey"), "Unexpected default value for grpc.authKey")
assert.Equal(t, "http://localhost:8000", viper.GetString("api.endpoint"), "Unexpected default value for api.endpoint")
assert.Equal(t, "/var/log/crawlab", viper.GetString("log.path"), "Unexpected default value for log.path")
// Test environment variable override
os.Setenv("CRAWLAB_MONGO_HOST", "mongodb.example.com")
defer os.Unsetenv("CRAWLAB_MONGO_HOST")
assert.Equal(t, "mongodb.example.com", viper.GetString("mongo.host"), "Environment variable should override default value")
// Test with a config file
tempDir, err := os.MkdirTemp("", "crawlab-config-test")
require.NoError(t, err, "Failed to create temp directory")
defer os.RemoveAll(tempDir)
// Create a temporary config file
configContent := []byte(`
log:
level: info
test:
string: default_string_value
int: 0
bool: false
nested:
key: default_nested_value
edition: global.edition.pro
mongo:
host: mongodb.custom.com
port: 27018
server:
port: 8001
`)
configPath := filepath.Join(tempDir, "config.yaml")
err = os.WriteFile(configPath, configContent, 0644)
require.NoError(t, err, "Failed to write config file")
// Set up the test environment
oldConfigPath := viper.ConfigFileUsed()
defer viper.SetConfigFile(oldConfigPath)
viper.SetConfigFile(configPath)
// Create a new Config instance with the config file
cWithFile := Config{Name: configPath}
err = cWithFile.Init()
require.NoError(t, err, "Failed to initialize config with file")
// Create a new Config instance
c := Config{Name: configPath}
// Test values from config file
assert.Equal(t, "global.edition.pro", viper.GetString("edition"), "Unexpected value for edition from config file")
assert.Equal(t, "mongodb.custom.com", viper.GetString("mongo.host"), "Unexpected value for mongo.host from config file")
assert.Equal(t, 27018, viper.GetInt("mongo.port"), "Unexpected value for mongo.port from config file")
assert.Equal(t, 8001, viper.GetInt("server.port"), "Unexpected value for server.port from config file")
// Initialize the config
err = c.Init()
require.NoError(t, err, "Failed to initialize config")
// Test config values
assert.Equal(t, "default_string_value", viper.GetString("test.string"), "Unexpected value for test.string")
assert.Equal(t, 0, viper.GetInt("test.int"), "Unexpected value for test.int")
assert.False(t, viper.GetBool("test.bool"), "Unexpected value for test.bool")
assert.Equal(t, "default_nested_value", viper.GetString("test.nested.key"), "Unexpected value for test.nested.key")
assert.Empty(t, viper.GetString("non.existent.key"), "Non-existent key should return empty string")
// Test environment variable override
os.Setenv("CRAWLAB_TEST_STRING", "env_string_value")
defer os.Unsetenv("CRAWLAB_TEST_STRING")
assert.Equal(t, "env_string_value", viper.GetString("test.string"), "Environment variable should override config value")
// Values not in config file should still use defaults
assert.Equal(t, "Crawlab2021!", viper.GetString("grpc.authKey"), "Unexpected default value for grpc.authKey")
}