This project is a fork of https://github.com/amitshekhariitbhu/go-backend-clean-architecture
What I (https://github.com/stefanwuthrich) have done is:
- Migrated from Gin to Chi (https://github.com/go-chi/chi). Reason: Chi is more lightweight and based on standard net/http library.
- Updated Go modules to latest versions.
A Go (Golang) Backend Clean Architecture project with Chi, MongoDB, JWT Authentication Middleware, Test, and Docker.
You can use this project as a template to build your Backend project in the Go language on top of this project.
Learn about this project architecture in detail from the blogs mentioned below:
- Go Backend Clean Architecture
- Go JWT Authentication Middleware
- Configuration with Viper in Go
- Test with Testify and Mockery in Go
- Database Normalization vs Denormalization
- Router
- Controller
- Usecase
- Repository
- Domain
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:
- What is System Design?
- Twitter Timeline Design with Fanout Approach - System Design
- HTTP Request vs HTTP Long-Polling vs WebSocket vs Server-Sent Events
- YouTube Video Upload Service - System Design
- What is Consistent Hashing?
- Capacity Estimation: Back-of-the-envelope calculation - Twitter
- chi: Chi is an HTTP web framework written in Go (Golang) using standard library net/http. It's lightweight and fast.
- 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
.
JWT Authentication Middleware for Access Token Validation.
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
# Move to your workspace
cd your-workspace
# Clone this project into your workspace
git clone https://github.com/altafino/go-backend-clean-architecture-chi.git
# Move to the project root directory
cd go-backend-clean-architecture
- 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
tolocalhost
(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
- 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
# Run all tests
go test ./...
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.
.
├── Dockerfile
├── api
│ ├── controller
│ │ ├── error.go
│ │ ├── 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
- Login
- Profile
- Refresh Token
- Signup
- Task Create
-
signup
- Request
curl --location --request POST 'http://localhost:8080/public/signup' \ --header 'Content-Type: application/json' \ --data-raw '{ "email": "test@gmail.com", "password": "test", "name": "Test Name" }'
- Response
{ "accessToken": "access_token", "refreshToken": "refresh_token" }
-
login
- Request
curl --location --request POST 'http://localhost:8080/public/login' \ --header 'Content-Type: application/json' \ --data-raw '{ "email": "test@gmail.com", "password": "test" }'
- Response
{ "accessToken": "access_token", "refreshToken": "refresh_token" }
-
profile
- Request
curl --location --request GET 'http://localhost:8080/protected/profile' \ --header 'Authorization: Bearer access_token'
- Response
{ "name": "Test Name", "email": "test@gmail.com" }
-
task create
- Request
curl --location --request POST 'http://localhost:8080/protected/task' \ --header 'Authorization: Bearer access_token' \ --header 'Content-Type: application/json' \ --data-raw '{ "title": "Test Task" }'
- Response
{ "message": "Task created successfully" }
-
task fetch
- Request
curl --location --request GET 'http://localhost:8080/protected/task' \ --header 'Authorization: Bearer access_token'
- Response
[ { "title": "Test Task" }, { "title": "Test Another Task" } ]
-
refresh token
- Request
curl --location --request POST 'http://localhost:8080/public/refresh' \ --header 'Content-Type: application/json' \ --data-raw '{ "refreshToken": "refresh_token" }'
- Response
{ "accessToken": "access_token", "refreshToken": "refresh_token" }
- Improvement based on feedback.
- Add more test cases.
- Always try to update with the latest version of the packages used.
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.
All pull requests are welcome.