Giter Club home page Giter Club logo

amitshekhariitbhu / go-backend-clean-architecture Goto Github PK

View Code? Open in Web Editor NEW
3.7K 44.0 360.0 341 KB

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

Home Page: https://amitshekhar.me/blog/go-backend-clean-architecture

License: Apache License 2.0

Go 99.71% Dockerfile 0.29%
architecture backend clean-architecture gin go golang jwt-authentication project project-template docker test api

go-backend-clean-architecture's Introduction

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

  • Router
  • Controller
  • Usecase
  • Repository
  • Domain

Go Backend Clean Architecture Diagram

About me

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:

System Design Playlist on YouTube

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

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.

  • Clone 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

  • 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?

# 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

  • signup

    • Request
    curl --location --request POST 'http://localhost:8080/signup' \
    --data-urlencode '[email protected]' \
    --data-urlencode 'password=test' \
    --data-urlencode 'name=Test Name'
    
    • Response
    {
      "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
    {
      "accessToken": "access_token",
      "refreshToken": "refresh_token"
    }
  • profile

    • Request
    curl --location --request GET 'http://localhost:8080/profile' \
    --header 'Authorization: Bearer access_token'
    
    • Response
    {
      "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
    {
      "message": "Task created successfully"
    }
  • task fetch

    • Request
    curl --location --request GET 'http://localhost:8080/task' \
    --header 'Authorization: Bearer access_token'
    
    • Response
    [
      {
        "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
    {
      "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.

go-backend-clean-architecture's People

Contributors

amitshekhariitbhu avatar jinrenjie avatar rezarahemtola avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-backend-clean-architecture's Issues

How to Create MongoDB Index

Hi Amit!

Thanks for this perfect project!

May I know how to build mongo db index in this project? Which file should it be in?

Discussion about use cases

Hello! Thank you for this repo, it is really helpful. 🎉

I want to ask you about the responsibilities of use cases and controller. If I understood correctly, the controller should have the responsibility of sanitising the input from the route, calling the use case and then returning the response. The use case is the one that contains the business logic.

However, the login use case, is just a wrapper around UserRepository and the actual business logic happens in the LoginController:

[...]
user, err := lc.LoginUsecase.GetUserByEmail(c, request.Email)
	if err != nil {
		c.JSON(http.StatusNotFound, domain.ErrorResponse{Message: "User not found with the given email"})
		return
	}

	if bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(request.Password)) != nil {
		c.JSON(http.StatusUnauthorized, domain.ErrorResponse{Message: "Invalid credentials"})
		return
	}

	accessToken, err := lc.LoginUsecase.CreateAccessToken(&user, lc.Env.AccessTokenSecret, lc.Env.AccessTokenExpiryHour)
	if err != nil {
		c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
		return
	}

	refreshToken, err := lc.LoginUsecase.CreateRefreshToken(&user, lc.Env.RefreshTokenSecret, lc.Env.RefreshTokenExpiryHour)
	if err != nil {
		c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
		return
	}
[...]

Here, the controller decides that first we should fetch the user, then we check if the password is correct and if it is, we generate both an access token and a refresh token. This is the business logic of a login and I feel like it should be in the use case.

Discuss about external api

Hi folks and the author of this project,

I was wondering, while working with my services, I could be to call external APIs using various methods such as REST, RPC, queue, pubsub patterns, etc. And I want to know where put this?

Can you please help me figure this out and make it better?

DevSecOps best practices

Hi Amit!

Thanks for this project! It's a perfect start for newcomers on Go backend development!

I'm preparing a Medium post about DevSecOps best practices and I took your project since you released it recently and have enough code to show security use cases.

I was not able to do that with a fork since most of the tools are not supporting it, but I will create PR to help implement automation if you are OK 👍

This is the list of the changes I already made to my cloned project:

  • Use Renovate to update the dependency vulnerabilities with automated PR
  • Added pre-commit to enforce check + security scan before committing (Go fmt, GoSec, Checkov, Hadolint, etc.)
  • Created a Makefile to the same commands between the local dev and CI/CD pipelines
  • Added a GitHub Workflow to test and build the app + code coverage + SonarCloud + SAST scan + versioning + CHANGELOG
  • Added a GitHub Workflow to review the PR with ReviewDog
  • Added a GitHub Workflow to provide the ScoreCard of the project (how secure is it)
  • Added a GitHub Workflow to test the project with GitHub CodeQL (detect security issues like SQL injection, etc.)

These changes are already on the following repo: https://github.com/timoa/secure-go-backend-clean-architecture

I will add the following:

  • Postman collection to run the API testing on the CI/CD
  • Smoke tests
  • OWASP ZAP Proxy scan to check the security of the API (HTTP headers, cookies, etc.)

connect to mysqldb

Hello,
I love your instruction for building this code structure.
Would you mind help us to make codes to connect to the mysqlDb?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.