This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"gardomatic.kleiax.de/internal/api"
|
||||
"gardomatic.kleiax.de/internal/mailer"
|
||||
"gardomatic.kleiax.de/internal/platform/environment"
|
||||
"gardomatic.kleiax.de/internal/platform/validate"
|
||||
"gardomatic.kleiax.de/internal/storage"
|
||||
)
|
||||
|
||||
func configFromEnvironment() (api.Config, error) {
|
||||
@@ -55,6 +57,9 @@ func configFromEnvironment() (api.Config, error) {
|
||||
Port: integer("GARDOMATIC_API_PORT", 4000),
|
||||
Env: environment.String("GARDOMATIC_ENV", "development"),
|
||||
WebBaseURL: environment.String("GARDOMATIC_WEB_BASE_URL", "http://localhost:4040"),
|
||||
DemoAccountEmail: strings.ToLower(strings.TrimSpace(
|
||||
environment.String("GARDOMATIC_DEMO_ACCOUNT_EMAIL", ""),
|
||||
)),
|
||||
DB: api.DatabaseConfig{
|
||||
Dsn: required("GARDOMATIC_DB_DSN"), MaxOpenConns: integer("GARDOMATIC_DB_MAX_OPEN_CONNS", 25),
|
||||
MaxIdleConns: integer("GARDOMATIC_DB_MAX_IDLE_CONNS", 25), MaxIdleTime: duration("GARDOMATIC_DB_MAX_IDLE_TIME", 15*time.Minute),
|
||||
@@ -92,6 +97,12 @@ func configFromEnvironment() (api.Config, error) {
|
||||
if cfg.Env == "production" && !cfg.Session.CookieSecure {
|
||||
errs = append(errs, errors.New("GARDOMATIC_COOKIE_SECURE must be true in production"))
|
||||
}
|
||||
if cfg.DemoAccountEmail != "" {
|
||||
v := validate.New()
|
||||
if storage.ValidateEmail(v, cfg.DemoAccountEmail); !v.Valid() {
|
||||
errs = append(errs, errors.New("GARDOMATIC_DEMO_ACCOUNT_EMAIL must be a valid email address"))
|
||||
}
|
||||
}
|
||||
if _, err := mailer.New(cfg.Mail); err != nil {
|
||||
errs = append(errs, fmt.Errorf("mail configuration: %w", err))
|
||||
}
|
||||
|
||||
+10
-1
@@ -7,15 +7,24 @@ func TestConfigFromEnvironment(t *testing.T) {
|
||||
t.Setenv("GARDOMATIC_API_HOST", "127.0.0.1")
|
||||
t.Setenv("GARDOMATIC_API_PORT", "4100")
|
||||
t.Setenv("GARDOMATIC_CORS_TRUSTED_ORIGINS", "https://example.com, https://app.example.com")
|
||||
t.Setenv("GARDOMATIC_DEMO_ACCOUNT_EMAIL", " Demo@Example.com ")
|
||||
cfg, err := configFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Host != "127.0.0.1" || cfg.Port != 4100 || len(cfg.Cors.TrustedOrigins) != 2 {
|
||||
if cfg.Host != "127.0.0.1" || cfg.Port != 4100 || len(cfg.Cors.TrustedOrigins) != 2 || cfg.DemoAccountEmail != "demo@example.com" {
|
||||
t.Fatalf("unexpected config: %#v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigRejectsInvalidDemoAccountEmail(t *testing.T) {
|
||||
t.Setenv("GARDOMATIC_DB_DSN", "postgres://example")
|
||||
t.Setenv("GARDOMATIC_DEMO_ACCOUNT_EMAIL", "not-an-email")
|
||||
if _, err := configFromEnvironment(); err == nil {
|
||||
t.Fatal("expected invalid demo account email error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigRequiresDSN(t *testing.T) {
|
||||
t.Setenv("GARDOMATIC_DB_DSN", "")
|
||||
if _, err := configFromEnvironment(); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user