diff --git a/backend/main.go b/backend/main.go index c63240e8..1d263afe 100644 --- a/backend/main.go +++ b/backend/main.go @@ -2,9 +2,14 @@ package main import ( "github.com/crawlab-team/crawlab/core/cmd" + "github.com/crawlab-team/crawlab/core/config" "github.com/crawlab-team/crawlab/core/utils" ) +func init() { + config.InitConfig() +} + func main() { go func() { err := cmd.Execute() diff --git a/core/config/config.go b/core/config/config.go index 09001cae..09bfde7a 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -3,45 +3,18 @@ package config import ( "errors" "strings" + "sync" "github.com/apex/log" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) -func init() { - // config instance - c := Config{Name: ""} - - // init config file - if err := c.Init(); err != nil { - log.Warn("unable to init config") - return - } - - // watch config change and load responsively - c.WatchConfig() - - // init log level - c.initLogLevel() -} - type Config struct { Name string } -type InitConfigOptions struct { - Name string -} - -func (c *Config) WatchConfig() { - viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - log.Infof("Config file changed: %s", e.Name) - }) -} - -func (c *Config) Init() (err error) { +func (c *Config) Init() { // Set default values c.setDefaults() @@ -74,7 +47,15 @@ func (c *Config) Init() (err error) { } } - return nil + // init log level + c.initLogLevel() +} + +func (c *Config) WatchConfig() { + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + log.Infof("Config file changed: %s", e.Name) + }) } func (c *Config) setDefaults() { @@ -95,3 +76,26 @@ func (c *Config) initLogLevel() { } log.SetLevel(l) } + +func newConfig() *Config { + return &Config{} +} + +var _config *Config +var _configOnce sync.Once + +func GetConfig() *Config { + _configOnce.Do(func() { + _config = newConfig() + _config.Init() + }) + return _config +} + +func InitConfig() { + // config instance + c := GetConfig() + + // watch config change and load responsively + c.WatchConfig() +} diff --git a/core/config/config_test.go b/core/config/config_test.go index ca4c81e4..039c4977 100644 --- a/core/config/config_test.go +++ b/core/config/config_test.go @@ -20,11 +20,7 @@ func init() { func TestInitConfig(t *testing.T) { // Create a new Config instance - c := Config{Name: ""} - - // Initialize the config - err := c.Init() - require.NoError(t, err, "Failed to initialize config") + InitConfig() // Test default values assert.Equal(t, "localhost", viper.GetString("mongo.host"), "Unexpected default value for mongo.host") @@ -58,8 +54,7 @@ server: // 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") + cWithFile.Init() // Test values from config file assert.Equal(t, "global.edition.pro", viper.GetString("edition"), "Unexpected value for edition from config file") diff --git a/core/task/handler/service.go b/core/task/handler/service.go index 3438794c..f979834f 100644 --- a/core/task/handler/service.go +++ b/core/task/handler/service.go @@ -457,8 +457,6 @@ func newTaskHandlerService() *Service { // grpc client svc.c = grpcclient.GetGrpcClient() - log.Debugf("[NewTaskHandlerService] svc[cfgPath: %s]", svc.cfgSvc.GetConfigPath()) - return svc } diff --git a/core/utils/config.go b/core/utils/config.go index f03bd3e7..2ec04c3f 100644 --- a/core/utils/config.go +++ b/core/utils/config.go @@ -81,10 +81,15 @@ func IsPro() bool { } func GetWorkspace() string { + homedirPath, err := homedir.Dir() + if err != nil { + log.Warnf("cannot find home directory: %v", err) + return DefaultWorkspace + } if res := viper.GetString("workspace"); res != "" { return res } - return DefaultWorkspace + return filepath.Join(homedirPath, DefaultWorkspace) } func GetTaskLogPath() string {