Skip to content

Commit 8bcbda9

Browse files
committed
feat(sync): 添加配置路径删除功能并优化同步逻辑
添加DeleteConfigMap方法支持删除单个配置路径,在UploadConfig中增加清理不存在路径的逻辑,同时优化ExtensionRules的JSON解析处理
1 parent 64b389c commit 8bcbda9

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (cm *ConfigManager) createDefaultConfig() error {
182182
// 创建默认配置
183183
defaultConfig := Config{
184184
MAP: map[string]PathConfig{
185-
"/": {
185+
"/test": {
186186
DefaultTarget: "http://localhost:8080",
187187
// 添加新式扩展名规则映射示例
188188
ExtensionMap: []ExtRuleConfig{

pkg/sync/d1_client.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,32 @@ func (c *D1Client) BatchUpsertConfigMaps(ctx context.Context, maps []ConfigMap)
304304
return nil
305305
}
306306

307+
// DeleteConfigMap 删除单个配置路径
308+
func (c *D1Client) DeleteConfigMap(ctx context.Context, path string) error {
309+
url := fmt.Sprintf("%s/config-maps/%s", c.endpoint, path)
310+
req, err := http.NewRequestWithContext(ctx, "DELETE", url, nil)
311+
if err != nil {
312+
return fmt.Errorf("failed to create request: %w", err)
313+
}
314+
315+
if c.token != "" {
316+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
317+
}
318+
319+
resp, err := c.client.Do(req)
320+
if err != nil {
321+
return fmt.Errorf("failed to send request: %w", err)
322+
}
323+
defer resp.Body.Close()
324+
325+
if resp.StatusCode != http.StatusOK {
326+
body, _ := io.ReadAll(resp.Body)
327+
return fmt.Errorf("D1 API error (status %d): %s", resp.StatusCode, string(body))
328+
}
329+
330+
return nil
331+
}
332+
307333
// ============================================
308334
// Config Other
309335
// ============================================

pkg/sync/d1_manager.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,35 @@ func (m *D1Manager) UploadConfig(ctx context.Context, config any) error {
145145
return fmt.Errorf("failed to convert config: %w", err)
146146
}
147147

148+
// 获取 D1 中现有的所有路径
149+
existingMaps, err := m.storage.GetConfigMaps(ctx, false)
150+
if err != nil {
151+
log.Printf("[D1Sync] Warning: failed to get existing config maps: %v", err)
152+
// 继续执行,不影响上传
153+
} else {
154+
// 构建新配置中的路径集合
155+
newPaths := make(map[string]bool)
156+
for _, m := range maps {
157+
newPaths[m.Path] = true
158+
}
159+
160+
// 删除 D1 中已不存在的路径
161+
deletedCount := 0
162+
for _, existing := range existingMaps {
163+
if !newPaths[existing.Path] {
164+
if err := m.storage.DeleteConfigMap(ctx, existing.Path); err != nil {
165+
log.Printf("[D1Sync] Warning: failed to delete config map %s: %v", existing.Path, err)
166+
} else {
167+
deletedCount++
168+
log.Printf("[D1Sync] Deleted config map: %s", existing.Path)
169+
}
170+
}
171+
}
172+
if deletedCount > 0 {
173+
log.Printf("[D1Sync] Deleted %d config maps", deletedCount)
174+
}
175+
}
176+
148177
// 批量上传 ConfigMaps
149178
if len(maps) > 0 {
150179
if err := m.storage.BatchUpsertConfigMaps(ctx, maps); err != nil {
@@ -349,11 +378,13 @@ func (m *D1Manager) downloadConfigWithFallback(ctx context.Context) (map[string]
349378
"Enabled": cm.IsEnabled(), // 使用方法转换为 bool
350379
}
351380

352-
// 解析 ExtensionRules JSON
381+
// 解析 ExtensionRules JSON (应该是数组格式)
353382
if cm.ExtensionRules != "" {
354-
var extRules map[string]any
383+
var extRules []any
355384
if err := json.Unmarshal([]byte(cm.ExtensionRules), &extRules); err == nil {
356385
pathConfig["ExtensionMap"] = extRules
386+
} else {
387+
log.Printf("[D1Sync] Warning: failed to parse ExtensionRules for path %s: %v", cm.Path, err)
357388
}
358389
}
359390

0 commit comments

Comments
 (0)