新增配置管理功能及多存储支持
- 添加管理员端 API,用于获取和更新完整配置。 - 添加公共端 API,用于获取非敏感配置信息。 - 增加本地存储(LocalStorage)、S3(S3Storage)、和 WebDAV(WebDAVStorage)存储类型的实现。 - 支持热更新存储配置和保存配置文件至磁盘。 - 更新 Swagger 文档以包含新接口定义。
This commit is contained in:
70
internal/api/admin/config.go
Normal file
70
internal/api/admin/config.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"FileRelay/internal/bootstrap"
|
||||
"FileRelay/internal/config"
|
||||
"FileRelay/internal/model"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ConfigHandler struct{}
|
||||
|
||||
func NewConfigHandler() *ConfigHandler {
|
||||
return &ConfigHandler{}
|
||||
}
|
||||
|
||||
// GetConfig 获取当前完整配置
|
||||
// @Summary 获取完整配置
|
||||
// @Description 获取系统的完整配置文件内容(仅管理员)
|
||||
// @Tags Admin
|
||||
// @Security AdminAuth
|
||||
// @Produce json
|
||||
// @Success 200 {object} config.Config
|
||||
// @Router /admin/config [get]
|
||||
func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, config.GlobalConfig)
|
||||
}
|
||||
|
||||
// UpdateConfig 更新配置
|
||||
// @Summary 更新配置
|
||||
// @Description 更新系统的配置文件内容(仅管理员)
|
||||
// @Tags Admin
|
||||
// @Security AdminAuth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param config body config.Config true "新配置内容"
|
||||
// @Success 200 {object} model.Response
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /admin/config [put]
|
||||
func (h *ConfigHandler) UpdateConfig(c *gin.Context) {
|
||||
var newConfig config.Config
|
||||
if err := c.ShouldBindJSON(&newConfig); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 简单的校验,防止数据库路径被改空等关键错误
|
||||
if newConfig.Database.Path == "" {
|
||||
newConfig.Database.Path = config.GlobalConfig.Database.Path
|
||||
}
|
||||
|
||||
// 更新内存配置
|
||||
config.UpdateGlobalConfig(&newConfig)
|
||||
|
||||
// 重新初始化存储(热更新业务逻辑)
|
||||
if err := bootstrap.ReloadStorage(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to reload storage: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 保存到文件
|
||||
if err := config.SaveConfig(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to save config: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.SuccessResponse("Config updated successfully and hot-reloaded"))
|
||||
}
|
||||
41
internal/api/public/config.go
Normal file
41
internal/api/public/config.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package public
|
||||
|
||||
import (
|
||||
"FileRelay/internal/config"
|
||||
"FileRelay/internal/model"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ConfigHandler struct{}
|
||||
|
||||
func NewConfigHandler() *ConfigHandler {
|
||||
return &ConfigHandler{}
|
||||
}
|
||||
|
||||
// PublicConfig 公开配置结构
|
||||
type PublicConfig struct {
|
||||
Site config.SiteConfig `json:"site"`
|
||||
Upload config.UploadConfig `json:"upload"`
|
||||
APIToken struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
} `json:"api_token"`
|
||||
}
|
||||
|
||||
// GetPublicConfig 获取非敏感配置
|
||||
// @Summary 获取公共配置
|
||||
// @Description 获取前端展示所需的非敏感配置数据
|
||||
// @Tags Public
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.Response{data=PublicConfig}
|
||||
// @Router /api/config [get]
|
||||
func (h *ConfigHandler) GetPublicConfig(c *gin.Context) {
|
||||
pub := PublicConfig{
|
||||
Site: config.GlobalConfig.Site,
|
||||
Upload: config.GlobalConfig.Upload,
|
||||
}
|
||||
pub.APIToken.Enabled = config.GlobalConfig.APIToken.Enabled
|
||||
|
||||
c.JSON(http.StatusOK, model.SuccessResponse(pub))
|
||||
}
|
||||
Reference in New Issue
Block a user