Skip to content

polyerter/hexacore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—οΈ Hexagonal Architecture Generator

Automated code generator for Hexagonal Architecture with Domain-Driven Design principles

πŸ“– Overview

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!

🎯 What Problem Does It Solve?

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!

πŸš€ Quick Start

Prerequisites

  • Python 3.9+
  • Docker & Docker Compose
  • RabbitMQ (optional, for event bus)

Installation

# 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 Your First Microservice

# 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-events

πŸ—οΈ Generated Architecture

app/
β”œβ”€β”€ 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

✨ Features

🎯 Core Generation

  • 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

πŸ”„ Event-Driven Architecture

  • Domain Events for business process modeling
  • Event Bus with RabbitMQ integration
  • Event Handlers for side effects
  • Configurable Routing with multiple exchanges

πŸ—„οΈ Database Support

  • SQLAlchemy Models with automatic generation
  • Async Repository implementations
  • Caching Layer with Redis integration
  • Migrations Ready structure

πŸ”Œ API Generation

  • FastAPI Routes with automatic OpenAPI docs
  • RESTful Endpoints with proper HTTP verbs
  • Validation with Pydantic models
  • Error Handling with proper status codes

🐳 Containerization

  • Docker Support with multi-stage builds
  • Docker Compose for development
  • Worker Daemons for event processing
  • Health Checks and monitoring

πŸ“š Usage Examples

Basic Entity Generation

# 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

Event-Driven Generation

# Generate with domain events and RabbitMQ
python generator.py add-entity order --with-crud --with-events

πŸŽͺ Generated Code Examples

Domain Entity

@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()

Domain Event

@dataclass
class ProductCreatedEvent(DomainEvent):
    product_id: str
    name: str
    price: float

    @property
    def event_type(self) -> str:
        return "PRODUCT_CREATED"

REST API Controller

@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)

Apply migrations

To apply migrations for the service project using Docker Compose, follow these steps:

  1. Open a terminal.
  2. Navigate to the root directory of your project where the docker-compose.yml file is located.
  3. Execute the following commands inside the web container:
docker compose exec web bash

This will open a Bash shell inside the web container.

  1. Once inside the container, run the Alembic revision and upgrade commands:
alembic revision --autogenerate -m "Add_new_model"
alembic upgrade head

These commands will generate a new migration based on the current state of your models and apply all migrations to your database.

  1. Exit the container by typing exit.

πŸ”§ Configuration

Environment Variables

# 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

Worker Configuration

{
  "workers": [
    {
      "queue": "email_notifications",
      "exchange": "notifications",
      "routing_key": "notifications.email.*",
      "worker_module": "app.workers.email_worker",
      "worker_class": "EmailWorker",
      "concurrency": 2
    }
  ]
}

🐳 Docker Deployment

Development

docker-compose up -d
docker-compose logs -f worker_daemon

πŸ“Š Monitoring & Health

  • API Health: GET /health
  • RabbitMQ Status: Management UI on port 15672
  • Metrics: Prometheus metrics endpoint
  • Logging: Structured JSON logging

πŸ› οΈ Development

Project Structure

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

Extending Functionality

  • New Database: Add adapter in adapters/secondary/
  • New Message Broker: Implement MessageBus interface
  • New API Protocol: Add primary adapter

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages