sandisunandar99 / rest_echo

REST API Blueprint with Go & Echo Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SIMPLE REST API WITH ECHO FRAMEWORK

Setup REST API with Echo Framework, Postgre and Messaging.

Package Dependencies

Below is the packages used by this project

Configs

Config file located in config.yaml on the root of project.

ORM

Ready for Postgres and MySQL. Connection function located at db/gorm/gorm.go. Those function called in main.go.

CRUD Functionality

Base CRUD function are located in models/orm/orm.go. Example implementation within each model can be found at models/user.go

Models

Models located in models. All models should inherit BaseModel struct in models/base.go. BaseModel struct are holding default tables attribute. Another attributes specific to each model should defined as struct in each model file (i.e models/user.go)

Request Validator

Currently there is two validation function for request.

Validate JSON Body

Function for validate this kind of request is ValidateRequest(c echo.Context, rules govalidator.MapData, data interface{}) map[string]interface{}, located in api/handlers/generalHandlers

Validate URL Query String

Function for validate this kind of request is ValidateQueryStr(c echo.Context, rules govalidator.MapData, data interface{}) map[string]interface{}, located in api/handlers/generalHandlers

Limitation

You cannot use single validation for both query string and/or request body

Logger

Logrus are wrapped within modules/logger/logger.go under logger package. This wrapper also implement file rotator.

Default rotator time is every one day and only kept for seven days of log.

Example implementation can be found at api/middlewares/logMiddlewares.go

Unit Test

This unit test is for testing each endpoint and response. All test file located in tests folder. You can run a test with

go test tests/*_test.go

Run with Docker

  • Install Docker
  • Clone this repo
  • Create another container for Postgre, and put in the same docker network (i.e my-shared-network). You can use my support container
  • Run docker-compose up --build on project root, and make sure to check it if its run well with docker-compose ps, you should see like this below
Imans-MacBook-Air:disbursement iman$ docker-compose ps
       Name                     Command               State           Ports
------------------------------------------------------------------------------------
<your_project_folder>_api_1   /bin/sh -c dep ensure && g ...   Up      0.0.0.0:8000->8000/tcp
  • Test with your REST API tools for GET 0.0.0.0:8000/users, and you should see some JSON response.

References When Building This Project

Insomania (RESTAPI Client) Workspace

If you use Insomnia you can import sample workspace from INSOMNIA-WORKSPACE.json.

About

REST API Blueprint with Go & Echo Framework


Languages

Language:Go 99.1%Language:Dockerfile 0.9%