feat(config): загрузка env-конфига (DSN, ENC-ключ, listen)

This commit is contained in:
2026-07-03 13:35:47 +07:00
parent 50aec973ff
commit fc10451340
4 changed files with 290 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
package config
import (
"encoding/base64"
"fmt"
"os"
)
type Config struct {
DBDSN string
EncKey []byte
ListenAddr string
}
func Load() (*Config, error) {
dsn := os.Getenv("DNS_AR_DB_DSN")
if dsn == "" {
return nil, fmt.Errorf("config: DNS_AR_DB_DSN is required")
}
rawKey := os.Getenv("DNS_AR_ENC_KEY")
if rawKey == "" {
return nil, fmt.Errorf("config: DNS_AR_ENC_KEY is required")
}
key, err := base64.StdEncoding.DecodeString(rawKey)
if err != nil {
return nil, fmt.Errorf("config: DNS_AR_ENC_KEY must be base64: %w", err)
}
if len(key) != 32 {
return nil, fmt.Errorf("config: DNS_AR_ENC_KEY must decode to 32 bytes, got %d", len(key))
}
listen := os.Getenv("DNS_AR_LISTEN")
if listen == "" {
listen = ":8080"
}
return &Config{DBDSN: dsn, EncKey: key, ListenAddr: listen}, nil
}