This repository serves as a template and documentation on implementing Hexagonal Architecture within a golang application
The idea of Hexagonal Architecture is to put inputs and outputs at the edges of our design. Business logic should not depend on whether we expose a REST or a GraphQL API, and it should not depend on where we get data from — a database, a microservice API exposed via gRPC or REST, or just a simple CSV file
The following diagrams the project structure
.
├── cmd
└── internal
├── adapters
│ ├── primary
│ └── secondary
├── core
│ ├── domain
│ ├── entity
│ ├── service
│ └── valobj
└── ports
The cmd directory contains your applications main modules. The main.go file will be responsible for setting up dependencies and running the application
The adapters directory contains the primary and secondary modules that handle communication between external entities and the core of the application. Adapters will be responsible for any data tranformations required for communication, as well as error handling, telemetry, and logging
The core directory contains the applications business logic and will utilize a lite version of Domain-Driven Design
There are four main concepts that define the core: Domains, Entities, Services, and Value Objects
Domainscan be thought of as "orchestrators" for domain use cases. They orchestrate services to implement business rules and validation logic specific to a domainEntitiesare the domain objectsServicesperform tasks specific to an entity and handle multiple data sourcesValue Objectsrepresent shared immutable data types
The ports directory contains the port definitions that define how the core will interact with internal and external entities. It is split up into the following files:
core.go- Contains interfaces that define how services and domains can interact with each other
primary.go- Contains interfaces that define how the core allows external entities to interact with it. Ex.
APIPortdefines how the core will allow APIs to interact with it
- Contains interfaces that define how the core allows external entities to interact with it. Ex.
secondary.go- Contains interfaces that define how the core wants to drive/interact with external entities. These entities are usually databases or some other data source, but can also be other internal applications or even a library
A command-line tool for Tmux setup
An example using a monorepo that contains a websocket game server and a http web server (WIP)