diff --git a/coolify/proxy/caddy/dynamic/caddy-sync.go b/coolify/proxy/caddy/dynamic/caddy-sync.go index c22f46e..73acaa0 100644 --- a/coolify/proxy/caddy/dynamic/caddy-sync.go +++ b/coolify/proxy/caddy/dynamic/caddy-sync.go @@ -140,7 +140,7 @@ func syncConfigs(config *Config) error { if err != nil { return fmt.Errorf("failed to get current directory: %w", err) } - logger := cylogger.Default.WithPrefix(fmt.Sprintf("dir=%s", currentDir)) + logger := cylogger.Default // Get existing .caddy files logger.Debug("Scanning for existing .caddy files") @@ -180,7 +180,7 @@ func syncConfigs(config *Config) error { return fmt.Errorf("failed to create file %s: %w", caddyFilename, err) } } - } else if normalizeContent(existingContent) != normalizeContent(expectedContent) { + } else if !contentMatches(existingContent, expectedContent) { // File exists but content differs fileLogger.Info("File content differs, updating") updated++ @@ -335,14 +335,37 @@ func generateAndLogDiff(logger *cylogger.Logger, filename, existingContent, expe } } -func normalizeContent(content string) string { - // Normalize content for comparison by trimming whitespace and standardizing line endings - lines := strings.Split(content, "\n") +func contentMatches(existing, expected string) bool { + dmp := diffmatchpatch.New() + diffs := dmp.DiffMain(normalize(existing), normalize(expected), false) + + // If there are any insertions or deletions, content differs + for _, diff := range diffs { + if diff.Type == diffmatchpatch.DiffInsert || diff.Type == diffmatchpatch.DiffDelete { + cylogger.Trace("Content differs at %q", diff.Text) + return false + } + } + return true +} + +func normalize(text string) string { + // Standardize line endings + text = strings.ReplaceAll(text, "\r\n", "\n") + text = strings.ReplaceAll(text, "\r", "\n") + + // Split into lines and normalize each + lines := strings.Split(text, "\n") var normalizedLines []string for _, line := range lines { - normalizedLines = append(normalizedLines, strings.TrimSpace(line)) + trimmed := strings.TrimSpace(line) + if trimmed != "" { // Skip empty lines + normalizedLines = append(normalizedLines, trimmed) + } } - return strings.Join(normalizedLines, "\n") + // Join with single newlines and trim ends + result := strings.Join(normalizedLines, "\n") + return strings.TrimSpace(result) } \ No newline at end of file