Skip to content
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

96 ensure defaults in env vars #98

Merged
merged 4 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func addRequestID() gin.HandlerFunc {
}

func logger() gin.HandlerFunc {
if os.Getenv("AUTH_SERVER_APP_ENV") == "dev" {
if util.IsDevEnvironment() {
return gin.Logger()
}

Expand Down
2 changes: 1 addition & 1 deletion authenticator/interactors/implementation/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type authenticator struct {
repository user.Repository
emailSender email.Sender
ExpirationTime int `env:"TOKEN_EXPIRATION" envDefault:"3600"`
Secret string `env:"AUTH_SERVER_SECRET"`
Secret string `env:"AUTH_SERVER_SECRET" envDefault:"super-secret"`
}

func NewAuthenticator(repository user.Repository, sender email.Sender) interactors.Authenticator {
Expand Down
6 changes: 3 additions & 3 deletions database/user/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
type sqliteStorage struct {
db *gorm.DB
Name string `env:"AUTH_SERVER_DATABASE_NAME" envDefault:"sqlite.s3db"`
User string `env:"AUTH_SERVER_DATABASE_USER"`
Password string `env:"AUTH_SERVER_DATABASE_PASSWORD"`
Salt string `env:"AUTH_SERVER_DATABASE_SALT"`
User string `env:"AUTH_SERVER_DATABASE_USER" envDefault:"admin"`
Password string `env:"AUTH_SERVER_DATABASE_PASSWORD" envDefault:"secure-password"`
Salt string `env:"AUTH_SERVER_DATABASE_SALT" envDefault:"salted"`
}

func NewSqliteStorage() (user.Repository, func()) {
Expand Down
4 changes: 2 additions & 2 deletions email/providers/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func NewSMTPSender() email.Provider {
}

if sender.Username == "" {
contextLogger.Panic("a username is required", err)
contextLogger.Panic("AUTH_SERVER_SMTP_USERNAME env variable is not set", err)
}

if sender.Password == "" {
contextLogger.Panic("a password is required", err)
contextLogger.Panic("AUTH_SERVER_SMTP_PASSWORD env variable is not set", err)
}

sender.dialer = gomail.NewDialer(sender.Host, sender.Port, sender.Username, sender.Password)
Expand Down
33 changes: 33 additions & 0 deletions tools/util/global_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package util

import (
"github.com/bixlabs/authentication/tools"
"github.com/caarlos0/env"
)

var config GlobalConfig //nolint

func IsDevEnvironment() bool {
return newConfig().Env == "dev"
}

type GlobalConfig struct {
Env string `env:"AUTH_SERVER_APP_ENV" envDefault:"dev"`
}

func newConfig() GlobalConfig {
if config.Env != "" {
return config
}
parseConfiguration()
return config
}

func parseConfiguration() GlobalConfig {
config = GlobalConfig{}
err := env.Parse(&config)
if err != nil {
tools.Log().WithError(err).Panic("parsing the env variables for global configuration failed")
}
return config
}