- 替换中转站从 xbcl.link 到 weda.cc - prompt 模板改为镭射卡全图生成(去掉 6 层合成/抠图依赖) - 4 路并发调用 + 原图展示 = 5 张 variant - 前端提示词中译英支持 - 全局 Vue errorHandler - WebSocket 鉴权失败跳登录 - 删除已弃用的 laserCompositor 微服务 Co-Authored-By: Claude <noreply@anthropic.com>
267 lines
7.6 KiB
Go
267 lines
7.6 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
|
||
OpenAI OpenAIConfig
|
||
LaserGen LaserGenConfig
|
||
LaserCompositor LaserCompositorConfig
|
||
Redis RedisConfig
|
||
DB DBConfig
|
||
WebSocket WebSocketConfig
|
||
Root string
|
||
}
|
||
|
||
// LaserGenConfig 镭射卡生成器配置
|
||
// Provider:
|
||
// "minimax" (默认) - 后端直连 MiniMax + 并行 5 variant + compositor 合成
|
||
// "dify" - 调 Dify 工作流, 由 Dify 内部分发与合成, 阻塞模式
|
||
// "openai" - 后端直连 OpenAI Images API /v1/images/edits, 5 路并发 + 直接落 OSS
|
||
type LaserGenConfig struct {
|
||
Provider 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
|
||
Workflow string // 工作流名称/ID,默认 laser_prompt_enhancer_v2
|
||
}
|
||
|
||
// OpenAIConfig OpenAI Images API 配置
|
||
// 仅在 LASER_GEN_PROVIDER=openai 时使用
|
||
type OpenAIConfig struct {
|
||
APIKey string // 必填
|
||
BaseURL string // 默认 https://api.openai.com/v1
|
||
Model string // 默认 gpt-image-1.5
|
||
}
|
||
|
||
// 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
|
||
StatisticServiceURL 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 路径,默认 /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"),
|
||
StatisticServiceURL: getEnv("DUBBO_STATISTIC_SERVICE_URL", "tri://127.0.0.1:20009"),
|
||
},
|
||
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", ""),
|
||
Workflow: getEnv("DIFY_WORKFLOW", "laser_prompt_enhancer_v2"),
|
||
},
|
||
Minimax: MinimaxConfig{
|
||
APIKey: getEnv("MINIMAX_API_KEY", ""),
|
||
APIURL: getEnv("MINIMAX_API_URL", "https://api.minimaxi.com/v1/image_generation"),
|
||
},
|
||
OpenAI: OpenAIConfig{
|
||
APIKey: getEnv("OPENAI_API_KEY", ""),
|
||
BaseURL: getEnv("OPENAI_BASE_URL", "https://api.openai.com/v1"),
|
||
Model: getEnv("OPENAI_MODEL", "gpt-image-1.5"),
|
||
},
|
||
LaserGen: LaserGenConfig{
|
||
Provider: getEnv("LASER_GEN_PROVIDER", "minimax"),
|
||
},
|
||
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),
|
||
},
|
||
DB: DBConfig{
|
||
Host: getEnv("DB_HOST", "localhost"),
|
||
Port: getEnvInt("DB_PORT", 5432),
|
||
User: getEnv("DB_USER", "postgres"),
|
||
Password: getEnv("DB_PASSWORD", ""),
|
||
DBName: getEnv("DB_NAME", "top-fans"),
|
||
SSLMode: getEnv("DB_SSLMODE", "disable"),
|
||
TimeZone: getEnv("DB_TIMEZONE", "Asia/Shanghai"),
|
||
},
|
||
WebSocket: WebSocketConfig{
|
||
AIChatPath: getEnv("WS_AI_CHAT_PATH", "/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
|
||
} |