Skip to content

0xirvan/hexagonal-architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

57 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Hexagonal Architecture Template

Go Version Echo Wire

A Go application template with Hexagonal Architecture (Ports & Adapters)

πŸ—οΈ Architecture

This project uses Hexagonal Architecture (also known as Ports & Adapters Pattern) which separates business logic from external dependencies.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Delivery Layer                       β”‚
β”‚         (HTTP Handlers, Routes, Middleware)             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Core Domain                           β”‚
β”‚          (Business Logic, Use Cases, Entities)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 Persistence Layer                       β”‚
β”‚          (Repositories, Database, Storage)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Layer Explanation

1. Core Domain (internal/core/)

Contains business logic that is independent from frameworks and external dependencies.

  • Domain: Entities and business rules
  • Port: Interfaces for repositories and services
  • UseCase: Business logic implementation

2. Adapter (internal/adapter/)

Concrete implementations of ports that interact with the outside world.

  • Delivery: HTTP handlers, routes, middleware
  • Persistence: Database implementation (inmemory, sqlite, mysql, etc.)
  • Config: Application configuration

3. App (internal/app/)

Application composition using Wire for dependency injection.

πŸ“ Struktur Project

server/
β”œβ”€β”€ cmd/
β”‚   └── server/
β”‚       └── main.go                    # Application entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ adapter/                       # Adapter Layer
β”‚   β”‚   β”œβ”€β”€ config/                    # Configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ app.go
β”‚   β”‚   β”‚   β”œβ”€β”€ config.go
β”‚   β”‚   β”‚   └── http.go
β”‚   β”‚   β”œβ”€β”€ delivery/                  # Delivery adapters
β”‚   β”‚   β”‚   └── http/
β”‚   β”‚   β”‚       β”œβ”€β”€ router.go          # HTTP router setup
β”‚   β”‚   β”‚       β”œβ”€β”€ todo_handler.go    # Todo HTTP handlers
β”‚   β”‚   β”‚       β”œβ”€β”€ todo_routes.go     # Todo routes registration
β”‚   β”‚   β”‚       β”œβ”€β”€ validator.go       # Request validation
β”‚   β”‚   β”‚       β”œβ”€β”€ dto/               # Data Transfer Objects
β”‚   β”‚   β”‚       β”œβ”€β”€ helper/            # HTTP helpers
β”‚   β”‚   β”‚       └── middleware/        # HTTP middleware
β”‚   β”‚   └── persistence/               # Persistence adapters
β”‚   β”‚       β”œβ”€β”€ inmemory/              # In-memory repository
β”‚   β”‚       β”‚   └── todo_repository.go
β”‚   β”‚       └── sqlite/                # SQLite repository (future)
β”‚   β”œβ”€β”€ app/                           # Application composition
β”‚   β”‚   β”œβ”€β”€ http_app.go                # HTTP application
β”‚   β”‚   β”œβ”€β”€ wire.go                    # Wire providers
β”‚   β”‚   └── wire_gen.go                # Wire generated code
β”‚   β”œβ”€β”€ core/                          # Core Domain Layer
β”‚   β”‚   β”œβ”€β”€ domain/                    # Domain entities
β”‚   β”‚   β”‚   β”œβ”€β”€ errors.go              # Domain errors
β”‚   β”‚   β”‚   └── todo.go                # Todo entity
β”‚   β”‚   β”œβ”€β”€ port/                      # Ports (interfaces)
β”‚   β”‚   β”‚   β”œβ”€β”€ todo_repository.go     # Repository interface
β”‚   β”‚   β”‚   └── todo_service.go        # Service interface
β”‚   β”‚   └── usecase/                   # Use cases
β”‚   β”‚       └── todo/
β”‚   β”‚           β”œβ”€β”€ service.go         # Todo service implementation
β”‚   β”‚           β”œβ”€β”€ create.go
β”‚   β”‚           β”œβ”€β”€ delete.go
β”‚   β”‚           β”œβ”€β”€ get.go
β”‚   β”‚           β”œβ”€β”€ list.go
β”‚   β”‚           β”œβ”€β”€ list_paginated.go
β”‚   β”‚           β”œβ”€β”€ mark_done.go
β”‚   β”‚           β”œβ”€β”€ mark_undone.go
β”‚   β”‚           └── update.go
β”‚   └── shared/                        # Shared utilities
β”‚       └── ptr/
β”‚           └── ptr.go                 # Pointer helpers
β”œβ”€β”€ go.mod                             # Go module definition
β”œβ”€β”€ go.sum                             # Go dependencies
β”œβ”€β”€ README.md                          # This file
└── .env.example                       # env example

πŸ”„ Architecture Flow

Request β†’ Handler β†’ Service Interface β†’ Use Case β†’ Repository Interface β†’ Storage
   ↓          ↓            ↓                 ↓              ↓                 ↓
 Echo     TodoHandler  TodoService      Service Impl   TodoRepository    InMemory
(Adapter)  (Adapter)    (Port)          (Core)          (Port)          (Adapter)

Key Principle: Adapters depend on Core, Core never depends on Adapters.

πŸ› οΈ Technologies

  • Go 1.21+ - Programming language
  • Echo v4 - High performance HTTP framework
  • Wire - Compile-time dependency injection
  • Validator - Struct and field validation

πŸ“¦ Prerequisites

  • Go 1.21 or higher
  • Wire (for dependency injection)

Install Wire:

go install github.com/google/wire/cmd/wire@latest

πŸš€ Setup & Installation

1. Clone Repository

git clone https://github.com/0xirvan/hexagonal-architecture.git
cd hexagonal-architecture/server

2. Update Module Name (Optional)

If you want to use your own module name:

# Update go.mod
go mod edit -module github.com/yourusername/yourproject/server

# Update all imports
find . -type f -name "*.go" -exec sed -i 's|github.com/0xirvan/hexagonal-architecture/server|github.com/yourusername/yourproject/server|g' {} +

# Tidy dependencies
go mod tidy

3. Install Dependencies

go mod download

4. Generate Wire Dependencies

wire ./internal/app

5. Configure Environment

cp .env.example .env

6. Run Application

go run cmd/server/main.go

Server will run on http://localhost:8080

Patterns Used

  • Hexagonal Architecture - Core isolation from external dependencies
  • Repository Pattern - Data access abstraction
  • Service Layer Pattern - Business logic encapsulation with interface
  • Dependency Injection - Loose coupling via Wire
  • DTO Pattern - Data transfer between layers
  • Port & Adapter Pattern - Interface-based boundaries

πŸ”„ Switching Persistence

To switch from in-memory to SQLite:

  1. Implement SQLite repository in internal/adapter/persistence/sqlite/
  2. Update Wire in internal/app/wire.go:
var repositorySet = wire.NewSet(
    sqlite.NewTodoRepository,  // Change from inmemory
)
  1. Regenerate Wire: wire ./internal/app

πŸ“š Resources

🀝 Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

β€πŸ’» Author

0xirvan

πŸ™ Acknowledgments

  • Hexagonal Architecture concept by Alistair Cockburn
  • Built with ❀️ using Go and Echo framework
  • Special thanks to the Go community for excellent tools and libraries

⭐ If you find this project helpful, please consider giving it a star!

About

Golang Hexagonal Architecture Template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages