-
Notifications
You must be signed in to change notification settings - Fork 44
feat(service): add a config package to load application configuration using Viper #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 11 commits
eb6a01b
4f2f17f
bd4716f
74f40b9
ce7e9da
1b52e61
11100ec
07ae542
f876bad
c435d17
4182e13
fdb494e
0ea8db3
80e754e
f3e7e75
8ad2a6b
d876833
5e79330
51eae0a
051b9ef
6161ada
46e1260
8e521b2
1368b7e
20c1ebd
284445a
f18c11d
8685410
5b99095
35cbf9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/Zomato/espresso/service/model" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func Load() (model.Config, error) { | ||
| viper.AutomaticEnv() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to service/internal pkg
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
|
|
||
| viper.SetDefault(CONFIG_FILE_NAME, "espressoconfig") | ||
| viper.SetDefault(CONFIG_FILE_TYPE, "yaml") | ||
| viper.SetDefault(CONFIG_FILE_PATH, "/app/espresso/configs") | ||
|
|
||
| viper.SetConfigName(viper.GetString(CONFIG_FILE_NAME)) // File name without extension | ||
| viper.SetConfigType(viper.GetString(CONFIG_FILE_TYPE)) // File type | ||
|
|
||
| // Search paths relative to where the binary runs in container | ||
| viper.AddConfigPath(CONFIG_FILE_PATH) // Main config path in container | ||
| viper.AddConfigPath("../../configs") // For local development | ||
| viper.AddConfigPath("./configs") // Fallback path for local development | ||
|
|
||
| err := viper.ReadInConfig() | ||
| if err != nil { | ||
| return model.Config{}, err | ||
| } | ||
|
|
||
| var config model.Config | ||
| err = viper.Unmarshal(&config) | ||
| if err != nil { | ||
| return model.Config{}, err | ||
| } | ||
|
|
||
| // TODO: Why are these fields set in the Dockerfile and not in the config.yaml file? | ||
| config.AppConfig.EnableUI = viper.GetBool(ENABLE_UI) | ||
| config.AppConfig.RodBrowserBin = viper.GetString(ROD_BROWSER_BIN) | ||
| if config.AppConfig.RodBrowserBin == "" { | ||
| return model.Config{}, fmt.Errorf("environment variable %s not set", ROD_BROWSER_BIN) | ||
| } | ||
|
|
||
| return config, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package config | ||
|
|
||
| const ( | ||
| CONFIG_FILE_NAME = "CONFIG_FILE_NAME" | ||
| CONFIG_FILE_TYPE = "CONFIG_FILE_TYPE" | ||
| CONFIG_FILE_PATH = "CONFIG_FILE_PATH" | ||
|
|
||
| ENABLE_UI = "ENABLE_UI" | ||
| ROD_BROWSER_BIN = "ROD_BROWSER_BIN" | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "github.com/Zomato/espresso/lib/s3" | ||
| ) | ||
|
|
||
| type Config struct { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this to service/internal pkg
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ..pkg/config/model.go for models and pkg/config/config.go for logic |
||
| AppConfig AppConfig `mapstructure:"app"` | ||
| TemplateStorageConfig StorageConfig `mapstructure:"template_storage"` | ||
| FileStorageConfig StorageConfig `mapstructure:"file_storage"` | ||
| BrowserConfig BrowserConfig `mapstructure:"browser"` | ||
| WorkerPoolConfig WorkerPoolConfig `mapstructure:"workerpool"` | ||
| S3Config s3.Config `mapstructure:"s3"` | ||
| AWSConfig s3.AwsCredConfig `mapstructure:"aws"` | ||
| CertConfig map[string]CertConfig `mapstructure:"certificates"` | ||
| DBConfig DBConfig `mapstructure:"db"` | ||
| } | ||
|
|
||
| type AppConfig struct { | ||
| LogLevel string `mapstructure:"log_level"` | ||
| ServerPort int `mapstructure:"server_port"` | ||
| EnableUI bool `mapstructure:"enable_ui"` | ||
| RodBrowserBin string `mapstructure:"rod_browser_bin"` | ||
| } | ||
|
|
||
| type StorageConfig struct { | ||
| StorageType string `mapstructure:"storage_type"` | ||
| } | ||
|
|
||
| type BrowserConfig struct { | ||
| TabPool int `mapstructure:"tab_pool"` | ||
| } | ||
|
|
||
| type WorkerPoolConfig struct { | ||
| WorkerCount int `mapstructure:"worker_count"` | ||
| WorkerTimeoutMs int `mapstructure:"worker_timeout_ms"` | ||
| } | ||
|
|
||
| type CertConfig struct { | ||
| CertPath string `mapstructure:"cert_path"` | ||
| KeyPath string `mapstructure:"key_path"` | ||
| KeyPassword string `mapstructure:"key_password"` | ||
| } | ||
|
|
||
| type DBConfig struct { | ||
| Host string `mapstructure:"host"` | ||
| Port int `mapstructure:"port"` | ||
| Username string `mapstructure:"username"` | ||
| Password string `mapstructure:"password"` | ||
| Database string `mapstructure:"database"` | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3306? check dockercompose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I was running Espresso service in my local machine (in debug mode) and not as a docker container, I had mapped the
hostaslocalhostand port as3308instead of 3306. I will resolve this error and push it.