88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
LayaURL string
|
|
LayaTimeout time.Duration
|
|
LayaAPIKey string
|
|
OpenFIGIAPIKey string
|
|
OpenFIGITimeout time.Duration
|
|
DatabaseURL string
|
|
JWTSecret string
|
|
EncryptionKey []byte
|
|
Port string
|
|
ScraperURL string
|
|
AdminEmail string
|
|
AdminPassword string
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
dbURL := os.Getenv("DATABASE_URL")
|
|
if dbURL == "" {
|
|
return nil, fmt.Errorf("DATABASE_URL is required")
|
|
}
|
|
|
|
jwtSecret := os.Getenv("JWT_SECRET")
|
|
if jwtSecret == "" {
|
|
return nil, fmt.Errorf("JWT_SECRET is required")
|
|
}
|
|
|
|
encHex := os.Getenv("ENCRYPTION_KEY")
|
|
if encHex == "" {
|
|
return nil, fmt.Errorf("ENCRYPTION_KEY is required")
|
|
}
|
|
encKey, err := hex.DecodeString(encHex)
|
|
if err != nil || len(encKey) != 32 {
|
|
return nil, fmt.Errorf("ENCRYPTION_KEY must be a valid 32-byte hex string")
|
|
}
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
scraperURL := os.Getenv("SCRAPER_URL")
|
|
if scraperURL == "" {
|
|
scraperURL = "http://scraper:3001"
|
|
}
|
|
|
|
timeout := 5 * time.Second
|
|
if value := os.Getenv("OPENFIGI_TIMEOUT"); value != "" {
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil || parsed <= 0 || parsed > 30*time.Second {
|
|
return nil, fmt.Errorf("OPENFIGI_TIMEOUT must be a duration greater than 0 and at most 30s")
|
|
}
|
|
timeout = parsed
|
|
}
|
|
layaURL := os.Getenv("LAYA_URL")
|
|
if layaURL == "" {
|
|
layaURL = "http://laya:8000"
|
|
}
|
|
layaTimeout := 5 * time.Minute
|
|
if value := os.Getenv("LAYA_TIMEOUT"); value != "" {
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil || parsed <= 0 || parsed > 10*time.Minute {
|
|
return nil, fmt.Errorf("LAYA_TIMEOUT must be a duration greater than 0 and at most 10m")
|
|
}
|
|
layaTimeout = parsed
|
|
}
|
|
return &Config{
|
|
LayaURL: layaURL, LayaTimeout: layaTimeout, LayaAPIKey: os.Getenv("LAYA_API_KEY"),
|
|
OpenFIGIAPIKey: os.Getenv("OPENFIGI_API_KEY"),
|
|
OpenFIGITimeout: timeout,
|
|
DatabaseURL: dbURL,
|
|
JWTSecret: jwtSecret,
|
|
EncryptionKey: encKey,
|
|
Port: port,
|
|
ScraperURL: scraperURL,
|
|
AdminEmail: os.Getenv("ADMIN_EMAIL"),
|
|
AdminPassword: os.Getenv("ADMIN_PASSWORD"),
|
|
}, nil
|
|
}
|