The Principal Dev – Masterclass for Tech Leads

The Principal Dev – Masterclass for Tech LeadsJuly 17-18

Join

Go Backend Clean Architecture

A Go (Golang) Backend Clean Architecture project with Gin, MongoDB, JWT Authentication Middleware, Test, and Docker.

Go Backend Clean Architecture

You can use this project as a template to build your Backend project in the Go language on top of this project.

Before creating this project, I have gone through more than 20 projects related to the Go(Golang) Clean Architecture on GitHub.

Thanks to all those projects, I learned a lot from all of those. As I keep saying:

The best way to learn to code is to code. But, to write good code, you will also have to read good code. Make a habit of reading good code. You can find many open-source projects on GitHub and start reading.

Then for the implementation part, I combined all of my ideas, experiences, and learnings from those projects to create this project.

And as always I would love to get feedback on my project. This helps everyone and most importantly me.

Learn about this project architecture in detail from the blogs mentioned below:

Architecture Layers of the project

Go Backend Clean Architecture Diagram

About me

Hi, I am Amit Shekhar, Co-Founder @ Outcome School β€’ IIT 2010-14 β€’ I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

You can connect with me on:

System Design Playlist on YouTube

Major Packages used in this project

Public API Request Flow without JWT Authentication Middleware

Public API Request Flow

Private API Request Flow with JWT Authentication Middleware

JWT Authentication Middleware for Access Token Validation.

Private API Request Flow

How to run this project?

We can run this Go Backend Clean Architecture project with or without Docker. Here, I am providing both ways to run this project.

# Move to your workspace
cd your-workspace

# Clone this project into your workspace
git clone https://github.com/amitshekhariitbhu/go-backend-clean-architecture.git

# Move to the project root directory
cd go-backend-clean-architecture

Run without Docker

Run with Docker

How to run the test?

# Run all tests
go test ./...

How to generate the mock code?

In this project, to test, we need to generate mock code for the use-case, repository, and database.

# Generate mock code for the usecase and repository
mockery --dir=domain --output=domain/mocks --outpkg=mocks --all

# Generate mock code for the database
mockery --dir=mongo --output=mongo/mocks --outpkg=mocks --all

Whenever you make changes in the interfaces of these use-cases, repositories, or databases, you need to run the corresponding command to regenerate the mock code for testing.

The Complete Project Folder Structure

.
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ api
β”‚   β”œβ”€β”€ controller
β”‚   β”‚   β”œβ”€β”€ login_controller.go
β”‚   β”‚   β”œβ”€β”€ profile_controller.go
β”‚   β”‚   β”œβ”€β”€ profile_controller_test.go
β”‚   β”‚   β”œβ”€β”€ refresh_token_controller.go
β”‚   β”‚   β”œβ”€β”€ signup_controller.go
β”‚   β”‚   └── task_controller.go
β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   └── jwt_auth_middleware.go
β”‚   └── route
β”‚       β”œβ”€β”€ login_route.go
β”‚       β”œβ”€β”€ profile_route.go
β”‚       β”œβ”€β”€ refresh_token_route.go
β”‚       β”œβ”€β”€ route.go
β”‚       β”œβ”€β”€ signup_route.go
β”‚       └── task_route.go
β”œβ”€β”€ bootstrap
β”‚   β”œβ”€β”€ app.go
β”‚   β”œβ”€β”€ database.go
β”‚   └── env.go
β”œβ”€β”€ cmd
β”‚   └── main.go
β”œβ”€β”€ docker-compose.yaml
β”œβ”€β”€ domain
β”‚   β”œβ”€β”€ error_response.go
β”‚   β”œβ”€β”€ jwt_custom.go
β”‚   β”œβ”€β”€ login.go
β”‚   β”œβ”€β”€ profile.go
β”‚   β”œβ”€β”€ refresh_token.go
β”‚   β”œβ”€β”€ signup.go
β”‚   β”œβ”€β”€ success_response.go
β”‚   β”œβ”€β”€ task.go
β”‚   └── user.go
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ internal
β”‚   └── tokenutil
β”‚       └── tokenutil.go
β”œβ”€β”€ mongo
β”‚   └── mongo.go
β”œβ”€β”€ repository
β”‚   β”œβ”€β”€ task_repository.go
β”‚   β”œβ”€β”€ user_repository.go
β”‚   └── user_repository_test.go
└── usecase
    β”œβ”€β”€ login_usecase.go
    β”œβ”€β”€ profile_usecase.go
    β”œβ”€β”€ refresh_token_usecase.go
    β”œβ”€β”€ signup_usecase.go
    β”œβ”€β”€ task_usecase.go
    └── task_usecase_test.go

API documentation of Go Backend Clean Architecture

View API Doc Button

Example API Request and Response

TODO

If this project helps you in anyway, show your love ❀️ by putting a ⭐ on this project ✌️

License

   Copyright (C) 2024 Amit Shekhar

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Contributing to Go Backend Clean Architecture

All pull requests are welcome.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.