A generic Rust project template using hexagonal architecture with Kafka, PostgreSQL, and clean architecture principles.
This is a production-ready template for building scalable Rust applications with:
- Hexagonal Architecture (Ports & Adapters pattern)
- CQRS and Event Sourcing patterns
- Clean separation of business logic and infrastructure
- Generic Kafka producers/consumers
- PostgreSQL with type-safe queries
- Graceful shutdown and observability
This project follows Hexagonal Architecture (Ports & Adapters) with clear separation of concerns:
- Domain Layer: Pure business logic (aggregates, entities, value objects, events)
- Application Layer: Use cases orchestration and port definitions
- Infrastructure Layer: Adapters for external systems (Kafka, PostgreSQL, HTTP)
- Shared Layer: Common utilities, traits, and error types
Domain (src/domain/)
- ✅ Zero infrastructure dependencies
- ✅ Pure business logic
- ✅ Aggregates apply events and validate invariants
- ✅ Immutable value objects
Application (src/application/)
- ✅ Defines Ports (interfaces) implemented by infrastructure
- ✅ Orchestrates use cases
- ✅ DTOs for cross-layer communication
- ✅ Queries for read-side (CQRS)
Infrastructure (src/infrastructure/)
- ✅ Implements Adapters (PostgreSQL, Kafka, HTTP)
- ✅ Bootstrap and configuration
- ✅ Event Store and message brokers
- ✅ Graceful shutdown handling
Shared Kernel (src/shared/)
- ✅ Common code across layers
- ✅ Shared traits and types
- ✅ Utilities (datetime, validation)
Presentation → Application → Domain
↓
Infrastructure
- Domain has zero dependencies (pure core)
- Application depends only on Domain
- Infrastructure implements Application contracts
- Presentation uses Application and injects Infrastructure via DI
- Runtime: Tokio async (full features)
- Web Framework: Axum 0.8.6 (optional)
- Database: PostgreSQL with SQLx (type-safe queries)
- Messaging: Kafka with rdkafka
- Logging: tracing + tracing-subscriber (structured JSON logs)
- Config: config + dotenvy (environment-based)
- Error Handling: thiserror + anyhow
src/
├── application/
│ ├── dtos/ # Data Transfer Objects
│ ├── ports/ # Port trait definitions (interfaces)
│ ├── queries/ # Query handlers (CQRS read side)
│ └── services/ # Application services (use case orchestration)
├── domain/
│ ├── aggregates/ # Domain aggregates (aggregate roots)
│ ├── entities/ # Domain entities
│ ├── events/ # Domain events
│ ├── repositories/ # Repository interfaces
│ ├── services/ # Domain services
│ └── value_objects/ # Immutable value objects
├── infrastructure/
│ ├── bootstrap/ # Component initialization with retry logic
│ ├── config/ # Configuration management
│ ├── event_store/ # Event store implementation
│ ├── messaging/
│ │ └── kafka/ # Generic Kafka producers/consumers
│ ├── repositories/ # Repository implementations
│ ├── shutdown/ # Graceful shutdown coordination
│ └── startup/ # Logging, banner, health checks
└── shared/
├── errors.rs # Common error types
├── traits/ # Common traits
├── types.rs # Type aliases
└── utils/ # Utilities
- ✅ Generic Kafka producers with configurable serialization (JSON, Avro, etc.)
- ✅ Generic Kafka consumers with pluggable message handlers
- ✅ Batching support for high-throughput scenarios
- ✅ Automatic retry and error handling
- ✅ Type-safe message processing
- ✅ PostgreSQL with SQLx for compile-time verified queries
- ✅ Generic repository pattern with
Repository<T>trait - ✅ Connection pooling and retry logic
- ✅ Migration support (Flyway or SQLx migrations)
- ✅ Structured JSON logging with tracing
- ✅ Startup health checks and banner
- ✅ Graceful shutdown with resource cleanup
- ✅ Signal handling (SIGTERM, SIGINT)
- ✅ Environment-based configuration
- ✅
.envfile support - ✅ Type-safe settings with validation
- Rust 1.70+ (2021 edition)
- PostgreSQL 14+
- Kafka (optional, if using messaging)
- Docker & Docker Compose (for local development)
-
Clone and configure environment:
cp .env.example .env # Edit .env with your configuration -
Update project metadata:
- Edit
Cargo.tomlto change thenamefield to your project name - Update version and authors
- Edit
-
Create your database:
createdb your_database_name # Update DATABASE_URL in .env -
Add your migrations (optional):
mkdir migrations # Create your SQL migration files: # migrations/V001__initial_schema.sql # migrations/V002__your_feature.sql
-
Build and run:
cargo build cargo run
This is a template - you need to add your business logic. Here's how:
-
Define your entities in
src/domain/entities/- Create domain objects with identity and business rules
-
Create DTOs in
src/application/dtos/- Define data transfer objects for API boundaries
-
Define ports in
src/application/ports/- Create trait interfaces for external dependencies
-
Implement adapters in
src/infrastructure/- Implement your ports with concrete technology choices
-
Add Kafka topics in configuration
- Configure producers/consumers for your messaging needs
-
Implement repositories using the base
Repository<T>trait- Add database access for your entities
See docs/GETTING_STARTED.md for detailed step-by-step guide.
Configuration is loaded from:
- Environment variables
.envfile- Config files (using config crate)
Key configuration sections:
app: Application settings (name, port, etc.)database: PostgreSQL connection and poolingkafka_producers: Kafka producer configurationskafka_consumers: Kafka consumer configurations
See .env.example for all available options.
# Run tests
cargo test
# Run with debug logs
RUST_LOG=debug cargo run
# Format code
cargo fmt
# Lint
cargo clippy
# Check without building
cargo check# Build production image
docker build -t project-struct-base .
# Run with docker-compose (includes PostgreSQL, Kafka, etc.)
docker-compose up
# Development mode with hot reload
docker-compose -f docker-compose.dev.yml up- Getting Started Guide - Step-by-step tutorial
- Architecture Guide - Detailed architecture documentation
- Project Map - Structured YAML documentation
This template includes production-ready implementations:
- ✅ Generic Kafka Producer/Consumer - Type-safe messaging with pluggable serialization
- ✅ Repository Pattern - Base
Repository<T>trait with PostgreSQL implementation - ✅ Database Bootstrap - Connection pooling with automatic retry logic
- ✅ Graceful Shutdown - Coordinated resource cleanup on termination
- ✅ Structured Logging - JSON logs with tracing for observability
- ✅ Configuration Management - Environment-based settings with validation
- ✅ Health Checks - Startup health summary for all components
- ✅ Event Store - In-memory event store (ready for persistent implementation)
- Hexagonal Architecture - Clean separation of business logic and infrastructure
- Ports & Adapters - Interface-based dependency inversion
- Repository Pattern - Abstract data access
- CQRS - Command/Query separation
- Event Sourcing - Event store foundation
- Generic Programming - Type-safe, reusable components
- Read
docs/GETTING_STARTED.mdfor a detailed tutorial - Define your domain entities and value objects
- Create application ports for your use cases
- Implement infrastructure adapters
- Add your business logic
- Write tests
- Deploy!
[Your License Here]
Note: This is a generic template. All domain-specific code has been removed. You need to add your business logic to make it functional for your specific use case.