实现 MinIO 对象存储图片流水线(可迁移 OSS)
- 新增 S3 兼容 Storage 抽象与 MinIO 实现,compose 启动 MinIO - 上传接口:校验 → 缩放 → WebP → 主图/缩略图入库 - 消息图片 content 改为对象 URL,拒绝 base64 - 工作台/访客端改为先上传再发消息
This commit is contained in:
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -9,6 +10,7 @@ type Config struct {
|
||||
Server ServerConfig
|
||||
Database DatabaseConfig
|
||||
JWT JWTConfig
|
||||
Storage StorageConfig
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -30,6 +32,20 @@ type JWTConfig struct {
|
||||
ExpireTime time.Duration
|
||||
}
|
||||
|
||||
// StorageConfig S3 兼容对象存储(MinIO / OSS / COS / S3 共用字段)
|
||||
type StorageConfig struct {
|
||||
Endpoint string // 如 localhost:9000 或 oss-cn-hangzhou.aliyuncs.com
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
Bucket string
|
||||
UseSSL bool
|
||||
Region string
|
||||
PublicBase string // 浏览器可访问前缀,如 http://localhost:9000/kefu
|
||||
MaxUploadMB int
|
||||
MaxImageEdge int // 最长边像素
|
||||
WebPQuality float32
|
||||
}
|
||||
|
||||
func Load() *Config {
|
||||
return &Config{
|
||||
Server: ServerConfig{
|
||||
@@ -38,7 +54,7 @@ func Load() *Config {
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Host: getEnv("DB_HOST", "localhost"),
|
||||
Port: getEnv("DB_PORT", "5432"),
|
||||
Port: getEnv("DB_PORT", "5433"),
|
||||
User: getEnv("DB_USER", "postgres"),
|
||||
Password: getEnv("DB_PASSWORD", "postgres"),
|
||||
Name: getEnv("DB_NAME", "kefu_sys"),
|
||||
@@ -48,6 +64,18 @@ func Load() *Config {
|
||||
Secret: getEnv("JWT_SECRET", "kefu-sys-secret-key"),
|
||||
ExpireTime: 24 * time.Hour,
|
||||
},
|
||||
Storage: StorageConfig{
|
||||
Endpoint: getEnv("STORAGE_ENDPOINT", "localhost:9000"),
|
||||
AccessKey: getEnv("STORAGE_ACCESS_KEY", "minioadmin"),
|
||||
SecretKey: getEnv("STORAGE_SECRET_KEY", "minioadmin"),
|
||||
Bucket: getEnv("STORAGE_BUCKET", "kefu"),
|
||||
UseSSL: getEnvBool("STORAGE_USE_SSL", false),
|
||||
Region: getEnv("STORAGE_REGION", "us-east-1"),
|
||||
PublicBase: getEnv("STORAGE_PUBLIC_BASE_URL", "http://localhost:9000/kefu"),
|
||||
MaxUploadMB: getEnvInt("STORAGE_MAX_UPLOAD_MB", 10),
|
||||
MaxImageEdge: getEnvInt("STORAGE_MAX_IMAGE_EDGE", 1920),
|
||||
WebPQuality: float32(getEnvInt("STORAGE_WEBP_QUALITY", 80)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,3 +90,27 @@ func getEnv(key, fallback string) string {
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getEnvBool(key string, fallback bool) bool {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return fallback
|
||||
}
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func getEnvInt(key string, fallback int) int {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return fallback
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user