spark-server/internal/bootstrap/config_defaults.go
2026-09-29 10:22:28 +08:00

118 lines
3.0 KiB
Go

package bootstrap
import (
"os"
"strings"
"time"
)
func applyConfigEnvOverrides(cfg *Config) {
if v := os.Getenv("SPARK_ENV"); v != "" {
cfg.Env = v
}
cfg.Env = parseEnv(cfg.Env)
if v := os.Getenv("SPARK_JWT_SECRET"); v != "" {
cfg.JWT.Secret = v
}
if v := os.Getenv("SPARK_ADMIN_JWT_SECRET"); v != "" {
cfg.Admin.JWTSecret = v
}
if v := os.Getenv("SPARK_REDIS_PASSWORD"); v != "" {
cfg.Redis.Password = v
}
if v := os.Getenv("SPARK_S3_ENDPOINT"); v != "" {
cfg.ObjectStorage.Endpoint = v
}
if v := os.Getenv("SPARK_S3_PUBLIC_ENDPOINT"); v != "" {
cfg.ObjectStorage.PublicEndpoint = v
}
if v := os.Getenv("SPARK_S3_ACCESS_KEY"); v != "" {
cfg.ObjectStorage.AccessKey = v
}
if v := os.Getenv("SPARK_S3_SECRET_KEY"); v != "" {
cfg.ObjectStorage.SecretKey = v
}
if v := os.Getenv("SPARK_S3_BUCKET"); v != "" {
cfg.ObjectStorage.Bucket = v
}
if v := os.Getenv("SPARK_SMTP_PASSWORD"); v != "" {
cfg.SMS.SMTP.Password = v
}
if v := os.Getenv("SPARK_ALIYUN_SMS_ACCESS_KEY_ID"); v != "" {
cfg.SMS.AliyunSMS.AccessKeyID = v
}
if v := os.Getenv("SPARK_ALIYUN_SMS_ACCESS_KEY_SECRET"); v != "" {
cfg.SMS.AliyunSMS.AccessKeySecret = v
}
}
func applyConfigStructDefaults(cfg *Config) {
if strings.TrimSpace(cfg.Admin.JWTSecret) == "" {
cfg.Admin.JWTSecret = "wyrs-admin-jwt-secret-change-in-production-2025"
}
if cfg.JWT.Issuer == "" {
cfg.JWT.Issuer = "spark-im"
}
if cfg.JWT.TTL <= 0 {
cfg.JWT.TTL = 7 * 24 * time.Hour
}
if cfg.JWT.RefreshTTL <= 0 {
cfg.JWT.RefreshTTL = 30 * 24 * time.Hour
}
if cfg.JWT.DenylistPrefix == "" {
cfg.JWT.DenylistPrefix = "denylist:"
}
if cfg.JWT.RefreshPrefix == "" {
cfg.JWT.RefreshPrefix = "refresh:"
}
if strings.TrimSpace(cfg.JWT.PairKeyMode) == "" {
cfg.JWT.PairKeyMode = "triple"
}
if cfg.Redis.DeliverQueueKey == "" {
cfg.Redis.DeliverQueueKey = "deliver:queue"
}
if cfg.Redis.DeliverProcessingKey == "" {
cfg.Redis.DeliverProcessingKey = "deliver:processing"
}
if cfg.Redis.DeliverDonePrefix == "" {
cfg.Redis.DeliverDonePrefix = "deliver:done:"
}
if cfg.Deliver.Workers < 1 {
cfg.Deliver.Workers = 8
}
if cfg.Deliver.TakeTimeout <= 0 {
cfg.Deliver.TakeTimeout = 5 * time.Second
}
if cfg.Deliver.DoneTTL <= 0 {
cfg.Deliver.DoneTTL = 10 * time.Minute
}
if cfg.RateLimit.AuthPerSecond < 1 {
cfg.RateLimit.AuthPerSecond = 5
}
if cfg.RateLimit.MessagePerSecond < 1 {
cfg.RateLimit.MessagePerSecond = 20
}
if cfg.ObjectStorage.Bucket == "" {
cfg.ObjectStorage.Bucket = "spark"
}
if cfg.ObjectStorage.Region == "" {
cfg.ObjectStorage.Region = "us-east-1"
}
if cfg.ObjectStorage.PresignTTL <= 0 {
cfg.ObjectStorage.PresignTTL = 15 * time.Minute
}
if cfg.ObjectStorage.ChatExpireDays < 1 {
cfg.ObjectStorage.ChatExpireDays = 365
}
if cfg.RateLimit.UploadPerSecond < 1 {
cfg.RateLimit.UploadPerSecond = 10
}
applyMediaDefaults(&cfg.Media)
if cfg.SquareBot.Interval <= 0 {
cfg.SquareBot.Interval = 5 * time.Minute
}
if cfg.SquareBot.TTL <= 0 {
cfg.SquareBot.TTL = 15 * 24 * time.Hour
}
}