Files
yhy-mager/backend/internal/config/config.go
xiewenjun dc2efcde08
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled
feat: add online payment management and checkout
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
2026-10-15 09:26:40 +08:00

121 lines
4.0 KiB
Go

package config
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type Config struct {
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
CredentialEncryptionKey []byte
PaymentConfigEncryptionKey []byte
}
func Load(getenv func(string) string) (Config, error) {
c := Config{HTTPAddr: "127.0.0.1:18080"}
if v := getenv("HTTP_ADDR"); v != "" {
c.HTTPAddr = v
}
_, port, err := net.SplitHostPort(c.HTTPAddr)
if err != nil {
return Config{}, fmt.Errorf("HTTP_ADDR must be host:port")
}
p, err := strconv.Atoi(port)
if err != nil || p < 1 || p > 65535 {
return Config{}, fmt.Errorf("HTTP_ADDR port must be between 1 and 65535")
}
for _, setting := range []struct {
name string
value *time.Duration
fallback time.Duration
}{
{"HTTP_READ_HEADER_TIMEOUT", &c.ReadHeaderTimeout, 5 * time.Second},
{"HTTP_READ_TIMEOUT", &c.ReadTimeout, 30 * time.Second},
{"HTTP_WRITE_TIMEOUT", &c.WriteTimeout, 30 * time.Second},
{"HTTP_IDLE_TIMEOUT", &c.IdleTimeout, 60 * time.Second},
{"HTTP_SHUTDOWN_TIMEOUT", &c.ShutdownTimeout, 10 * time.Second},
} {
*setting.value = setting.fallback
if v := getenv(setting.name); v != "" {
d, err := time.ParseDuration(v)
if err != nil || d <= 0 || d > 5*time.Minute {
return Config{}, fmt.Errorf("%s must be a duration greater than zero and at most 5m", setting.name)
}
*setting.value = d
}
}
if value := getenv("HTTP_TRUSTED_PROXY_CIDRS"); value != "" {
for _, value := range strings.Split(value, ",") {
value = strings.TrimSpace(value)
_, network, err := net.ParseCIDR(value)
if err != nil {
return Config{}, fmt.Errorf("trusted proxies must be explicit CIDRs")
}
ones, _ := network.Mask.Size()
if ones == 0 {
return Config{}, fmt.Errorf("trust-all proxies are forbidden")
}
c.TrustedProxies = append(c.TrustedProxies, value)
}
}
if value := getenv("SESSION_COOKIE_SECURE"); value != "" && value != "true" && value != "false" {
return Config{}, fmt.Errorf("SESSION_COOKIE_SECURE must be true or false")
}
c.SigningPrivateKeyPEM = getenv("LICENSE_SIGNING_PRIVATE_KEY_PEM")
if path := getenv("LICENSE_SIGNING_PRIVATE_KEY_FILE"); path != "" {
if c.SigningPrivateKeyPEM != "" {
return Config{}, fmt.Errorf("only one signing key source may be configured")
}
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("LICENSE_SIGNING_PRIVATE_KEY_FILE must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) == 0 || len(data) > 16384 {
return Config{}, fmt.Errorf("signing key file must be readable and between 1 and 16384 bytes")
}
c.SigningPrivateKeyPEM = string(data)
}
if path := getenv("LICENSE_CREDENTIAL_ENCRYPTION_KEY_FILE"); path != "" {
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("credential encryption key path must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) != 32 {
return Config{}, fmt.Errorf("credential encryption key must be exactly 32 raw bytes")
}
c.CredentialEncryptionKey = data
}
if path := getenv("PAYMENT_CONFIG_ENCRYPTION_KEY_FILE"); path != "" {
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("PAYMENT_CONFIG_ENCRYPTION_KEY_FILE must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) != 32 {
return Config{}, fmt.Errorf("payment config encryption key must be exactly 32 raw bytes")
}
c.PaymentConfigEncryptionKey = data
}
if root := getenv("PACKAGE_STORAGE_ROOT"); root != "" {
if !filepath.IsAbs(root) {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be absolute")
}
info, err := os.Stat(root)
if err != nil || !info.IsDir() {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be an existing directory")
}
}
return c, nil
}