145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
// Config 网关配置
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Dubbo DubboConfig
|
|
JWT JWTConfig
|
|
OSS OSSConfig
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// JWTConfig JWT 配置
|
|
type JWTConfig struct {
|
|
Secret string
|
|
}
|
|
|
|
// 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 目录
|
|
}
|
|
}
|
|
|
|
// Load 加载配置
|
|
func Load() *Config {
|
|
return &Config{
|
|
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"),
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", "topfans-secret-key-please-change-in-production"),
|
|
},
|
|
OSS: OSSConfig{
|
|
Region: getEnv("OSS_REGION", "cn-shanghai"),
|
|
BucketName: getEnv("OSS_BUCKET_NAME", "top-fans-test"),
|
|
RoleArn: getEnv("OSS_STS_ROLE_ARN", "acs:ram::1387642798143585:role/top-fans-oss-user"),
|
|
AccessKeyID: getEnv("OSS_ACCESS_KEY_ID", "LTAI5tNaAjTNiHnefMCG3q4J"),
|
|
AccessKeySecret: getEnv("OSS_ACCESS_KEY_SECRET", "48wwZvNkUn1PO1xWjV4HuE5JjB6G7c"),
|
|
AvatarDir: getEnv("OSS_AVATAR_DIR", "avatar/"),
|
|
AssetDir: getEnv("OSS_ASSET_DIR", "asset/"),
|
|
TokenExpireTime: getEnvInt("OSS_TOKEN_EXPIRE_TIME", 3600),
|
|
},
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|