Code Monkey home page Code Monkey logo

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

View Code? Open in Web Editor NEW
3.8K 44.0 367.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

go-backend-clean-architecture's Issues

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.

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.)

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?

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?

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?

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.