amitshekhariitbhu / go-backend-clean-architecture
- четверг, 12 января 2023 г. в 00:37:13
A Go (Golang) Backend Clean Architecture project with Gin, MongoDB, JWT Authentication Middleware, Test, and Docker.
A Go (Golang) Backend Clean Architecture project with Gin, 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.
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:
Hi, I am Amit Shekhar, I have 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:
.env
file. Go configuration with fangs. Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, INI, envfile, or Java properties formats.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.
# 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
.env
similar to .env.example
at the root directory with your configuration.go
if not installed on your machine.MongoDB
if not installed on your machine.DB_HOST
to localhost
(DB_HOST=localhost
) in .env
configuration file. DB_HOST=mongodb
is needed only when you run with Docker.go run cmd/main.go
.http://localhost:8080
.env
similar to .env.example
at the root directory with your configuration.docker-compose up -d
.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
│ │ ├── 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
│ └── v1
│ ├── 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
signup
curl --location --request POST 'http://localhost:8080/v1/signup' \
--data-urlencode 'email=test@gmail.com' \
--data-urlencode 'password=test' \
--data-urlencode 'name=Test Name'
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
login
curl --location --request POST 'http://localhost:8080/v1/login' \
--data-urlencode 'email=test@gmail.com' \
--data-urlencode 'password=test'
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
profile
curl --location --request GET 'http://localhost:8080/v1/profile' \
--header 'Authorization: Bearer access_token'
{
"name": "Test Name",
"email": "test@gmail.com"
}
task create
curl --location --request POST 'http://localhost:8080/v1/task' \
--header 'Authorization: Bearer access_token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'title=Test Task'
{
"message": "Task created successfully"
}
task fetch
curl --location --request GET 'http://localhost:8080/v1/task' \
--header 'Authorization: Bearer access_token'
[
{
"title": "Test Task"
},
{
"title": "Test Another Task"
}
]
refresh token
curl --location --request POST 'http://localhost:8080/v1/refresh' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'refreshToken=refresh_token'
{
"accessToken": "access_token",
"refreshToken": "refresh_token"
}
Copyright (C) 2023 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.