feat(config): env-based configuration with validation

This commit is contained in:
2026-07-01 16:27:31 +07:00
parent 353f1e9dd3
commit 0d42eb2db0
3 changed files with 100 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
package config
import (
"encoding/base64"
"testing"
)
func TestLoadRequiresEncKey32Bytes(t *testing.T) {
t.Setenv("DATABASE_URL", "postgres://x")
t.Setenv("AUTH_USER", "admin")
t.Setenv("AUTH_PASS", "pass")
t.Setenv("SESSION_SECRET", "secret")
t.Setenv("ENC_KEY", base64.StdEncoding.EncodeToString(make([]byte, 16))) // wrong size
if _, err := Load(); err == nil {
t.Fatal("expected error for 16-byte ENC_KEY, got nil")
}
}
func TestLoadDefaults(t *testing.T) {
t.Setenv("DATABASE_URL", "postgres://x")
t.Setenv("AUTH_USER", "admin")
t.Setenv("AUTH_PASS", "pass")
t.Setenv("SESSION_SECRET", "secret")
t.Setenv("ENC_KEY", base64.StdEncoding.EncodeToString(make([]byte, 32)))
cfg, err := Load()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.HTTPAddr != ":8080" {
t.Errorf("HTTPAddr = %q, want :8080", cfg.HTTPAddr)
}
if cfg.WorkerConcurrency != 4 {
t.Errorf("WorkerConcurrency = %d, want 4", cfg.WorkerConcurrency)
}
}