This is an opinionated Serverless Framework template that can be used to bootstrap a Go project to use a hexagonal architecture.
It's designed primarily for building microservices around lambdas.
- Implements a hexagonal layout, abstracting lambdas/storage from domain logic
- Zerolog included for contextual JSON logging
- Wire included to manage dependency injection
You can setup a new serverless project with this template with:
sls create --path {SERVICE_NAME} --template-url https://github.com/tomkdickinson/serverless-hexagonal-go/tree/main
Then run make gomodgen MODULE={MODULE_NAME}
(or make gomodgen
to auto generate module based on parent and folder name) to
generate the go.mod and go.sum files.
This template also uses Wire for the examples, although you can easily be taken out if not needed.
The example provided with this template is a simple Blog endpoint, where we want to find either a list of posts, or a specific post by its slug.
The cmd
folder contains main applications for each lambda we declare in serverless.yml, including
wiring of layers using Wire.
internal/lambda contains the actual lambda implementations for each http request.
internal/blog contains the domain logic for interacting with the blog.
internal/storage contains a simple memory
implementation of the blogs repository, to load posts.