✅ 已完成功能: 1. 项目基础设施和Docker开发环境 2. 前端React 18 + TypeScript架构 3. 后端Golang + Gin框架 4. 多租户数据库设计 5. 完整API路由系统 6. 智能客服聊天界面 7. 详细文档和部署指南 🔧 技术栈: - 前端:React 18, TypeScript, Vite, Zustand - 后端:Golang, Gin, GORM, PostgreSQL - 部署:Docker, Docker Compose 🎨 设计规范: - 无渐变色,无紫色 - 简洁专业的中性色系 - 响应式布局 📊 状态: - 前端开发服务器:http://localhost:5173 - 后端API服务:http://localhost:8080 - 数据库:PostgreSQL + Redis - 完整的多租户架构 作者:小弟 (大哥的AI助手) 日期:2026-02-27
124 lines
2.7 KiB
Go
124 lines
2.7 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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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", "localhost"),
|
|
Port: getEnvAsInt("DB_PORT", 5432),
|
|
User: getEnv("DB_USER", "postgres"),
|
|
Password: getEnv("DB_PASSWORD", "postgres"),
|
|
DBName: getEnv("DB_NAME", "customer_service"),
|
|
SSLMode: getEnv("DB_SSL_MODE", "disable"),
|
|
},
|
|
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
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intValue, err := strconv.Atoi(value); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsFloat(key string, defaultValue float64) float64 {
|
|
if value := os.Getenv(key); value != "" {
|
|
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
|
return floatValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|