Example Go codebase based on the main objective of Hexagonal Architecture: isolate business logic from external dependencies (in each bounded context or feature).
├── cmd/
│ └── main/
│ └── main.go
├── commons.go <-- root as shared package
├── config.go
├── compose/ <-- manual wiring dependencies
│ └── services.go
├── user/ <-- user (feature)
│ ├── user.go <-- domain models, entities
│ ├── service.go <-- service layer
│ ├── repo.go <-- repository layer
│ ├── user_test.go
│ └── service_test.go
├── billing/ <-- billing (feature)
│ ├── invoice.go
│ ├── service.go
│ ├── repo.go
│ └── service_test.go
├── store/ <-- store (feature)
├── rest/ <-- http restfull server (infra)
│ ├── jwt/
│ │ ├── claims.go
│ │ └── claims_test.go
│ ├── router.go
│ ├── billing.go <-- handlers, dtos
│ ├── store.go <-- handlers, dtos
│ ├── user.go <-- handlers, dtos
│ └── ...
├── grpc/ <-- grpc server (infra) [TODO]
├── pgsql/ <-- postgres storage (infra)
│ ├── sqlc/
│ │ ├── dbgen/
│ │ │ ├── customer.sql.go
│ │ │ ├── db.go
│ │ │ ├── models.go
│ │ │ └── ...
│ │ ├── queries/
│ │ │ ├── product.sql
│ │ │ ├── user.sql
│ │ │ └── ...
│ │ ├── sqlc.go
│ │ └── sqlc_test.go
│ ├── utils.go
│ ├── filter.go
│ └── filter_test.go
├── logger/ <-- more dependency (infra)
├── test/ <-- integration tests
│ ├── sqlc/
│ │ ├── sqlc_test.go
│ │ ├── userrepo_test.go
│ │ └── ...
│ └── utils.go
├── sqlc.yaml
└── Taskfile.yml
-
Package by feature based, also called by domain, bounded context or Vertical Slice.
-
We use domain models as an example of design. Using domain models is not necessary if you are doing a small project, instead use the persistence models generated by
sqlc
library. -
We did not use ports (interfaces) of Hexagonal Architecture because in Go it's not idiomatic have fat interfaces (with many methods), nor were small interfaces possible.
-
There are three library implementations for working with Postgres:
pgx
andsqlc
. For now, the project usespgx
, but it will be switched tosqlc+pgx
.
$ git clone https://github.com/adrianolmedo/genesis.git
$ cp .env.example .env
$ docker-compose up -d --build postgres
Up application service:
$ task genrsa
$ task swagger
$ docker-compose up -d --build app