Scalable Golang Microservices
Requirements
- Golang - 1.15 or higher recommended
- Mysql - 8.0 or higher
- Swag cli - For generating swagger docs
- Mockery - For generating mock classes for testing
- Docker and Docker-compose for the containerized setup - Not mandatory
Install Swag and Mockery
go get -u github.com/swaggo/swag/cmd/swag
go get -u github.com/vektra/mockery/cmd/mockery
Featues
- Multi tenancy support
- Scalable folder structure
- Config using .env
- Database migration
- GORM Integration
- Dependency Injection
- Swagger docs
- Separate handler, service, repository(repo), model
- Multi language support
- Json logger
- Makefile for commands
- Mock object integration
- Unit Test
- Integration Test
- Standard request and response and errors
- Common form validation
- Health endpoint
- Krakend Gateway integration
- Common error messages
- Docker
- Docker Compose
- Share library across service
- CRUD with pagination support
- Kubernetes
Overview
Gateway
Which act as a singe entrypoint for all the services
- Handle logs,trace,metrics collection
- Handle Authentication
- Handle ratelimit, Circuit breaker and many more
Common Library
- Common functionality shared accross the microserices , Refer micro-common
Account Service
- Manage tenants, users, authentication and authrorization
Product Service
- Manage product catalog. Demo purpose
Running Application
Go to the folders ./gateway
./account-service
./product-service
and follow the readme.md files
Eg: Build Account microservice
cd account-service
Setup packages locally
go mod vendor
Change the config in .env for database and migrate the tables
make migrate-up
Generate API document to the ./doc folder using swag cli
make doc
Swagger docs will be available at /swagger/index.html of the api path
Run service
make run
or
go run cmd/api/main.go
Generate mock files
make mock
Test service
make test
Swagger
- Access the url http://localhost:8082/swagger/index.html
- or through gateway once krakend is started http://localhost:8080/account/swagger/index.html
Same steps can be followed for product-service. For gateway steps are different. Please refere readme.md for gateway
Useful go commands
go clean --modcache # Clean the mod cache
go mod vendor # Initilize vendor folder locally
go mod download #Download missing packages
Docker Compose setup
If Docker and Docker compose installed. Run below command from root directory
sudo docker-compose up
Create an account that can be used for /adminLogin api for token generation. Refer account service swagger.
curl -X POST "https://localhost:8080/account/v1/tenantRegister" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"domain\": \"eBook\", \"email\": \"tenant1@mail.com\", \"firstName\": \"John\", \"lastName\": \"Doe\", \"name\": \"Tenant1\", \"password\": \"Pass@1\"}"
Can use the same user email and password for /adminLogin api
Folder Structure
├── app # App Initialization
│ ├── app.go
│ └── database
│ ├── db
│ │ └── db.go
│ └── gorm
│ └── gorm.go
├── cmd # Starting point for any application
│ ├── api
│ │ └── main.go # Main application start
│ └── migrate
│ └── main.go # Migration start
├── config
│ └── config.go # App configurations
├── deploy
│ ├── bin
│ │ └── init.sh
│ ├── Dockerfile
│ └── prod.Dockerfile
├── doc # Swagger doc - Autogenerated
│ ├── docs.go
│ ├── swagdto
│ │ └── error.go # Common errors used for swagger - Custom
│ ├── swagger.json
│ └── swagger.yaml
├── go.mod
├── go.sum
├── locale # Language files
│ ├── el-GR
│ │ └── language.yml
│ ├── en-US
│ │ └── language.yml
│ └── zh-CN
│ └── language.yml
├── log
│ ├── micro.log -> micro.log.20210216.log
│ └── micro.log.20210216.log
├── Makefile
├── migration # Migration files
│ └── 20210316142300_create_product.sql
├── module # Application module - Main buisiness logic
│ ├── module.go
│ └── product
│ ├── handler.go
│ ├── handler_test.go
│ ├── inject.go
│ ├── mocks
│ │ ├── IProductRepo.go
│ │ └── IProductService.go
│ ├── model
│ │ └── product.go
│ ├── repo
│ │ └── product.go
│ ├── routes.go
│ ├── service
│ │ ├── product_service.go
│ │ └── product_service_test.go
│ └── swagger
│ └── product.go
├── README.md
└── util