A Go (Golang) backend clean architecture project with API Gateway, Microservices, MongoDB and JWT Authentication.
The project was created for educational purposes and is not ideal. It has its shortcomings, which are gradually being corrected.
The architecture of a web application consists of these layers:
- Reverse Proxy
- API Gateway
- Microservice
- Database
The architecture of the microservice application consists of these layers:
- Server
- Service
- Repository
The API is accessed via Reverse Proxy, in our case it is Nginx. It handles all incoming requests and prevents access to microservices and API gateway directly.
- Gin: Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
- gRPC: The Go implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. For more information see the Go gRPC docs, or jump directly into the quick start.
- protobuf: Go support for Google's protocol buffers.
- MongoDB: The Official Golang driver for MongoDB.
- JWT: Go implementation of JSON Web Tokens (JWT).
- Cleanenv: Clean and minimalistic environment configuration reader for Golang.
- Bcrypt: Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm.
- Logrus: Structured, pluggable logging for Go.
- Validator: Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.
First, download it and navigate to the root directory:
# Move to your workspace
cd your-workspace
# Clone the project into your workspace
git clone https://github.com/ArtoriasCode/jwtgo.git
# Move to the project root directory
cd jwtgo
-
Create a
.env
file, similar to.env.sample
. -
Install the Docker, Protoc, Taskfile if it is not installed on your computer.
-
Fill in the
.env
file with your data. -
Run the application build with the following command:
task build
-
Access API using http://localhost.
-
Request:
curl --location 'http://localhost/api/v1/auth/signup' \ --header 'Content-Type: application/json' \ --data-raw '{ "email": "[email protected]", "password": "securepassword" }'
-
Response:
HTTP/1.1 200 OK Content-Type: application/json
{ "message": "User successfully registered" }
-
Request:
curl --location 'http://localhost/api/v1/auth/signin' \ --header 'Content-Type: application/json' \ --data-raw '{ "email": "[email protected]", "password": "securepassword" }'
-
Response:
HTTP/1.1 200 OK Content-Type: application/json Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
{ "message": "User successfully logged in" }
-
Request:
curl --location 'http://localhost/api/v1/auth/signout' \ --header 'Content-Type: application/json' \ -b 'access_token=access_token; refresh_token=refresh_token'
-
Response:
HTTP/1.1 200 OK Content-Type: application/json Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
{ "message": "User successfully logged out" }
-
Request:
curl --location 'http://localhost/api/v1/auth/refresh' \ --header 'Content-Type: application/json' \ -b 'access_token=access_token; refresh_token=refresh_token'
-
Response:
HTTP/1.1 200 OK Content-Type: application/json Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
{ "message": "Tokens successfully updated" }
βββ build
β βββ package
β βββ api.Dockerfile
β βββ auth.Dockerfile
β βββ user.Dockerfile
βββ cmd
β βββ api
β β βββ main.go
β βββ auth
β β βββ main.go
β βββ user
β βββ main.go
βββ configs
β βββ nginx.conf
βββ deployments
β βββ docker-compose.yaml
βββ internal
β βββ app
β β βββ api
β β β βββ config
β β β β βββ config.go
β β β βββ controller
β β β β βββ http
β β β β βββ dto
β β β β β βββ user.go
β β β β βββ mapper
β β β β β βββ user.go
β β β β βββ middleware
β β β β β βββ security.go
β β β β β βββ validation.go
β β β β βββ v1
β β β β βββ auth.go
β β β βββ main.go
β β βββ auth
β β β βββ config
β β β β βββ config.go
β β β βββ interface
β β β β βββ service
β β β β βββ auth.go
β β β βββ server
β β β β βββ grpc
β β β β βββ dto
β β β β β βββ user.go
β β β β βββ mapper
β β β β β βββ user.go
β β β β βββ v1
β β β β βββ auth.go
β β β βββ service
β β β β βββ auth.go
β β β βββ main.go
β β βββ user
β β βββ adapter
β β β βββ mongodb
β β β βββ entity
β β β β βββ user.go
β β β βββ mapper
β β β β βββ user.go
β β β βββ repository
β β β βββ user.go
β β βββ config
β β β βββ config.go
β β βββ entity
β β β βββ user.go
β β βββ interface
β β β βββ repository
β β β β βββ user.go
β β β βββ service
β β β βββ user.go
β β βββ server
β β β βββ grpc
β β β βββ dto
β β β β βββ user.go
β β β βββ mapper
β β β β βββ user.go
β β β βββ v1
β β β βββ user.go
β β βββ service
β β β βββ user.go
β β βββ main.go
β βββ pkg
β βββ error
β β βββ auth.go
β β βββ jwt.go
β β βββ repository.go
β β βββ server.go
β βββ interface
β β βββ service
β β βββ jwt.go
β β βββ password.go
β βββ proto
β β βββ auth
β β β βββ auth.pb.go
β β β βββ auth_grpc.pb.go
β β βββ user
β β βββ user.pb.go
β β βββ user_grpc.pb.go
β βββ request
β β βββ schema
β β β βββ response.go
β β βββ response.go
β βββ service
β βββ schema
β β βββ jwt.go
β βββ jwt.go
β βββ password.go
βββ pkg
β βββ client
β β βββ mongodb.go
β βββ logging
β β βββ logger.go
β βββ proto
β βββ auth
β β βββ auth.proto
β βββ user
β βββ user.proto
βββ .env
βββ .gitignore
βββ go.mod
βββ go.sum
βββ LICENSE
βββ README.md
βββ taskfile.yaml