Enhance database configuration to support multiple data sources
- Updated .env.example to include optional configuration for additional database instances. - Refactored DatabaseConfig in config.go to accommodate a default database and additional instances. - Implemented loadAdditionalDBConfigs function to dynamically load configurations for extra databases. - Modified InitDB function in database.go to establish connections for both default and additional databases. - Updated README.md to document the new configuration options for connecting to multiple databases.
This commit is contained in:
+40
-5
@@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
@@ -27,6 +28,11 @@ type ServerConfig struct {
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Default DatabaseInstanceConfig
|
||||
Additional map[string]DatabaseInstanceConfig
|
||||
}
|
||||
|
||||
type DatabaseInstanceConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
@@ -94,17 +100,22 @@ func LoadConfig() {
|
||||
log.Println("未找到 .env 文件,使用环境变量")
|
||||
}
|
||||
|
||||
defaultDB := DatabaseInstanceConfig{
|
||||
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"),
|
||||
}
|
||||
|
||||
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"),
|
||||
Default: defaultDB,
|
||||
Additional: loadAdditionalDBConfigs(defaultDB),
|
||||
},
|
||||
JWT: JWTConfig{
|
||||
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
||||
@@ -148,6 +159,30 @@ func LoadConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
func loadAdditionalDBConfigs(defaultCfg DatabaseInstanceConfig) map[string]DatabaseInstanceConfig {
|
||||
instances := strings.Split(getEnv("DB_INSTANCES", ""), ",")
|
||||
result := make(map[string]DatabaseInstanceConfig)
|
||||
for _, rawName := range instances {
|
||||
name := strings.TrimSpace(rawName)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
upperName := strings.ToUpper(name)
|
||||
prefix := "DB_" + upperName + "_"
|
||||
result[strings.ToLower(name)] = DatabaseInstanceConfig{
|
||||
Host: getEnv(prefix+"HOST", defaultCfg.Host),
|
||||
Port: getEnv(prefix+"PORT", defaultCfg.Port),
|
||||
User: getEnv(prefix+"USER", defaultCfg.User),
|
||||
Password: getEnv(prefix+"PASSWORD", defaultCfg.Password),
|
||||
DBName: getEnv(prefix+"NAME", defaultCfg.DBName),
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getEnv(key, defaultValue string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user