file
This commit is contained in:
@@ -16,6 +16,16 @@ type Config struct {
|
||||
MySQL MySQLConfig `mapstructure:"mysql"`
|
||||
Logging LoggingConfig `mapstructure:"logging"`
|
||||
Admin AdminConfig `mapstructure:"admin"`
|
||||
Upload UploadConfig `mapstructure:"upload"`
|
||||
}
|
||||
|
||||
// UploadConfig 文件上传相关配置。
|
||||
type UploadConfig struct {
|
||||
StorageType string `mapstructure:"storage_type"` // local | minio(minio 预留)
|
||||
LocalPath string `mapstructure:"local_path"` // 本地存储根目录(storage_type=local)
|
||||
BaseURL string `mapstructure:"base_url"` // 文件可访问的基础路径/域名
|
||||
MaxSize int64 `mapstructure:"max_size"` // 单文件大小上限(字节),0 表示不限制
|
||||
AllowedExt []string `mapstructure:"allowed_ext"` // 扩展名白名单;为空表示不限制类型
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -97,5 +107,23 @@ func Load(configPath string) (*Config, error) {
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal config: %w", err)
|
||||
}
|
||||
applyUploadDefaults(&cfg.Upload)
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// applyUploadDefaults 为上传配置补齐合理默认值。
|
||||
func applyUploadDefaults(u *UploadConfig) {
|
||||
if u.StorageType == "" {
|
||||
u.StorageType = "local"
|
||||
}
|
||||
if u.LocalPath == "" {
|
||||
u.LocalPath = "./uploads"
|
||||
}
|
||||
if u.BaseURL == "" {
|
||||
u.BaseURL = "/files/static"
|
||||
}
|
||||
// MaxSize=0 视为不限制;仅当完全未配置(<0)时兜底为 10MB,避免误配导致无限大。
|
||||
if u.MaxSize < 0 {
|
||||
u.MaxSize = 10 << 20
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user