Build simple golang restfull API. It features a simple and better performance, and customize with requirements needed.
- GO 1.17.5 - go1.17.5.
- Gin Web Framework 1.3.0 - Gin-Gionic
- MySQL 5.7.26 - MySQL
- Go Validator v10 - go-validator
- Init workdir
git clone https://github.com/F8-Developer/golang-restAPI-FR.git
cd golang-restAPI-FR- Build mod Vendor
# make sure you have folder vendor in your root directory "golang-restAPI-FR/vendor"
# if you dont have folder vendor create new one with this command
mkdir vendor
# install golang package in folder vendor
go mod vendor- Copy .env.example to .env
cp .env.example .env
# change default config .env with your local config
APP_ENV=development
APP_ADDRESS=0.0.0.0:7070
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=rest_api_db
DB_USERNAME=root
DB_PASSWORD=
- Database Note
# restAPI will automatically migrate when there is no table in you database- Start restAPI
# start with default
go run server.goIf running normally, you can access http://0.0.0.0:7070
# Compile the image
docker build -t golang-restapi-fr .
# Create container
docker container create --name golang-restapi-fr -e APP_ADDRESS=0.0.0.0:7070 -e DB_CONNECTION=mysql -e DB_HOST=127.0.0.1 -e DB_PORT=8889 -e DB_DATABASE=rest_api_db -e DB_USERNAME=root -e DB_PASSWORD=root -p 7070:7070 golang-restapi-fr
# Start Container
docker container start golang-restapi-fr
docker container ls
# for mac user check database connection because DB_HOST=docker.for.mac.localhost
# command check log docker container
# $ docker logs <container id># Download container image from https://hub.docker.com/
docker container create --name golang-restapi-fr -e APP_ADDRESS=0.0.0.0:7070 -e DB_CONNECTION=mysql -e DB_HOST=127.0.0.1 -e DB_PORT=8889 -e DB_DATABASE=rest_api_db -e DB_USERNAME=root -e DB_PASSWORD=root -p 7070:7070 jinrave/golang-restapi-fr
# Start Container
docker container start golang-restapi-fr
docker container ls
-
Endpoint
METHOD URL INFO GET / index POST /friend/request for create a friend request POST /friend/accept to accept friend request POST /friend/reject to reject friend request POST /friend/list-request list of friend requests by user email POST /friend/list-friends list of friends by user email POST /friend/list-friends-between friends list between two email addresses POST /friend/block blocked users cannot send friend request -
Example Api
friend request api : http://0.0.0.0:7070/friend/request
POST /friend/request HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 69 { "requestor": "andy@example.com", "to": "john@example.com" } response: { "success": true }accept friend request api : http://0.0.0.0:7070/friend/accept
request: POST /friend/accept HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 69 { "requestor": "andy@example.com", "to": "john@example.com" } response: { "success": true }reject friend request api : http://0.0.0.0:7070/friend/reject
POST /friend/reject HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 69 { "requestor": "andy@example.com", "to": "john@example.com" } response: { "success": true } ## # if friend request already accepted or there in no friend request, will return "success": false ##list of friend requests api : http://0.0.0.0:7070/friend/list-request
request: POST /friend/list-request HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 36 { "email": "john@example.com" } response: { "requests": [ { "requestor": "andy@example.com", "status": "accepted" }, { "requestor": "joe@example.com", "status": "rejected" }, { "requestor": "grace@example.com", "status": "pending" } ] }list of friends api : http://0.0.0.0:7070/friend/list-friends
POST /friend/list-friends HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 36 { "email": "andy@example.com" } response: { "friends": [ "john@example.com", "joe@example.com" ] }friends list between api : http://0.0.0.0:7070/friend/list-friends-between
POST /friend/list-friends-between HTTP/1.1 Host: localhost:7070 Content-Type: application/json Content-Length: 80 { "friends":[ "andy@example.com", "john@example.com" ] } response: { "success": true, "friends": [ "frank@example.com" ], "count": 1 }friend block api : http://0.0.0.0:7070/friend/block
POST /friend/block HTTP/1.1 Host: 0.0.0.0:7070 Content-Type: application/json Content-Length: 73 { "requestor": "andry@example.com", "block": "john@example.com" } response: { "success": true }
Add validation to all request API for better experience.
- Ax7-cmd - Initial work - Ax7.







