233 lines
6.2 KiB
Go
233 lines
6.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
// Config 网关配置
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Dubbo DubboConfig
|
|
JWT JWTConfig
|
|
OSS OSSConfig
|
|
Segment SegmentConfig
|
|
Dify DifyConfig
|
|
Minimax MinimaxConfig
|
|
LaserCompositor LaserCompositorConfig
|
|
Redis RedisConfig
|
|
DB DBConfig
|
|
Root string
|
|
}
|
|
|
|
// MinimaxConfig MiniMax 图像生成配置
|
|
type MinimaxConfig struct {
|
|
APIKey string // MiniMax API 密钥
|
|
APIURL string // 默认 https://api.minimaxi.com/v1/image_generation
|
|
}
|
|
|
|
// LaserCompositorConfig laser-compositor 合成服务配置
|
|
type LaserCompositorConfig struct {
|
|
URL string // 默认 http://127.0.0.1:7000
|
|
}
|
|
|
|
// DifyConfig Dify 工作流配置
|
|
type DifyConfig struct {
|
|
APIBase string
|
|
APIKey string
|
|
Server ServerConfig
|
|
Dubbo DubboConfig
|
|
JWT JWTConfig
|
|
OSS OSSConfig
|
|
Redis RedisConfig
|
|
WebSocket WebSocketConfig
|
|
Root string
|
|
}
|
|
|
|
// RedisConfig Redis 配置
|
|
type RedisConfig struct {
|
|
Host string
|
|
Port int
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
// DBConfig 数据库配置
|
|
type DBConfig struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
DBName string
|
|
SSLMode string
|
|
TimeZone string
|
|
}
|
|
|
|
// ServerConfig 服务器配置
|
|
type ServerConfig struct {
|
|
Port string
|
|
Mode string // debug, release, test
|
|
}
|
|
|
|
// DubboConfig Dubbo 服务配置
|
|
type DubboConfig struct {
|
|
UserServiceURL string
|
|
SocialServiceURL string
|
|
AssetServiceURL string
|
|
GalleryServiceURL string
|
|
ActivityServiceURL string
|
|
TaskServiceURL string
|
|
StarbookServiceURL string
|
|
AIChatServiceURL string
|
|
}
|
|
|
|
// JWTConfig JWT 配置
|
|
type JWTConfig struct {
|
|
Secret string
|
|
}
|
|
|
|
// SegmentConfig 人像抠图(服务端代理)
|
|
type SegmentConfig struct {
|
|
Provider string // auto | imageseg | viapi | ivpd | http
|
|
InferenceURL string // 自部署 rembg 等 HTTP 地址,如 http://127.0.0.1:7000/api/remove
|
|
}
|
|
|
|
// OSSConfig OSS 配置
|
|
type OSSConfig struct {
|
|
Region string
|
|
BucketName string
|
|
RoleArn string
|
|
AccessKeyID string
|
|
AccessKeySecret string
|
|
AvatarDir string // 头像上传目录,如 "avatar/"
|
|
AssetDir string // 资产上传目录,如 "asset/"
|
|
TokenExpireTime int // Token 过期时间(秒),默认 3600
|
|
}
|
|
|
|
// GetUploadDir 根据类型获取上传目录
|
|
func (c *OSSConfig) GetUploadDir(uploadType string) string {
|
|
switch uploadType {
|
|
case "avatar":
|
|
return c.AvatarDir
|
|
case "asset":
|
|
return c.AssetDir
|
|
default:
|
|
return c.AssetDir // 默认使用 asset 目录
|
|
}
|
|
}
|
|
|
|
// WebSocketConfig WebSocket 配置
|
|
type WebSocketConfig struct {
|
|
AIChatPath string // WebSocket 路径,默认 /ws/ai-chat
|
|
}
|
|
|
|
// Load 加载配置
|
|
func Load() *Config {
|
|
root, _ := os.Getwd()
|
|
return &Config{
|
|
Root: root,
|
|
Server: ServerConfig{
|
|
Port: getEnv("SERVER_PORT", "8080"),
|
|
Mode: getEnv("GIN_MODE", "debug"),
|
|
},
|
|
Dubbo: DubboConfig{
|
|
UserServiceURL: getEnv("DUBBO_USER_SERVICE_URL", "tri://127.0.0.1:20000"),
|
|
SocialServiceURL: getEnv("DUBBO_SOCIAL_SERVICE_URL", "tri://127.0.0.1:20002"),
|
|
AssetServiceURL: getEnv("DUBBO_ASSET_SERVICE_URL", "tri://127.0.0.1:20003"),
|
|
GalleryServiceURL: getEnv("DUBBO_GALLERY_SERVICE_URL", "tri://127.0.0.1:20004"),
|
|
ActivityServiceURL: getEnv("DUBBO_ACTIVITY_SERVICE_URL", "tri://127.0.0.1:20005"),
|
|
TaskServiceURL: getEnv("DUBBO_TASK_SERVICE_URL", "tri://127.0.0.1:20006"),
|
|
StarbookServiceURL: getEnv("DUBBO_STARBOOK_SERVICE_URL", "tri://127.0.0.1:20007"),
|
|
AIChatServiceURL: getEnv("DUBBO_AI_CHAT_SERVICE_URL", "tri://127.0.0.1:20008"),
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", ""),
|
|
},
|
|
OSS: OSSConfig{
|
|
Region: getEnv("OSS_REGION", "cn-shanghai"),
|
|
BucketName: getEnv("OSS_BUCKET_NAME", ""),
|
|
RoleArn: getEnv("OSS_STS_ROLE_ARN", ""),
|
|
AccessKeyID: getEnv("OSS_ACCESS_KEY_ID", ""),
|
|
AccessKeySecret: getEnv("OSS_ACCESS_KEY_SECRET", ""),
|
|
AvatarDir: getEnv("OSS_AVATAR_DIR", "avatar/"),
|
|
AssetDir: getEnv("OSS_ASSET_DIR", "asset/"),
|
|
TokenExpireTime: getEnvInt("OSS_TOKEN_EXPIRE_TIME", 3600),
|
|
},
|
|
Segment: SegmentConfig{
|
|
Provider: getEnv("SEGMENT_PROVIDER", "imageseg"),
|
|
InferenceURL: getEnv("SEGMENT_INFERENCE_URL", ""),
|
|
},
|
|
Dify: DifyConfig{
|
|
APIBase: getEnv("DIFY_API_BASE", ""),
|
|
APIKey: getEnv("DIFY_API_KEY", ""),
|
|
},
|
|
Minimax: MinimaxConfig{
|
|
APIKey: getEnv("MINIMAX_API_KEY", ""),
|
|
APIURL: getEnv("MINIMAX_API_URL", "https://api.minimaxi.com/v1/image_generation"),
|
|
},
|
|
LaserCompositor: LaserCompositorConfig{
|
|
URL: getEnv("LASER_COMPOSITOR_URL", "http://127.0.0.1:7000"),
|
|
},
|
|
Redis: RedisConfig{
|
|
Host: getEnv("REDIS_HOST", "127.0.0.1"),
|
|
Port: getEnvInt("REDIS_PORT", 6379),
|
|
Password: getEnv("REDIS_PASSWORD", ""),
|
|
DB: getEnvInt("REDIS_DB", 0),
|
|
},
|
|
WebSocket: WebSocketConfig{
|
|
AIChatPath: getEnv("WS_AI_CHAT_PATH", "/ws/ai-chat"),
|
|
},
|
|
}
|
|
}
|
|
|
|
// getEnv 获取环境变量,如果不存在则返回默认值
|
|
func getEnv(key, defaultValue string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return defaultValue
|
|
}
|
|
return value
|
|
}
|
|
|
|
// getEnvInt 获取整型环境变量,如果不存在或解析失败则返回默认值
|
|
func getEnvInt(key string, defaultValue int) int {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return defaultValue
|
|
}
|
|
intValue, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return intValue
|
|
}
|
|
|
|
// Validate 验证配置
|
|
func (c *Config) Validate() error {
|
|
if c.Server.Port == "" {
|
|
return fmt.Errorf("server port is required")
|
|
}
|
|
if c.Dubbo.UserServiceURL == "" {
|
|
return fmt.Errorf("dubbo user service URL is required")
|
|
}
|
|
if c.Dubbo.SocialServiceURL == "" {
|
|
return fmt.Errorf("dubbo social service URL is required")
|
|
}
|
|
if c.Dubbo.AssetServiceURL == "" {
|
|
return fmt.Errorf("dubbo asset service URL is required")
|
|
}
|
|
if c.Dubbo.GalleryServiceURL == "" {
|
|
return fmt.Errorf("dubbo gallery service URL is required")
|
|
}
|
|
if c.Dubbo.ActivityServiceURL == "" {
|
|
return fmt.Errorf("dubbo activity service URL is required")
|
|
}
|
|
if c.Dubbo.TaskServiceURL == "" {
|
|
return fmt.Errorf("dubbo task service URL is required")
|
|
}
|
|
if c.JWT.Secret == "" {
|
|
return fmt.Errorf("JWT secret is required")
|
|
}
|
|
return nil
|
|
} |