138 lines
3.2 KiB
Go
138 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
Redis RedisConfig
|
|
JWT JWTConfig
|
|
AI AIConfig
|
|
WebSocket WebSocketConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int
|
|
Mode string
|
|
ReadTimeout int
|
|
WriteTimeout int
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
DBName string
|
|
SSLMode string
|
|
Driver string // mysql, postgres
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Host string
|
|
Port int
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string
|
|
Expiration int
|
|
}
|
|
|
|
type AIConfig struct {
|
|
Provider string
|
|
APIKey string
|
|
Model string
|
|
BaseURL string
|
|
MaxTokens int
|
|
Temperature float64
|
|
}
|
|
|
|
type WebSocketConfig struct {
|
|
Port int
|
|
Path string
|
|
}
|
|
|
|
// Load 加载配置文件
|
|
func Load() (*Config, error) {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnvAsInt("SERVER_PORT", 8080),
|
|
Mode: getEnv("SERVER_MODE", "debug"),
|
|
ReadTimeout: getEnvAsInt("SERVER_READ_TIMEOUT", 30),
|
|
WriteTimeout: getEnvAsInt("SERVER_WRITE_TIMEOUT", 30),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Host: getEnv("DB_HOST", "124.221.239.98"),
|
|
Port: getEnvAsInt("DB_PORT", 3306),
|
|
User: getEnv("DB_USER", "root"),
|
|
Password: getEnv("DB_PASSWORD", "machine03"),
|
|
DBName: getEnv("DB_NAME", "openclaw"),
|
|
SSLMode: getEnv("DB_SSL_MODE", "TRUE"),
|
|
Driver: getEnv("DB_DRIVER", "mysql"),
|
|
},
|
|
Redis: RedisConfig{
|
|
Host: getEnv("REDIS_HOST", "localhost"),
|
|
Port: getEnvAsInt("REDIS_PORT", 6379),
|
|
Password: getEnv("REDIS_PASSWORD", ""),
|
|
DB: getEnvAsInt("REDIS_DB", 0),
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", "your-secret-key-change-in-production"),
|
|
Expiration: getEnvAsInt("JWT_EXPIRATION", 86400),
|
|
},
|
|
AI: AIConfig{
|
|
Provider: getEnv("AI_PROVIDER", "openai"),
|
|
APIKey: getEnv("AI_API_KEY", ""),
|
|
Model: getEnv("AI_MODEL", "gpt-3.5-turbo"),
|
|
BaseURL: getEnv("AI_BASE_URL", "https://api.openai.com/v1"),
|
|
MaxTokens: getEnvAsInt("AI_MAX_TOKENS", 1000),
|
|
Temperature: getEnvAsFloat("AI_TEMPERATURE", 0.7),
|
|
},
|
|
WebSocket: WebSocketConfig{
|
|
Port: getEnvAsInt("WS_PORT", 8081),
|
|
Path: getEnv("WS_PATH", "/ws"),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// GetDSN 返回数据库连接字符串
|
|
func (c *Config) GetDSN() string {
|
|
dbConfig := c.Database
|
|
if dbConfig.Driver == "mysql" {
|
|
return dbConfig.User + ":" + dbConfig.Password + "@tcp(" +
|
|
dbConfig.Host + ":" + strconv.Itoa(dbConfig.Port) + ")/" +
|
|
dbConfig.DBName + "?charset=utf8mb4&parseTime=True&loc=Local"
|
|
}
|
|
return "" // PostgreSQL 暂未实现
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt(key string, defaultValue int) int {
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
if intValue, err := strconv.Atoi(value); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsFloat(key string, defaultValue float64) float64 {
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
|
return floatValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|