This repo aims to implement the Clean Architecture in Go by an library example. This project is inspired by the go-clean-arch and go-backend-clean-architecture. The implementation includes the following features:
- Domain Driven Design (DDD)
- Dependency Injection (DI)
- Repository Pattern (RP)
- Database Migration (DM)
- API Documentation (AD)
- The system is a library with 3 roles: Director, Librarian, and Visitor.
- Everyone can find all the books in the library.
- registered visitors can borrow books, return books, and view their own borrowing records.
- librarians can update the information of books and have all the privileges of registered visitors.
- The director has full access to the librarians and can delete books. Deleting a book is a soft delete, so it does not affect the borrowing record.
List third party packages with readme tables in this project
Package | Description |
---|---|
gorm | The fantastic ORM library for Golang, aims to be developer friendly |
gorm/gen | GORM GEN is a type-safe gorm code generator. |
gin | Gin is a HTTP web framework written in Go (Golang) |
go-redis | Redis client for golang |
air | Live reload for Go apps written in Go |
We suggest you to follow the steps below to develop with Unix-like OS
, the whole project is developed and tested on Ubuntu 22.04
.
Visual Studio Code
is recommended as the IDE for this project, you can install the following extensions to make your development easier:
The following tools are started by docker
in the development environment, but you can install them on your machine if you want to:
- DBeaver is recommended as the database management tool.
- RedisInsight is recommended as the redis management tool.
Here is the steps to start the development:
-
Clone the repo
git clone https://github.com/alan890104/go-clean-architecture.git
-
Make sure you have installed
make
,go
anddocker-compose / docker compose
on your machine -
Copy
.env.example
to.env.dev
and modify the environment variables if neededcp .env.example .env.dev
-
Run
make install
to install the toolsRun only once when you start the development or the tools required to be updated
-
Run
make dev-up
to start the development database and redis -
Run
make dev-migrate
to migrate the database -
Run
make generate
to generate the gorm/gen queries -
Run
make serve
to start the development server with live reload- Webserver will be listening on localhost:8000, you may change the port in
.env.dev
- Swagger UI will be listening on localhost:8080
- RedisInsight will be listening on localhost:8001
- DBeaver will be listening on localhost:8978
- Webserver will be listening on localhost:8000, you may change the port in
-
Run
make dev-down
to stop the development database and redis
We suggest the iteration steps for the development of this project as follows:
- Have a API spec in openapi/grpc format (requires a lot of discussion)
- Define the domain model
- Define the repository interface
- Define the usecase interface
- Implement the controller
- Implement the routes
- Implement
cmd/app/app.go
- Implement the usecase
- Implement the repository