viettranx / micro-clean-architecture-service-demo

A demo microservice with Clean Architecture in practice
260 stars 90 forks source link
clean-architecture go golang grpc grpc-go microservices rest-api

Clean Architecture in Microservices Demo

Introduction

If you're very new to Clean Architecture, I recommend you check more simple demo.

This repo was built up with three services: Task, User and Auth. Each of them conforms Clean Architecture.

Clean Architecture Overview Clean Architecture Flow

Folder structure

Let's focus on directories in /services:

services
├── auth
│   ├── business
│   ├── entity
│   ├── repository
│   │   ├── mysql
│   │   └── rpc
│   └── transport
│       ├── api
│       └── rpc
├── task
│   ├── business
│   ├── entity
│   ├── repository
│   │   ├── mysql
│   │   └── rpc
│   └── transport
│       └── api
└── user
    ├── business
    ├── entity
    ├── repository
    │   └── mysql
    └── transport
        ├── api
        └── rpc

Protobuf and generated files at proto folder:

├── auth.proto
├── pb
│   ├── auth.pb.go
│   ├── auth_grpc.pb.go
│   ├── user.pb.go
│   └── user_grpc.pb.go
└── user.proto

Microservices in this demo

Instead of separating to 3 repos on GitHub, I merged them to a single repo to give more transparent and convenience to build up the demo. But the services are isolated with each others.

Service stacks:

Task Service

User Service

Auth Service

The Diagrams

Some of main diagrams to demonstration how they work together:

Create Task

Create Task Diagram

Fetch Tasks

Fetch Task Diagram

How to run this demo

1. Clone and start the service with Docker-Compose

Open your terminal/console:

git clone https://github.com/viettranx/microservices-clean-architecture-demo
cd microservices-clean-architecture-demo

docker compose up --force-recreate --detach --build app

If everything is ok, the service will be running at on localhost:3000

Print all ENV variables:

docker compose exec app ./demo_app outenv

2. Make some requests

curl --location 'http://localhost:3000/v1/register' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "demo@demo.com",
    "password": "12345678",
    "last_name": "Microservices",
    "first_name": "Demo "
}'
{"data": true}
curl --location 'http://localhost:3000/v1/authenticate' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "demo@demo.com",
    "password": "12345678"
}'

The access token will return like this

{
  "data": {
    "access_token": {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJlNTMycW9zOGpqTTIiLCJleHAiOjE2ODAyNTI0MDgsIm5iZiI6MTY3OTY0NzYwOCwiaWF0IjoxNjc5NjQ3NjA4LCJqdGkiOiI3OTEzYzhjYy05NmI0LTQ3ZmUtOWIzZi01MTUwZTk5NTM3MGUifQ.51d6zVuGtcAbw2poEWV4TffhEqJG8uxMOcGq7Mt8sZA",
      "expire_in": 604800
    }
  }
}

New Task id will be returned

{"data":"e532sJ4XpCi8"}

Conclusion

Building services with Clean Architecture is difficult, especially in Microservices. I hope this repo help you. Enjoy it and feel free to create PRs or Issues.