Build and Publish Server / build (push) Successful in 2m6s
- 支持 465 隐式 TLS 与 587 STARTTLS,SMTP 配置优先、Postmark 备用 - 发件人支持 QQ 域名邮箱地址(z@kepo.net),凭证仅走环境变量 - HTTP 测试覆盖发送成功与认证失败;真实发信测试需显式开启 RUN_QQ_SMTP_LIVE_TEST Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
192 lines
4.6 KiB
Go
192 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
Redis RedisConfig
|
|
JWT JWTConfig
|
|
Wechat WechatConfig
|
|
Stripe StripeConfig
|
|
BinancePay BinancePayConfig
|
|
Postmark PostmarkConfig
|
|
SMTP SMTPConfig
|
|
Commerce CommerceConfig
|
|
Admin AdminConfig
|
|
Wiki WikiConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
Env string
|
|
CORSOrigins string
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Type string
|
|
Host string
|
|
Port string
|
|
User string
|
|
Password string
|
|
Name string
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Host string
|
|
Port string
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string
|
|
}
|
|
|
|
type WechatConfig struct {
|
|
AppID string
|
|
AppSecret string
|
|
PayKey string
|
|
MchID string
|
|
}
|
|
|
|
type StripeConfig struct {
|
|
SecretKey string
|
|
WebhookSecret string
|
|
APIBaseURL string
|
|
}
|
|
|
|
type BinancePayConfig struct {
|
|
APIKey string
|
|
SecretKey string
|
|
APIBaseURL string
|
|
WebhookPublicKey string
|
|
}
|
|
|
|
type PostmarkConfig struct {
|
|
ServerToken string
|
|
FromEmail string
|
|
APIBaseURL string
|
|
}
|
|
|
|
type SMTPConfig struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
FromEmail string
|
|
TLSMode string
|
|
TLSCAFile string
|
|
}
|
|
|
|
func (c SMTPConfig) IsConfigured() bool {
|
|
return c.Host != "" || c.Username != "" || c.Password != "" || c.FromEmail != ""
|
|
}
|
|
|
|
type CommerceConfig struct {
|
|
PublicBaseURL string
|
|
WebAppURL string
|
|
}
|
|
|
|
// AdminConfig 管理后台配置
|
|
type AdminConfig struct {
|
|
// Password 管理员登录密码;为空时禁用后台登录
|
|
Password string
|
|
}
|
|
|
|
// WikiConfig 百科/历史上的今天(维基百科同步)配置
|
|
type WikiConfig struct {
|
|
// SyncIntervalDays 同步间隔(天),距上次成功同步超过该间隔才会自动执行
|
|
SyncIntervalDays int
|
|
// SyncHour 自动同步执行的小时点(0-23)
|
|
SyncHour int
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnv("SERVER_PORT", "8080"),
|
|
Env: getEnv("SERVER_ENV", "development"),
|
|
CORSOrigins: getEnv("CORS_ORIGINS", ""),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Type: getEnv("DB_TYPE", "mysql"),
|
|
Host: getEnv("DB_HOST", "localhost"),
|
|
Port: getEnv("DB_PORT", "3306"),
|
|
User: getEnv("DB_USER", "root"),
|
|
Password: getEnv("DB_PASSWORD", ""),
|
|
Name: getEnv("DB_NAME", "lunar"),
|
|
},
|
|
Redis: RedisConfig{
|
|
Host: getEnv("REDIS_HOST", "localhost"),
|
|
Port: getEnv("REDIS_PORT", "6379"),
|
|
Password: getEnv("REDIS_PASSWORD", ""),
|
|
DB: 0,
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
|
},
|
|
Wechat: WechatConfig{
|
|
AppID: getEnv("MINI_APP_ID", ""),
|
|
AppSecret: getEnv("MINI_APP_SECRET", ""),
|
|
PayKey: getEnv("WECHAT_PAY_APIKEY", ""),
|
|
MchID: getEnv("WECHAT_PAY_MCHID", ""),
|
|
},
|
|
Stripe: StripeConfig{
|
|
SecretKey: getEnv("STRIPE_SECRET_KEY", ""),
|
|
WebhookSecret: getEnv("STRIPE_WEBHOOK_SECRET", ""),
|
|
APIBaseURL: getEnv("STRIPE_API_BASE_URL", "https://api.stripe.com"),
|
|
},
|
|
BinancePay: BinancePayConfig{
|
|
APIKey: getEnv("BINANCE_PAY_API_KEY", ""),
|
|
SecretKey: getEnv("BINANCE_PAY_SECRET_KEY", ""),
|
|
APIBaseURL: getEnv("BINANCE_PAY_API_BASE_URL", "https://bpay.binanceapi.com"),
|
|
WebhookPublicKey: getEnv("BINANCE_PAY_WEBHOOK_PUBLIC_KEY", ""),
|
|
},
|
|
Postmark: PostmarkConfig{
|
|
ServerToken: getEnv("POSTMARK_SERVER_TOKEN", ""),
|
|
FromEmail: getEnv("POSTMARK_FROM_EMAIL", ""),
|
|
APIBaseURL: getEnv("POSTMARK_API_BASE_URL", "https://api.postmarkapp.com"),
|
|
},
|
|
SMTP: SMTPConfig{
|
|
Host: getEnv("SMTP_HOST", ""),
|
|
Port: getEnv("SMTP_PORT", "465"),
|
|
Username: getEnv("SMTP_USERNAME", ""),
|
|
Password: getEnv("SMTP_PASSWORD", ""),
|
|
FromEmail: getEnv("SMTP_FROM_EMAIL", ""),
|
|
TLSMode: getEnv("SMTP_TLS_MODE", "implicit"),
|
|
TLSCAFile: getEnv("SMTP_TLS_CA_FILE", ""),
|
|
},
|
|
Commerce: CommerceConfig{
|
|
PublicBaseURL: getEnv("PUBLIC_BASE_URL", "http://localhost:8080"),
|
|
WebAppURL: getEnv("WEB_APP_URL", "http://localhost:4260"),
|
|
},
|
|
Admin: AdminConfig{
|
|
Password: getEnv("ADMIN_PASSWORD", ""),
|
|
},
|
|
Wiki: WikiConfig{
|
|
SyncIntervalDays: getEnvInt("WIKI_SYNC_INTERVAL_DAYS", 7),
|
|
SyncHour: getEnvInt("WIKI_SYNC_HOUR", 4),
|
|
},
|
|
}
|
|
}
|
|
|
|
func getEnvInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
if n, err := strconv.Atoi(value); err == nil {
|
|
return n
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|