Skip to content

Hexagonal architecture through a Go-based project demonstration

License

Notifications You must be signed in to change notification settings

yinebebt/hexagonal-architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hexagonal-architecture

build-workflow

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.

Adapters

Handler

  • REST API - GIN
  • gRPC
  • WebSocket
  • GraphQL

Repository

  • Sqlite
  • Postgres

Project Structure

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

Architecture Principles

  • 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

Installation

# 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 ./...

Running the Application

# 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.go

Testing

Run all tests:

go test -v -race ./...

Sponsor this project

 

Languages