Files
ddns_local_server/CLEAN_ARCHITECTURE.md
2025-09-30 12:49:39 +02:00

4.5 KiB

Clean Architecture Refactoring - Complete

Summary

Successfully refactored the DDNS updater from a monolithic design to a clean architecture with Domain-Driven Design principles. The new architecture supports multiple web servers through trait-based abstractions.

Architecture Overview

Domain Layer (src/domain/)

  • entities.rs: Core domain entities (IpEntry, WebServerConfig, WebServerType)
  • ports.rs: Trait definitions for dependency inversion (Repository patterns, Service interfaces)
  • services.rs: Business logic implementation (DdnsUpdateService)
  • value_objects.rs: Value objects with validation (ConfigPath, Hostname, BackupRetention)

Infrastructure Layer (src/infrastructure/)

  • repositories.rs: File-based and in-memory IP storage implementations
  • webservers/nginx.rs: Nginx configuration handler
  • webservers/apache.rs: Apache configuration handler
  • network.rs: HTTP-based network service for IP discovery
  • notifications.rs: Console, log, and composite notification services
  • config_discovery.rs: Configuration file type detection

Application Layer (src/application/)

  • services.rs: Service factory and application configuration
  • use_cases.rs: Use case orchestration (UpdateDdnsUseCase, ConfigValidationUseCase, DdnsApplication)

Interface Layer (src/interface/)

  • cli_interface.rs: Clean async CLI implementation using tokio runtime

Key Features

Multi-Server Support

  • Nginx: Complete implementation with location block handling
  • Apache: Complete implementation with Directory/Location block handling
  • 🔲 Caddy: Framework ready for implementation
  • 🔲 Traefik: Framework ready for implementation

Clean Architecture Benefits

  • Dependency Inversion: All dependencies flow inward to the domain layer
  • Testability: Each layer can be unit tested independently
  • Extensibility: New web servers can be added by implementing WebServerHandler trait
  • Maintainability: Clear separation of concerns and single responsibility

Async Architecture

  • Full async/await support with tokio runtime
  • Send + Sync error handling for thread safety
  • Async trait implementations across all service boundaries

Technical Implementation

Trait-Based Design

#[async_trait]
pub trait WebServerHandler {
    async fn update_allow_list(&self, config: &WebServerConfig, hostname: &str, old_ip: Option<IpAddr>, new_ip: IpAddr) -> Result<bool, Box<dyn std::error::Error + Send + Sync>>;
    async fn validate_config(&self, config: &WebServerConfig) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
    async fn create_backup(&self, config: &WebServerConfig) -> Result<std::path::PathBuf, Box<dyn std::error::Error + Send + Sync>>;
    async fn test_configuration(&self, config: &WebServerConfig) -> Result<bool, Box<dyn std::error::Error + Send + Sync>>;
    async fn reload_server(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
}

Dependency Injection

  • ServiceFactory pattern for creating configured service instances
  • Arc for shared ownership of trait objects
  • Constructor injection for testability

Error Handling

  • Consistent error types: Box<dyn std::error::Error + Send + Sync>
  • Thread-safe error propagation
  • Domain-specific error types where appropriate

Migration Status

  • Domain layer: Complete
  • Infrastructure layer: Complete
  • Application layer: Complete
  • Interface layer: Complete
  • Compilation: Success
  • CLI interface: Working
  • Backward compatibility: Maintained

Usage

The application maintains the same CLI interface while now supporting multiple web server types:

# Update nginx configuration (auto-detected)
./ddns_updater --config /etc/nginx/sites-available/mysite.conf --verbose

# Update apache configuration (auto-detected)  
./ddns_updater --config /etc/apache2/sites-available/mysite.conf --verbose

# Process directory of configurations (mixed types supported)
./ddns_updater --config-dir /etc/nginx/sites-available/ --verbose

Future Enhancements

  1. Add Caddy Support: Implement WebServerHandler for Caddy configurations
  2. Add Traefik Support: Implement WebServerHandler for Traefik dynamic configurations
  3. Plugin System: Dynamic loading of web server handlers
  4. Configuration Validation: Enhanced validation for each server type
  5. Testing Suite: Comprehensive integration tests for all server types

The clean architecture foundation makes all these enhancements straightforward to implement.