Files
FileRelay/internal/config/config.go
hxuanyu fe656fb298 新增配置管理功能及多存储支持
- 添加管理员端 API,用于获取和更新完整配置。
- 添加公共端 API,用于获取非敏感配置信息。
- 增加本地存储(LocalStorage)、S3(S3Storage)、和 WebDAV(WebDAVStorage)存储类型的实现。
- 支持热更新存储配置和保存配置文件至磁盘。
- 更新 Swagger 文档以包含新接口定义。
2026-01-14 14:15:20 +08:00

110 lines
2.2 KiB
Go

package config
import (
"os"
"sync"
"gopkg.in/yaml.v3"
)
type Config struct {
Site SiteConfig `yaml:"site"`
Security SecurityConfig `yaml:"security"`
Upload UploadConfig `yaml:"upload"`
Storage StorageConfig `yaml:"storage"`
APIToken APITokenConfig `yaml:"api_token"`
Database DatabaseConfig `yaml:"database"`
}
type SiteConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
}
type SecurityConfig struct {
AdminPasswordHash string `yaml:"admin_password_hash"`
PickupCodeLength int `yaml:"pickup_code_length"`
PickupFailLimit int `yaml:"pickup_fail_limit"`
JWTSecret string `yaml:"jwt_secret"`
}
type UploadConfig struct {
MaxFileSizeMB int64 `yaml:"max_file_size_mb"`
MaxBatchFiles int `yaml:"max_batch_files"`
MaxRetentionDays int `yaml:"max_retention_days"`
}
type StorageConfig struct {
Type string `yaml:"type"`
Local struct {
Path string `yaml:"path"`
} `yaml:"local"`
WebDAV struct {
URL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Root string `yaml:"root"`
} `yaml:"webdav"`
S3 struct {
Endpoint string `yaml:"endpoint"`
Region string `yaml:"region"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
Bucket string `yaml:"bucket"`
UseSSL bool `yaml:"use_ssl"`
} `yaml:"s3"`
}
type APITokenConfig struct {
Enabled bool `yaml:"enabled"`
AllowAdminAPI bool `yaml:"allow_admin_api"`
MaxTokens int `yaml:"max_tokens"`
}
type DatabaseConfig struct {
Path string `yaml:"path"`
}
var (
GlobalConfig *Config
ConfigPath string
configLock sync.RWMutex
)
func LoadConfig(path string) error {
configLock.Lock()
defer configLock.Unlock()
data, err := os.ReadFile(path)
if err != nil {
return err
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return err
}
GlobalConfig = &cfg
ConfigPath = path
return nil
}
func SaveConfig() error {
configLock.RLock()
defer configLock.RUnlock()
data, err := yaml.Marshal(GlobalConfig)
if err != nil {
return err
}
return os.WriteFile(ConfigPath, data, 0644)
}
func UpdateGlobalConfig(cfg *Config) {
configLock.Lock()
defer configLock.Unlock()
GlobalConfig = cfg
}