Go language server sample code with Clean Architecture-like configuration (using echo, wire)

Introduction

Let's implement Go's REST API Server with a focus on the Clean Architecture-like package structure. In addition, Go's web framework echo and DI code generator wire are incorporated.

excuse

With this configuration, there is no actual operation record, so I first thought about it as a starting point. There seems to be room for improvement.

Implementation repository

img

Directory structure

├── cmd
│   └── server
├── db
├── domain
│   ├── model
│   └── repository
├── handler
├── infra
│   └── mysql
├── response
├── router
└── usecase

cmd

The main file for starting sever.

db

Connection information to db. Migration information may also be placed here. It may be placed in infra.

domain/model

The domain logic of a single entity is described here.

domain/repository

Persistence information interface.

handler

Handler county called from echo endpoint processing. Here, the configuration was set to Dependeny Injection using wire.

infra

Implementation details of persistence processing.

response

Determine the response format here according to your requirements.

router

Aggregate the routing endpoint definitions for echo.

usecase

Describe application logic other than domain logic here.

Points to consider when implementing further

Where to put the DB transaction?

I feel that it fits well enough to start in the use case and carry around the DB session as an argument. (Although the persistence implementation details are not contained in infra,)

Are the layers separated?

If you want to do it more strictly, you can prepare a separate DB tag in the structure with infra and convert it to each other with model, but if possible, I would like to carry around the model struct as it is and reduce the number of steps.

What if you do table join with sql?

Should I prepare a nested structure in domain / service and receive the query result in infra?

type (
	Joined struct {
		Example
		Example2
	}
)

reference

Clean Architecture Software structure and design learned from masters https://github.com/golang-standards/project-layout https://future-architect.github.io/articles/20200528/

Recommended Posts

Go language server sample code with Clean Architecture-like configuration (using echo, wire)
Create CRUD apps with Go with Mysql, GORM, Echo, Clean Architecture