43 lines
850 B
Go
43 lines
850 B
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
// Config 应用配置(从环境变量读取)
|
|
type Config struct {
|
|
Port string
|
|
Mode string
|
|
DBDSN string
|
|
TrustedProxies []string
|
|
CORSOrigins []string
|
|
}
|
|
|
|
// Load 加载配置,开发环境可读 .env
|
|
func Load() (*Config, error) {
|
|
_ = godotenv.Load()
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
mode := os.Getenv("GIN_MODE")
|
|
if mode == "" {
|
|
mode = "debug"
|
|
}
|
|
dsn := os.Getenv("DB_DSN")
|
|
if dsn == "" {
|
|
dsn = "user:pass@tcp(127.0.0.1:3306)/soul?charset=utf8mb4&parseTime=True"
|
|
}
|
|
|
|
return &Config{
|
|
Port: port,
|
|
Mode: mode,
|
|
DBDSN: dsn,
|
|
TrustedProxies: []string{"127.0.0.1", "::1"},
|
|
CORSOrigins: []string{"http://localhost:5174", "http://127.0.0.1:5174", "https://soul.quwanzhi.com"},
|
|
}, nil
|
|
}
|