Hexagonal architecture is a design pattern suitable for building scalable and complex projects. This repository serves as a demonstration of the principles of Hexagonal Architecture in a Go project.
The goal of this project is to provide a straightforward example that developers can use to understand and apply Hexagonal Architecture in their own projects. By following the structure and patterns demonstrated here, developers can build scalable and maintainable systems with ease.
In this demo, the core business functionality revolves around managing user and video entities. Administrators have the capability to manage videos, while users are provided with access to view the available videos.
Explore the concept of Hexagonal Architecture further in Hexagonal-architecture.
- REST API - GIN
- gRPC
- WebSocket
- GraphQL
- Sqlite
- Postgres
hexagonal-architecture/
├── cmd/
│ └── main.go # Application entry point
├── docs/ # Swagger documentation
├── internal/
│ ├── adapter/ # External adapters (driving & driven)
│ │ ├── repository/ # Repository adapters (driven/outbound)
│ │ │ ├── sqlite/ # SQLite implementation
│ │ │ └── postgres/ # PostgreSQL implementation
│ │ ├── rest/ # REST adapter (driving/inbound)
│ │ │ ├── handler.go # Handler implementation & routes
│ │ │ └── middleware.go # HTTP middleware
│ │ └── templates/ # HTML templates
│ └── core/ # Business logic (hexagon)
│ ├── entity/ # Domain entities
│ ├── port/ # Ports (interfaces)
│ │ ├── handler.go # port.VideoHandler interface
│ │ └── repository.go # port.VideoRepository interface
│ └── service/ # Business services
├── go.mod
├── go.sum
└── README.md- Core (Domain): Contains business logic, entities, and ports (interfaces)
- Adapters: Implement ports for external concerns (HTTP, databases)
- Dependency Inversion: Core depends on abstractions (ports), not implementations
- Clean Separation: Business logic is independent of frameworks and databases
# Clone the repository
git clone https://github.com/yinebebt/hexagonal-architecture.git
cd hexagonal-architecture
# Install dependencies
go mod download
# Build the application
go build ./cmd/main.go
# Run tests
go test ./...# Using SQLite (default)
go run ./cmd/main.go -dbtype=sqlite -dsn=app.db
# Using PostgreSQL
go run ./cmd/main.go -dbtype=postgres -dsn="postgres://user:password@localhost/dbname"
# Set custom port
PORT=8080 go run ./cmd/main.goRun all tests:
go test -v -race ./...