base on A Go (Golang) Backend Clean Architecture project with Gin, MongoDB, JWT Authentication Middleware, Test, and Docker. # Go Backend Clean Architecture
A Go (Golang) Backend Clean Architecture project with Gin, MongoDB, JWT Authentication Middleware, Test, and Docker.
![Go Backend Clean Architecture](https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/assets/go-backend-clean-architecture.png?raw=true)
**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:
- [Go Backend Clean Architecture](https://outcomeschool.com/blog/go-backend-clean-architecture)
- [Go JWT Authentication Middleware](https://outcomeschool.com/blog/go-jwt-authentication-middleware)
- [Configuration with Viper in Go](https://outcomeschool.com/blog/configuration-with-viper-in-go)
- [Test with Testify and Mockery in Go](https://outcomeschool.com/blog/test-with-testify-and-mockery-in-go)
- [Database Normalization vs Denormalization](https://outcomeschool.com/blog/database-normalization-vs-denormalization)
## Architecture Layers of the project
- Router
- Controller
- Usecase
- Repository
- Domain
![Go Backend Clean Architecture Diagram](https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/assets/go-backend-arch-diagram.png?raw=true)
## About me
Hi, I am Amit Shekhar, Co-Founder @ [Outcome School](https://outcomeschool.com) • 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:
- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
## System Design Playlist on YouTube
- [What is System Design?](https://www.youtube.com/watch?v=i4YWRY3hsdA)
- [Twitter Timeline Design with Fanout Approach - System Design](https://www.youtube.com/watch?v=_7qHGfwgPz0)
- [HTTP Request vs HTTP Long-Polling vs WebSocket vs Server-Sent Events](https://www.youtube.com/watch?v=8ksWRX4xV-s)
- [YouTube Video Upload Service - System Design](https://www.youtube.com/watch?v=N0vvJTkokZc)
- [What is Consistent Hashing?](https://www.youtube.com/watch?v=dV5cIm9T3ss)
- [Capacity Estimation: Back-of-the-envelope calculation - Twitter](https://www.youtube.com/watch?v=yrbKxzXm6_Q)
## Major Packages used in this project
- **gin**: Gin is an 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 a smashing performance, get yourself some Gin.
- **mongo go driver**: The Official Golang driver for MongoDB.
- **jwt**: JSON Web Tokens are an open, industry-standard RFC 7519 method for representing claims securely between two parties. Used for Access Token and Refresh Token.
- **viper**: For loading configuration from the `.env` file. Go configuration with fangs. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, INI, envfile, or Java properties formats.
- **bcrypt**: Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm.
- **testify**: A toolkit with common assertions and mocks that plays nicely with the standard library.
- **mockery**: A mock code autogenerator for Golang used in testing.
- Check more packages in `go.mod`.
### Public API Request Flow without JWT Authentication Middleware
![Public API Request Flow](https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/assets/go-arch-public-api-request-flow.png?raw=true)
### Private API Request Flow with JWT Authentication Middleware
> JWT Authentication Middleware for Access Token Validation.
![Private API Request Flow](https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/assets/go-arch-private-api-request-flow.png?raw=true)
### 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.
- Clone this project
```bash
# 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
- Create a file `.env` similar to `.env.example` at the root directory with your configuration.
- Install `go` if not installed on your machine.
- Install `MongoDB` if not installed on your machine.
- Important: Change the `DB_HOST` to `localhost` (`DB_HOST=localhost`) in `.env` configuration file. `DB_HOST=mongodb` is needed only when you run with Docker.
- Run `go run cmd/main.go`.
- Access API using `http://localhost:8080`
#### Run with Docker
- Create a file `.env` similar to `.env.example` at the root directory with your configuration.
- Install Docker and Docker Compose.
- Run `docker-compose up -d`.
- Access API using `http://localhost:8080`
### How to run the test?
```bash
# 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.
```bash
# 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
<a href="https://documenter.getpostman.com/view/391588/2s8Z75S9xy" target="_blank">
<img alt="View API Doc Button" src="https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/assets/button-view-api-docs.png?raw=true" width="200" height="60"/>
</a>
### Example API Request and Response
- signup
- Request
```
curl --location --request POST 'http://localhost:8080/signup' \
--data-urlencode '
[email protected]' \
--data-urlencode 'password=test' \
--data-urlencode 'name=Test Name'
```
- Response
```json
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
```
- login
- Request
```
curl --location --request POST 'http://localhost:8080/login' \
--data-urlencode '
[email protected]' \
--data-urlencode 'password=test'
```
- Response
```json
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
```
- profile
- Request
```
curl --location --request GET 'http://localhost:8080/profile' \
--header 'Authorization: Bearer access_token'
```
- Response
```json
{
"name": "Test Name",
"email": "
[email protected]"
}
```
- task create
- Request
```
curl --location --request POST 'http://localhost:8080/task' \
--header 'Authorization: Bearer access_token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'title=Test Task'
```
- Response
```json
{
"message": "Task created successfully"
}
```
- task fetch
- Request
```
curl --location --request GET 'http://localhost:8080/task' \
--header 'Authorization: Bearer access_token'
```
- Response
```json
[
{
"title": "Test Task"
},
{
"title": "Test Another Task"
}
]
```
- refresh token
- Request
```
curl --location --request POST 'http://localhost:8080/refresh' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'refreshToken=refresh_token'
```
- Response
```json
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
```
### TODO
- Improvement based on feedback.
- Add more test cases.
- Always try to update with the latest version of the packages used.
## 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.
", Assign "at most 3 tags" to the expected json: {"id":"4090","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"