Feat: Config Manager API

This commit is contained in:
DullJZ
2025-11-06 01:14:09 +08:00
parent d463fdb65e
commit 291c327477
4 changed files with 240 additions and 4 deletions

View File

@@ -16,6 +16,7 @@ type AdminHandler struct {
bucketManager *bucket.Manager
balancer *balancer.Balancer
config *config.Config
configManager *config.Manager
}
// NewAdminHandler 创建新的管理API处理器
@@ -23,11 +24,13 @@ func NewAdminHandler(
bucketManager *bucket.Manager,
balancer *balancer.Balancer,
cfg *config.Config,
configManager *config.Manager,
) *AdminHandler {
return &AdminHandler{
bucketManager: bucketManager,
balancer: balancer,
config: cfg,
configManager: configManager,
}
}
@@ -72,9 +75,12 @@ type HealthResponse struct {
// RegisterRoutes 注册管理API路由
func (h *AdminHandler) RegisterRoutes(router *mux.Router) {
router.HandleFunc("/api/buckets", h.ListBuckets).Methods(http.MethodGet)
router.HandleFunc("/api/buckets/{name}", h.GetBucketDetail).Methods(http.MethodGet)
router.HandleFunc("/api/health", h.GetHealth).Methods(http.MethodGet)
// 注册路由,同时支持 OPTIONS 方法用于 CORS 预检
router.HandleFunc("/buckets", h.ListBuckets).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/buckets/{name}", h.GetBucketDetail).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/health", h.GetHealth).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/config", h.GetConfig).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/config", h.UpdateConfig).Methods(http.MethodPost, http.MethodOptions)
}
// ListBuckets 获取存储桶列表
@@ -137,6 +143,58 @@ func (h *AdminHandler) GetHealth(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(response)
}
// GetConfig 获取当前配置
func (h *AdminHandler) GetConfig(w http.ResponseWriter, r *http.Request) {
if h.configManager == nil {
http.Error(w, `{"error": "config manager not available"}`, http.StatusInternalServerError)
return
}
currentConfig := h.configManager.GetConfig()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(currentConfig)
}
// UpdateConfig 更新配置
func (h *AdminHandler) UpdateConfig(w http.ResponseWriter, r *http.Request) {
if h.configManager == nil {
http.Error(w, `{"error": "config manager not available"}`, http.StatusInternalServerError)
return
}
// 解析请求体
var newConfig config.Config
if err := json.NewDecoder(r.Body).Decode(&newConfig); err != nil {
http.Error(w, `{"error": "invalid JSON format: `+err.Error()+`"}`, http.StatusBadRequest)
return
}
// 设置默认值
newConfig.SetDefaults()
// 更新配置
if err := h.configManager.UpdateConfig(&newConfig); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{
"error": "validation failed",
"message": err.Error(),
})
return
}
// 返回成功响应
response := map[string]interface{}{
"success": true,
"message": "Configuration updated successfully. Changes will take effect automatically.",
"config": &newConfig,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// convertBucketInfo 转换BucketInfo为BucketResponse
func (h *AdminHandler) convertBucketInfo(b *bucket.BucketInfo) BucketResponse {
resp := BucketResponse{