• Stars
    star
    289
  • Rank 138,074 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go Todo Backend example using modular project layout for product microservice.

go-todo-backend

GoDoc Build Status Go Report Card Maintainability Test Coverage

Go Todo Backend Example Using Modular Project Layout for Product Microservice. It's suitable as starting point for a medium to larger project.

This example uses Chi for http router and REL for database access.

Feature:

  • Modular Project Structure.
  • Full example including tests.
  • Docker deployment.
  • Compatible with todobackend.

Installation

Prerequisite

  1. Install mockery for interface mock generation.
  2. Install rel cli for database migration.

Running

  1. Prepare .env.
    cp .env.sample .env
    
  2. Start postgresql and create database.
    docker-compose up -d
    
  3. Prepare database schema.
    rel migrate
    
  4. Build and Running
    make
    

Project Structure

.
β”œβ”€β”€ api
β”‚   β”œβ”€β”€ handler
β”‚   β”‚   β”œβ”€β”€ todos.go
β”‚   β”‚   └── [other handler].go
β”‚   └── middleware
β”‚       └── [other middleware].go
β”œβ”€β”€ bin
β”‚   β”œβ”€β”€ api
β”‚   └── [other executable]
β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ api
β”‚   β”‚   └── main.go
β”‚   └── [other cmd]
β”‚       └── main.go
β”œβ”€β”€ db
β”‚   β”œβ”€β”€ schema.sql
β”‚   └── migrations
β”‚       └── [migration file]
β”œβ”€β”€ todos
β”‚   β”œβ”€β”€ todo.go
β”‚   β”œβ”€β”€ create.go
β”‚   β”œβ”€β”€ update.go
β”‚   β”œβ”€β”€ delete.go
β”‚   β”œβ”€β”€ service.go
β”‚   └── todostest
β”‚       β”œβ”€β”€ todo.go
β”‚       └── service.go
β”œβ”€β”€ [other domain]
β”‚   β”œβ”€β”€ [entity a].go
β”‚   β”œβ”€β”€ [business logic].go
β”‚   β”œβ”€β”€ [other domain]test
β”‚   β”‚   └── service.go
β”‚   └── service.go
└── [other client]
    β”œβ”€β”€ [entity b].go
    β”œβ”€β”€ client.go
    └── [other client]test
        └── client.go

This project structure is based on a modular project structure, with loosely coupled dependencies between domain, Think of making libraries under a single repo that only exports certain functionality that used by other service and http handler. One of domain that present in this example is todos.

Loosely coupled dependency between domain is enforced by avoiding the use of shared entity package, therefore any entity struct should be included inside it's own respective domain. This will prevent cyclic dependency between entity. This shouldn't be a problem in most cases, becasause if you encounter cyclic dependency, there's huge chance that the entity should belongs to the same domain.

For example, consider three structs: user, transaction and transaction items. transaction and its transaction items might need cyclic dependency and items doesn't works standalone (items without transaction should not exists), thus it should be on the same domain. In the other hand, user and transaction shouldn't require cyclic dependency, transaction might have a user field in the struct, but user shouldn't have a slice of transaction field, therefore it should be on a separate domain.

Domain vs Client

Domain and Client folder is very similar, the difference is client folder doesn't actually implement any business logic (service), but instead a client that calls any internal/external API to works with the domain entity.