anthanhphan / golang-clean-architecture

Clean architecture implementation in Golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A Simple Clean Architecture Implementation In Golang

Clean Architecture is a software architectural pattern designed to create scalable, maintainable, and testable applications using the Go programming language. It enforces a clear separation of concerns, making it easier to develop and maintain complex software systems.

alt text

What I have done in this sample project?

  • A sample user module with 5 apis
  • Send template mail using SMTP
  • Dockerizing project by dockerfile and docker-compose
  • Swagger API docs
  • Unit test
  • Integration tests for each use case

I. Tech stack:

  • Gin
  • GORM
  • Postgres
  • JWT
  • Docker

II. API documentation with Swagger

alt text

1. Install Swaggo

    go install github.com/swaggo/swag/cmd/swag@latest

2. Init swag to generate docs

    swag init -g cmd/main.go

III. Prepare database

1. Create postgres database using Docker

    make init_db

2. Remove database

    make  rm_db

IV. Run application

  • Remember to download all dependencies before you start server
    go mod download
    go mod tidy
  • Then you need to config your SMPT (gmail) in local.env and prod.env in config/env directory.
    # MAIL CONFIG
    MAIL_FROM=YOUR GMAIL
    MAIL_PASS=YOUR GMAIL APP PASSWORD
    MAIL_SERVER=smtp.gmail.com
    MAIL_PORT=587

1. Run with migration database

    make local m=true

2. Run without migration database

    make local m=false

V. Run with Docker

  • When you initiate the server using docker-compose, you don't need to start the database separately, as it will already be set up within docker-compose.

1. Compose up

  • The only command you need to start the server.
  • Remember to set up a prod.env file in the config/env directory before running the command. You can set up this file the same way as the local.env file, for example.
    make compose_up

2. Compose down

  • Run this command if you want to clear everything, including images and containers of the application, and the database as well.
    make compose_down

About

Clean architecture implementation in Golang.


Languages

Language:Go 95.7%Language:HTML 2.0%Language:Dockerfile 1.6%Language:Makefile 0.6%