Automated code generator for Hexagonal Architecture with Domain-Driven Design principles
Hexagonal Architecture Generator is a powerful CLI tool that automatically generates complete domain-driven microservices with Hexagonal Architecture. Stop writing boilerplate code and focus on business logic!
Building microservices with proper architecture requires:
- β Domain layer with entities and value objects
- β Application layer with services and use cases
- β Infrastructure layer with adapters
- β API layer with controllers and DTOs
- β Event-driven architecture with domain events
- β Dependency Injection configuration
- β Database models and repositories
- β Testing structure
This generator creates all of the above automatically!
- Python 3.9+
- Docker & Docker Compose
- RabbitMQ (optional, for event bus)
# Clone repository
git clone https://github.com/polyerter/hexacore.git
cd hexacore
# Install dependencies
pip install -r requirements.txt
# Run with Docker
docker-compose up -d# Generate a Product entity with full CRUD API
python generator.py add-entity product --with-crud
# Generate with Event-Driven capabilities
python generator.py add-entity order --with-crud --with-eventsapp/
βββ core/ # Domain Layer
β βββ domain/
β β βββ entities/ # Domain Entities
β β βββ interfaces/ # Ports (Repository, EventBus)
β β βββ events/ # Domain Events
β βββ services/ # Domain Services
β βββ dtos/ # Data Transfer Objects
β βββ di/ # Dependency Injection
βββ adapters/
β βββ primary/ # Driving Adapters
β β βββ api/v1/ # REST API Controllers
β βββ secondary/ # Driven Adapters
β βββ database/ # Database Models & Repositories
β βββ messaging/ # Message Bus & Event Handlers
βββ main.py # FastAPI Application
- Domain Entities with dataclasses and business logic
- Repository Pattern with interface and implementation
- Domain Services with use case orchestration
- DTO Layers for API request/response models
- Dependency Injection with auto-wiring
- Domain Events for business process modeling
- Event Bus with RabbitMQ integration
- Event Handlers for side effects
- Configurable Routing with multiple exchanges
- SQLAlchemy Models with automatic generation
- Async Repository implementations
- Caching Layer with Redis integration
- Migrations Ready structure
- FastAPI Routes with automatic OpenAPI docs
- RESTful Endpoints with proper HTTP verbs
- Validation with Pydantic models
- Error Handling with proper status codes
- Docker Support with multi-stage builds
- Docker Compose for development
- Worker Daemons for event processing
- Health Checks and monitoring
# Generate a simple entity
python generator.py add-entity product
# Generate with full CRUD operations
python generator.py add-entity customer --with-crud
# Remove an entity
python generator.py remove-entity product# Generate with domain events and RabbitMQ
python generator.py add-entity order --with-crud --with-events
@dataclass
class Product:
id: Optional[str] = None
name: Optional[str] = None
price: Optional[float] = None
created_at: Optional[datetime] = None
def __post_init__(self):
if self.created_at is None:
self.created_at = datetime.utcnow()@dataclass
class ProductCreatedEvent(DomainEvent):
product_id: str
name: str
price: float
@property
def event_type(self) -> str:
return "PRODUCT_CREATED"@router.post("/", response_model=ProductResponseDTO)
async def create_product(
product_data: ProductCreateDTO,
service: ProductService = Depends(create_product_service)
):
return await service.create_product(product_data)To apply migrations for the service project using Docker Compose, follow these steps:
- Open a terminal.
- Navigate to the root directory of your project where the
docker-compose.ymlfile is located. - Execute the following commands inside the web container:
docker compose exec web bashThis will open a Bash shell inside the web container.
- Once inside the container, run the Alembic revision and upgrade commands:
alembic revision --autogenerate -m "Add_new_model"
alembic upgrade headThese commands will generate a new migration based on the current state of your models and apply all migrations to your database.
- Exit the container by typing
exit.
# Database
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/db
# RabbitMQ
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
# Application
API_V1_STR=/api/v1
DEBUG=true{
"workers": [
{
"queue": "email_notifications",
"exchange": "notifications",
"routing_key": "notifications.email.*",
"worker_module": "app.workers.email_worker",
"worker_class": "EmailWorker",
"concurrency": 2
}
]
}docker-compose up -d
docker-compose logs -f worker_daemon- API Health:
GET /health - RabbitMQ Status: Management UI on port 15672
- Metrics: Prometheus metrics endpoint
- Logging: Structured JSON logging
hexacore/
βββ generator.py # Main generator
βββ app/ # Generated application
βββ app/utils/ # Generator utilities
βββ app/tests/ # Test suite
βββ docker # Docker files
βββ migrations # Migrations
βββ worker_daemon # Point for the Worker Daemon
βββ docker-compose.yml # Development environment
- New Database: Add adapter in
adapters/secondary/ - New Message Broker: Implement
MessageBusinterface - New API Protocol: Add primary adapter
This project is licensed under the MIT License - see the LICENSE file for details.