77 lines
1.4 KiB
Go
Executable File
77 lines
1.4 KiB
Go
Executable File
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
WeChat WeChatConfig
|
|
JWT JWTConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
Mode string
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Host string
|
|
Port string
|
|
User string
|
|
Password string
|
|
DBName string
|
|
}
|
|
|
|
type WeChatConfig struct {
|
|
AppID string
|
|
AppSecret string
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string
|
|
Expire int
|
|
}
|
|
|
|
var AppConfig *Config
|
|
|
|
func LoadConfig() {
|
|
// 加载 .env 文件
|
|
if err := godotenv.Load(); err != nil {
|
|
log.Println("未找到 .env 文件,使用环境变量")
|
|
}
|
|
|
|
AppConfig = &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnv("SERVER_PORT", "8080"),
|
|
Mode: getEnv("GIN_MODE", "debug"),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Host: getEnv("DB_HOST", "localhost"),
|
|
Port: getEnv("DB_PORT", "3306"),
|
|
User: getEnv("DB_USER", "root"),
|
|
Password: getEnv("DB_PASSWORD", ""),
|
|
DBName: getEnv("DB_NAME", "wx_service"),
|
|
},
|
|
WeChat: WeChatConfig{
|
|
AppID: getEnv("WECHAT_APP_ID", ""),
|
|
AppSecret: getEnv("WECHAT_APP_SECRET", ""),
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
|
Expire: 86400, // 24小时
|
|
},
|
|
}
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|