smmd / sample-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sample API

Sample API uses dependency injection, unit tests using mocks, and Clean Architecture.

Architecture

Clean Architecture consists on separate the logic into different packages. The main proposal is the Usecase package. The Usecase package is used to handle the bussines logic, and call other packages according to the different flows.

Clean Architecture proposes a lot of packages, this Sample API contains the principal ones:

  • Model
  • Service
  • Usecase
  • Controller
  • Router

Model

Model package contains all the structures to handle the JSON data and responses. Is the only package that could be used in all the layers.

Service

Service package (also called Repository) contains all the logic to handle the operations. This logic is separated into several functions, which allows to test in an easier way, have decoupled logic, and maintain the code in a better way. If some logic is added in the future, a new function will be created and the Usecase will call it.

Usecase

Usecase package contains the business logic, according to the flows, it will call the specific function in the Service package to get or store data.

Controller

Controller package contains the handlers, it calls the usecase flows, returns the responses and handles the http errors.

Router

Router package contains the endpoints, each enpoint is linked to a handler.

Requirements

  • Go - Version go1.15.2 or above.

Setup

  1. Clone the repository

  2. Open the terminal and go to the project path

  3. In the root level, run the following commands:

        make build
    
        ./sample-api
  4. The Samplle API will be running on:

        http://localhost:8080/v1

Running the tests

The Service layer does not contain tests due to is mocked data.

  • Run all the tests with the following command:

        make unit-test

Endpoints

Sample API contains two GET endpoints:

    http://localhost:8080/v1/ping
    http://localhost:8080/v1/sample

About


Languages

Language:Go 92.5%Language:Makefile 7.5%