-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from fabiante/feat/refactor-env-config
- Loading branch information
Showing
11 changed files
with
565 additions
and
63 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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# devtools | ||
.idea | ||
|
||
# config files | ||
/.env | ||
/app.* | ||
|
||
# other | ||
*.sqlite |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func DbDSN() string { | ||
dsn := vip.GetString("db.dsn") | ||
|
||
if dsn == "" { | ||
panic(errors.New("db dsn may not be empty")) | ||
} | ||
|
||
return dsn | ||
} | ||
|
||
func DbMaxConnections() int { | ||
return vip.GetInt("db.max_connections") | ||
} | ||
|
||
func TestLoad() bool { | ||
return vip.IsSet("test_load") | ||
} |
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,44 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var vip *viper.Viper | ||
|
||
func init() { | ||
vip = setupViper() | ||
} | ||
|
||
func setupViper() *viper.Viper { | ||
v := viper.New() | ||
|
||
// loading | ||
v.AddConfigPath(".") | ||
v.SetConfigName("app") | ||
|
||
// helper to panic on any error | ||
check := func(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
|
||
// defaults | ||
v.SetDefault("db.max_connections", 10) | ||
|
||
// env binding | ||
check(setupEnv(v)) | ||
|
||
// trigger config parsing - optional | ||
if err := v.ReadInConfig(); err != nil { | ||
var configFileNotFoundError viper.ConfigFileNotFoundError | ||
if !errors.As(err, &configFileNotFoundError) { | ||
check(err) | ||
} | ||
} | ||
|
||
return v | ||
} |
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,8 @@ | ||
# Example configuration - copy as app.yml and modify | ||
|
||
# Enable this if you want to run load tests | ||
# test_load: true | ||
|
||
db: | ||
dsn: postgresql://persurl:persurl@localhost:5432/persurl?sslmode=disable | ||
max_connections: 10 |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.