/Golang-Echo-Gorm-Clean-Architecture

Implementation of Clean Architecture in Golang projects.

Primary LanguageGo

Golang-Echo-Gorm-Clean-Architecture

Implementation of Clean Architecture in Golang RestAPI projects. Maintaining a large codebase with maximum readability and minimal overhead is hard.

How to structure the code

Untitled Diagram

  • Handlers are responsible for receiving requests from clients and sending responses back to to clients.
  • Services are responsible for receiving information from handlers to process bussiness logic.
  • Repositories abstract data storage behind a common interface, allowing services to persist data to and retrieve data

Project structure

├── main.go                     // running
├── config                      
│   ├── config_prop.go          // config property (read all config file to struct)
│   ├── db                      // config db
│   └── redis                   // config redis
├── middlewares
├── routes
│   └── route.go                // routes
├── features
│   ├── user                    // user module
│   │   ├── data.go             // request and response data
│   │   ├── service.go          // business logic
│   │   └── handler.go    
│   ├── role                    // role module
│   │   ├── data.go             // request and response data
│   │   ├── service.go          // business logic
│   │   └── handler.go
├── repositories
│   ├── models.go               // models or entities
│   ├── user_repo.go            // repo interface
│   ├── role_repo.go            // repo interface
│   ├── mysql
│   │   ├── user_repo.go        // repo implement
│   │   └── role_repo.go        // repo implement
├── config.yaml
├── docker-compose.yaml
├── Makefile

Start app function

func main() {

	var server = echo.New()

	route.SetupLogger(server)
	route.SetupGlobalErrorHandler(server)
	route.SetupMiddleware(server)
	route.SetupRoute(server)

	server.Logger.Fatal(server.Start(":8080"))
}

Dependencies

  • github.com/labstack/echo/v4
  • github.com/labstack/echo/v4/middleware
  • github.com/labstack/gommon/log
  • gorm.io/gorm
  • gorm.io/driver/mysql
  • github.com/jinzhu/copier

Reference