Files
RelationshipManagement-backend/config/config.go
2025-12-10 19:03:24 +08:00

53 lines
989 B
Go

package config
import (
"os"
)
// Config 应用配置
type Config struct {
Server ServerConfig
DB DBConfig
}
// ServerConfig 服务器配置
type ServerConfig struct {
Port string
Mode string // debug, release, test
}
// DBConfig 数据库配置
type DBConfig struct {
Host string
Port string
User string
Password string
DBName string
}
// LoadConfig 加载配置
func LoadConfig() *Config {
return &Config{
Server: ServerConfig{
Port: getEnv("SERVER_PORT", "8080"),
Mode: getEnv("GIN_MODE", "debug"),
},
DB: DBConfig{
Host: getEnv("DB_HOST", "localhost"),
Port: getEnv("DB_PORT", "3306"),
User: getEnv("DB_USER", "root"),
Password: getEnv("DB_PASSWORD", ""),
DBName: getEnv("DB_NAME", "relationship_db"),
},
}
}
// getEnv 获取环境变量,如果不存在则返回默认值
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}