-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f417073
commit 88ff51d
Showing
4 changed files
with
48 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,98 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/caarlos0/env" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/knadh/koanf/parsers/json" | ||
"github.com/knadh/koanf/providers/env" | ||
koanfenv "github.com/knadh/koanf/providers/env" | ||
"github.com/knadh/koanf/providers/file" | ||
"github.com/knadh/koanf/v2" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Config struct { | ||
PostgresqlHosts string `koanf:"postgresql_hosts"` | ||
PostgresqlDbName string `koanf:"postgresql_db_name"` | ||
PostgresqlUsername string `koanf:"postgresql_username"` | ||
PostgresqlPassword string `koanf:"postgresql_password"` | ||
PostgresqlSslMode string `koanf:"postgresql_ssl_mode"` | ||
PostgresqlPort string `koanf:"postgresql_port"` | ||
RedisHosts string `koanf:"redis_hosts"` | ||
RedisPort string `koanf:"redis_port"` | ||
RedisUsername string `koanf:"redis_username"` | ||
RedisPassword string `koanf:"redis_password"` | ||
RedisReadTimeout time.Duration `koanf:"redis_read_time_out"` | ||
RedisWriteTimeout time.Duration `koanf:"redis_write_time_out"` | ||
PostgresqlReadTimeout time.Duration `koanf:"postgresql_read_time_out"` | ||
PostgresqlWriteTimeout time.Duration `koanf:"postgresql_write_time_out"` | ||
InMemoryDbUpdateInterval time.Duration `koanf:"in_memory_db_update_interval"` | ||
OpenAiKey string `koanf:"openai_key"` | ||
StatsProvider string `koanf:"stats_provider"` | ||
AdminPass string `koanf:"admin_pass"` | ||
ProxyTimeout time.Duration `koanf:"proxy_timeout"` | ||
NumberOfEventMessageConsumers int `koanf:"number_of_event_message_consumers"` | ||
OpenAiApiKey string `koanf:"openai_api_key"` | ||
CustomPolicyDetectionTimeout time.Duration `koanf:"custom_policy_detection_timeout"` | ||
AmazonRegion string `koanf:"amazon_region"` | ||
AmazonRequestTimeout time.Duration `koanf:"amazon_request_timeout"` | ||
AmazonConnectionTimeout time.Duration `koanf:"amazon_connection_timeout"` | ||
PostgresqlHosts string `koanf:"postgresql_hosts" env:"POSTGRESQL_HOSTS" envSeparator:":" envDefault:"localhost"` | ||
PostgresqlDbName string `koanf:"postgresql_db_name" env:"POSTGRESQL_DB_NAME"` | ||
PostgresqlUsername string `koanf:"postgresql_username" env:"POSTGRESQL_USERNAME"` | ||
PostgresqlPassword string `koanf:"postgresql_password" env:"POSTGRESQL_PASSWORD"` | ||
PostgresqlSslMode string `koanf:"postgresql_ssl_mode" env:"POSTGRESQL_SSL_MODE" envDefault:"disable"` | ||
PostgresqlPort string `koanf:"postgresql_port" env:"POSTGRESQL_PORT" envDefault:"5432"` | ||
RedisHosts string `koanf:"redis_hosts" env:"REDIS_HOSTS" envSeparator:":" envDefault:"localhost"` | ||
RedisPort string `koanf:"redis_port" env:"REDIS_PORT" envDefault:"6379"` | ||
RedisUsername string `koanf:"redis_username" env:"REDIS_USERNAME"` | ||
RedisPassword string `koanf:"redis_password" env:"REDIS_PASSWORD"` | ||
RedisReadTimeout time.Duration `koanf:"redis_read_time_out" env:"REDIS_READ_TIME_OUT" envDefault:"1s"` | ||
RedisWriteTimeout time.Duration `koanf:"redis_write_time_out" env:"REDIS_WRITE_TIME_OUT" envDefault:"500ms"` | ||
PostgresqlReadTimeout time.Duration `koanf:"postgresql_read_time_out" env:"POSTGRESQL_READ_TIME_OUT" envDefault:"2s"` | ||
PostgresqlWriteTimeout time.Duration `koanf:"postgresql_write_time_out" env:"POSTGRESQL_WRITE_TIME_OUT" envDefault:"1s"` | ||
InMemoryDbUpdateInterval time.Duration `koanf:"in_memory_db_update_interval" env:"IN_MEMORY_DB_UPDATE_INTERVAL" envDefault:"5s"` | ||
StatsProvider string `koanf:"stats_provider" env:"STATS_PROVIDER"` | ||
AdminPass string `koanf:"admin_pass" env:"ADMIN_PASS"` | ||
ProxyTimeout time.Duration `koanf:"proxy_timeout" env:"PROXY_TIMEOUT"` | ||
NumberOfEventMessageConsumers int `koanf:"number_of_event_message_consumers" env:"NUMBER_OF_EVENT_MESSAGE_CONSUMERS" envDefault:"3"` | ||
OpenAiApiKey string `koanf:"openai_api_key" env:"OPENAI_API_KEY"` | ||
CustomPolicyDetectionTimeout time.Duration `koanf:"custom_policy_detection_timeout" env:"CUSTOM_POLICY_DETECTION_TIMEOUT"` | ||
AmazonRegion string `koanf:"amazon_region" env:"AMAZON_REGION"` | ||
AmazonRequestTimeout time.Duration `koanf:"amazon_request_timeout" env:"AMAZON_REQUEST_TIMEOUT"` | ||
AmazonConnectionTimeout time.Duration `koanf:"amazon_connection_timeout" env:"AMAZON_CONNECTION_TIMEOUT"` | ||
} | ||
|
||
func prepareDotEnv(envFilePath string) error { | ||
if envFilePath == "" { | ||
err := godotenv.Load(envFilePath) | ||
if err != nil { | ||
ex, err := os.Executable() | ||
if err != nil { | ||
panic(err) | ||
return err | ||
} | ||
|
||
exPath := filepath.Dir(ex) | ||
|
||
// first check .env, then .env_{DEV|TEST|UAT|PROD} | ||
envFile := exPath + "/.env" | ||
envFilePath = envFile | ||
|
||
err = godotenv.Load(envFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err := godotenv.Load(envFilePath) | ||
return err | ||
return nil | ||
} | ||
|
||
var k = koanf.New(".") | ||
|
||
func LoadConfig() (*Config, error) { | ||
func LoadConfig(log *zap.Logger) (*Config, error) { | ||
err := prepareDotEnv(".env") | ||
if err != nil { | ||
panic(err) | ||
log.Sugar().Infof("error loading config from .env file: %v", err) | ||
} | ||
|
||
cfgPath := os.Getenv("CONFIG_FILE_NAME") | ||
if cfgPath != "" { | ||
f := file.Provider(cfgPath) | ||
if err := k.Load(f, json.Parser()); err != nil { | ||
log.Fatalf("error loading config from json file: %v", err) | ||
log.Sugar().Infof("error loading config from json file: %v", err) | ||
} | ||
} | ||
|
||
k.Load(env.Provider("", ".", func(s string) string { | ||
k.Load(koanfenv.Provider("", ".", func(s string) string { | ||
return strings.ToLower(s) | ||
}), nil) | ||
|
||
cfg := &Config{} | ||
k.Unmarshal("", cfg) | ||
|
||
err = env.Parse(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cfg, nil | ||
} |