The Rust URL Shortener is built using a modern, layered architecture that emphasizes separation of concerns, maintainability, and performance.
rust-url-shortener/
├── src/
│ ├── main.rs # Application entry point
│ ├── lib.rs # Library root, module declarations
│ ├── config.rs # Configuration management
│ ├── db.rs # Database connection pool setup
│ ├── error.rs # Custom error types and handlers
│ ├── handlers.rs # HTTP request handlers
│ ├── loggers.rs # Logging configuration
│ ├── models.rs # Database models and structures
│ ├── routes.rs # Route configuration
│ ├── schema.rs # Database schema (generated by Diesel)
│ └── utils.rs # Utility functions
├── tests/
│ └── integrationTests.rs # Integration tests
├── migrations/ # Database migrations
├── docs/ # Documentation
└── examples/ # Usage examples
Responsibility: Handle HTTP requests and responses
Responsibility: Core application logic
Responsibility: Database interactions
Responsibility: Data persistence
┌─────────────┐
│ Client │
└──────┬──────┘
│ HTTP Request
▼
┌─────────────────────────────────────────┐
│ Actix-web Server │
│ ┌──────────────────────────────────┐ │
│ │ Middleware Layer │ │
│ │ - Logger │ │
│ │ - Error Handler │ │
│ └──────────────┬───────────────────┘ │
│ ▼ │
│ ┌──────────────────────────────────┐ │
│ │ Routes │ │
│ │ - POST / │ │
│ │ - GET / │ │
│ │ - GET /{short_code} │ │
│ └──────────────┬───────────────────┘ │
│ ▼ │
│ ┌──────────────────────────────────┐ │
│ │ Handlers │ │
│ │ - Business Logic │ │
│ │ - Validation │ │
│ └──────────────┬───────────────────┘ │
│ ▼ │
│ ┌──────────────────────────────────┐ │
│ │ Models/DB │ │
│ │ - Diesel ORM │ │
│ │ - Connection Pool │ │
│ └──────────────┬───────────────────┘ │
└─────────────────┼───────────────────────┘
▼
┌──────────────┐
│ Database │
│ (SQLite) │
└──────────────┘
Rationale:
Rationale:
Rationale:
Implementation: r2d2 connection pool
Benefits:
original_urlGET /{short_code}┌─────────────────────────────────────┐
│ Error Types (error.rs) │
│ - DatabaseError │
│ - ValidationError │
│ - NotFoundError │
└─────────────────┬───────────────────┘
│
▼
┌─────────────────────────────────────┐
│ Error Handler Middleware │
│ - Converts errors to HTTP responses│
│ - Logs errors │
│ - Returns user-friendly messages │
└─────────────────────────────────────┘