-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multi-file configuration read example
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Parse multiple files for configuration | ||
|
||
This example shows how the package can be used to read from mutiple configuration files and assign them to the same structure. | ||
|
||
In this example, the configuration is read from ```db_config.yaml```,```email_config.yaml``` and ```general_config.yaml``` and the values are stored in the ```config``` struct. |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
database: | ||
host: "localhost" | ||
user: "root" | ||
password: "password" | ||
name: "cleanenv" | ||
port: "5432" | ||
ssl_mode: "disable" |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
email_service: | ||
email_key: "RANDOM_KEY" | ||
account_validation_template_id: "template_id" |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
port: "8888" | ||
jwt_secret: "RANDOM_SECRET" |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ilyakaznacheev/cleanenv" | ||
) | ||
|
||
type config struct { | ||
Port string `yaml:"port"` | ||
JWTSecret string `yaml:"jwt_secret"` | ||
Database databaseConfig `yaml:"database"` | ||
EmailService emailService `yaml:"email_service"` | ||
} | ||
|
||
type databaseConfig struct { | ||
User string `yaml:"user"` | ||
Password string `yaml:"password"` | ||
Name string `yaml:"name"` | ||
Host string `yaml:"host"` | ||
Port string `yaml:"port"` | ||
SSLMode string `yaml:"ssl_mode"` | ||
} | ||
|
||
type emailService struct { | ||
EmailKey string `yaml:"email_key"` | ||
AccountValidationTemplateID string `yaml:"account_validation_template_id"` | ||
} | ||
|
||
func main() { | ||
cfg, err := ParseConfigFiles("./db_config.yaml", "./email_config.yaml", "./general_config.yaml") | ||
if err != nil { | ||
log.Printf("Error parsing config files: %v", err) | ||
return | ||
} | ||
|
||
log.Println("Parsed Configuration") | ||
log.Println(*cfg) | ||
|
||
return | ||
} | ||
|
||
func ParseConfigFiles(files ...string) (*config, error) { | ||
var cfg config | ||
|
||
for i := 0; i < len(files); i++ { | ||
err := cleanenv.ReadConfig(files[i], &cfg) | ||
if err != nil { | ||
log.Printf("Error reading configuration from file:%v", files[i]) | ||
return nil, err | ||
} | ||
} | ||
|
||
return &cfg, nil | ||
} |