diff --git a/prestart/init_config_driver.go b/prestart/init_config_driver.go index b56c138..e7f559d 100644 --- a/prestart/init_config_driver.go +++ b/prestart/init_config_driver.go @@ -2,13 +2,13 @@ package prestart import ( "bytes" + "github.com/cockroachdb/errors" + "github.com/hitokoto-osc/Moe/config" "github.com/hitokoto-osc/Moe/logging" + "github.com/spf13/viper" "go.uber.org/zap" "os" "strings" - - "github.com/hitokoto-osc/Moe/config" - "github.com/spf13/viper" ) // The Config Parse Driver is served by viper @@ -16,14 +16,8 @@ func initConfigDriver() { logger := logging.GetLogger() defer logger.Sync() config.SetDefault() - // Parse env config - viper.SetEnvPrefix("moe") // like: MOE_PORT=8000 - viper.AutomaticEnv() - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - // Set default viper information viper.SetConfigName("config") - viper.SetConfigType("toml") // Toml is the best! if config.File != "" { content, err := os.ReadFile(config.File) if err != nil { @@ -53,8 +47,20 @@ func initConfigDriver() { viper.AddConfigPath("../config") err := viper.ReadInConfig() if err != nil { - logger.Fatal("[init] Fatal error while reading config file.", zap.Error(err)) + var e viper.ConfigFileNotFoundError + if !errors.As(err, &e) { + logger.Fatal("[init] Fatal error while reading config file.", zap.Error(err)) + } + logger.Warn("[init] No config file detected, reading config from env.") } } + // Parse env config + viper.SetEnvPrefix("moe") // like: MOE_PORT=8000 + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + logger.Debug("[init] config is parsed.", + zap.String("config_file_used", viper.ConfigFileUsed()), + zap.Any("settings", viper.AllSettings()), + ) config.Inject() }