Skip to content

Commit

Permalink
fix env variables config issue
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelu2016 committed May 30, 2024
1 parent f417073 commit 88ff51d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
1 change: 0 additions & 1 deletion cmd/bricksllm/.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ REDIS_WRITE_TIME_OUT=500ms
POSTGRESQL_READ_TIME_OUT=2m
POSTGRESQL_WRITE_TIME_OUT=5s
IN_MEMORY_DB_UPDATE_INTERVAL=5s
OPENAI_KEY=
STATS_PROVIDER=
ADMIN_PASS=
PROXY_TIMEOUT=600s
Expand Down
1 change: 0 additions & 1 deletion cmd/bricksllm/config_local.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"postgresql_read_time_out": "2m",
"postgresql_write_time_out": "5s",
"in_memory_db_update_interval": "5s",
"openai_key": "",
"stats_provider": "",
"admin_pass": "",
"proxy_timeout": "600s",
Expand Down
2 changes: 1 addition & 1 deletion cmd/bricksllm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {

gin.SetMode(gin.ReleaseMode)

cfg, err := config.LoadConfig()
cfg, err := config.LoadConfig(log)
if err != nil {
log.Sugar().Fatalf("cannot parse environment variables: %v", err)
}
Expand Down
82 changes: 47 additions & 35 deletions internal/config/config.go
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
}

0 comments on commit 88ff51d

Please sign in to comment.