https://github.com/saalikmubeen/go-grpc-implementation
Golang + Gin + Docker + gRPC + NGINX
https://github.com/saalikmubeen/go-grpc-implementation
docker gin golang grpc nginx protocol-buffers sqlc
Last synced: 9 months ago
JSON representation
Golang + Gin + Docker + gRPC + NGINX
- Host: GitHub
- URL: https://github.com/saalikmubeen/go-grpc-implementation
- Owner: saalikmubeen
- License: mit
- Created: 2024-05-20T19:43:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-12T05:49:42.000Z (over 1 year ago)
- Last Synced: 2025-04-23T12:04:11.819Z (9 months ago)
- Topics: docker, gin, golang, grpc, nginx, protocol-buffers, sqlc
- Language: Go
- Homepage:
- Size: 5.54 MB
- Stars: 63
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
- **`Gin`** as HTTP web framework
- **`PostgreSQL`** as database
- **`SQLC`** as code generator for SQL
- **`golang-migrate`** for database migration
- **`gRPC`** Remote procedure call framework.
- **`Docker`** for containerizing the application
- **`NGINX`** as a load balancer and reverse proxy
- **`Protoc`** as protocol buffer compiler
# Architecture
The project consists of the following 3 kinds of servers:
### 1. HTTP Gin Server
- `Description`: A lightweight HTTP web server built with the Gin framework.
- `Functionality`: Serves HTTP requests directly without any additional translation.
- `Purpose`: Offers a simple and efficient means for handling HTTP client requests.
### 2. gRPC Server
- `Description`: Implements gRPC services for remote procedure calls.
- `Functionality`: Handles gRPC client requests directly, providing efficient communication between client and server.
- `Purpose`: Facilitates high-performance, bidirectional communication between client and server.
### 3. gRPC Gateway Server
- `Description`: An HTTP server that acts as a gateway for HTTP clients to communicate with the gRPC server.
- `Functionality`: Receives HTTP requests from clients and translates them into gRPC calls, forwarding them to the appropriate gRPC handlers.
- `Purpose`: Enables HTTP clients to interact seamlessly with the gRPC server, expanding compatibility and usability.
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
- [Docker desktop](https://www.docker.com/products/docker-desktop)
- [Golang](https://golang.org/)
- [Homebrew](https://brew.sh/)
- [Migrate](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate)
- [Evans Cli](https://github.com/ktr0731/evans)
```bash
brew install golang-migrate
```
- [Sqlc](https://github.com/kyleconroy/sqlc#installation)
```bash
brew install sqlc
```
- [Gomock](https://github.com/golang/mock)
``` bash
go install github.com/golang/mock/mockgen@v1.6.0
```
# Installation
1. Clone project
```
git git@github.com:saalikmubeen/go-grpc-implementation.git
```
## Manual
**`If you aren't a docker person`**, ([Please learn docker 🥲](https://www.docker.com/why-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:
- Load balance the incoming HTTP requests between the 4 HTTP Gateway servers running in the 4 instances
- Load balance the incoming gRPC requests between the 4 gRPC servers running in the 4 instances
NGINX Load Balancer opens two ports:
- Port 80, which maps to port 3050 for incoming HTTP requests
- Port 9090, which maps to same port 9090 for incoming gRPC requests
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;
```