Code Monkey home page Code Monkey logo

microservice-mono-gin-gorm's Introduction

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

image Architecture

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

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\": \"[email protected]\",  \"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

microservice-mono-gin-gorm's People

Contributors

krishnarajimp avatar krishnarajvr avatar ahakrishnaraj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.