Golang + Gin + Postgres + Docker + gRPC + NGINX

gRPC and RESTful HTTP implementation of backend server written in Go.

Description

A Go-based based implementation of gRPC with Gin, PostgreSQL, Docker, and NGINX. This project demonstrates how to build a robust backend service with HTTP and gRPC servers, using Go's Gin framework for HTTP, a separate gRPC server and a gRPC gateway to handle HTTP requests under the hood.

Technologies used

Architecture

The project consists of the following 3 kinds of servers:

1. HTTP Gin Server

2. gRPC Server

3. gRPC Gateway Server

By following this architecture, developers can seamlessly switch between running the server as a standalone HTTP Gin server or as a gRPC server with an HTTP Gateway server serving both gRPC and HTTP clients at the same time. NGINX ensures efficient load balancing and distribution of incoming requests, enhancing the scalability and reliability of the architecture.

Setup local development

Install tools

Installation

  1. Clone project
git git@github.com:saalikmubeen/go-grpc-implementation.git

Manual

If you aren't a docker person, (Please learn docker 🥲)

cd into root project

cd go-grpc-implementation

go mod tidy to install server dependencies

Setup required environment variables:

*In the app.env file, replace the environment variables with your own.

Make sure you have postgresQL installed

To start the gRPC and HTTP server:

make server

OR ( if you don't have make installed)

go run main.go

This will start the gRPC server by default on port 50051 and the HTTP Gateway server on port 8080

Docker

If you use docker, respect++

Running project through docker is a breeze. You don't have to do any setup. Just one docker-compose command and magic

cd go-grpc-implementation

1. To run the project without load balancing

run:

make dockerup:

OR

docker-compose up --build

This will start the gRPC server by default on port 50051 and the HTTP Gateway server on port 8080

2. To run the project with load balancing

run:

make loadbalancerup

OR

docker-compose -f docker-compose-lb.yml up --build

This will run the 4 instances of our Go server in 4 different containers, each running on a different port and an NGINX load balancer to distribute the load between the 4 instances Each instance or container will be running two servers, one for gRPC and as an HTTP Gateway server. The NGINX will do two things:

NGINX Load Balancer opens two ports:

To send HTTP requests to the NGINX load balancer (which will distribute the requests between the 4 HTTP Gateway servers), just open web browser or Postman and send requests to http://localhost:3050

To send gRPC requests to the NGINX load balancer (which will distribute the requests between the 4 gRPC servers), you can use the Evans CLI tool to send gRPC requests to the NGINX load balancer. Run the following command:

evans --port 9090 --host localhost -r repl;