How dyhalmeida/go-clean-arch works
Project type and purpose
• This is a backend “Orders” microservice written in Go, demonstrating Clean Architecture.
• It exposes three protocols (HTTP REST, GraphQL, gRPC), persists orders in MySQL, publishes domain events over RabbitMQ, and is wired via Google Wire in cmd/orders.
File structure
• cmd/orders: application entrypoint, DI setup (wire.go), environment (.env) files.
• configs: environment‐variable loaders.
• internal/entity: core domain model (Order).
• internal/usecase: application/business logic (CreateOrder, ListOrder).
• internal/infra: adapter implementations
• database (MySQL repository + migrations)
• web (HTTP handlers + webserver starter)
• graph (gqlgen GraphQL server, schema, resolvers)
• grpc (proto definitions, generated pb, gRPC service implementation)
• internal/event: domain event definitions and handlers triggered after order creation.
• pkg/events: generic event dispatcher interface and RabbitMQ implementation.
README for additional insights
• Confirms use of Docker‐Compose (MySQL, RabbitMQ), Go 1.21+, GraphQL playground, gRPC via Evans CLI.
• Emphasizes Clean Architecture layered boundaries.
a. Main components
• External clients: REST clients (curl/Postman), GraphQL clients (Playground), gRPC clients (Evans).
• API Adapters:
• HTTP REST adapter (internal/infra/web)
• GraphQL adapter (internal/infra/graph)
• gRPC adapter (internal/infra/grpc)
• Application Core:
• Use Case layer (internal/usecase)
• Domain Entities (internal/entity)
• Infrastructure:
• Order Repository / MySQL (internal/infra/database)
• Event Dispatcher / RabbitMQ (pkg/events)
• Event Handlers (internal/event/handler)
• Composition Root / DI (cmd/orders, configs)
b. Relationships & data flow
1. Client → Adapter → Use Case → Entity → Repository → MySQL
2. Use Case emits OrderCreated event → Event Dispatcher → RabbitMQ
3. RabbitMQ → Event Handler → (optional follow‐on actions)
4. All dependencies are wired at startup by the DI container in cmd/orders.
c. Architectural patterns & design principles
• Clean/Onion Architecture: concentric layers with inward dependencies only.
• Hexagonal Ports & Adapters: adapters for HTTP, GraphQL, gRPC, DB, and message bus.
• Dependency Injection via Wire.
• Single Responsibility & Interface Segregation: each adapter, use case, and repository has a single, focused interface.
d. Key technologies & libraries
• Go (lang)
• Gin/HTTP (or net/http) for REST
• gqlgen for GraphQL
• gRPC + Protobuf
• MySQL (RDBMS)
• RabbitMQ (message broker)
• Docker & Docker Compose