Setup REST API with Echo Framework, Postgre and Messaging.
Below is the packages used by this project
- Framework : https://github.com/labstack/echo
- ORM : https://github.com/jinzhu/gorm
- DB Driver :
- https://github.com/lib/pq (for Postgres)
- https://github.com/go-sql-driver/mysql (for MySQL)
- Configs:
- main : https://github.com/spf13/viper
- listener : https://github.com/fsnotify/fsnotify
- Request Validator : github.com/thedevsaddam/govalidator
- Unit Test : https://github.com/gavv/httpexpect
- Logger :
- Message Queue: TBD
Config file located in config.yaml
on the root of project.
Ready for Postgres and MySQL. Connection function located at db/gorm/gorm.go
. Those function called in main.go
.
Base CRUD function are located in models/orm/orm.go
. Example implementation within each model can be found at models/user.go
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
)
Currently there is two validation function for request.
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
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
You cannot use single validation for both query string and/or request body
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
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
- 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 withdocker-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.
If you use Insomnia you can import sample workspace from INSOMNIA-WORKSPACE.json
.