Skip to content

F8-Developer/golang-restAPI-FR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[ golang-restAPI-FR ]:

Build simple golang restfull API. It features a simple and better performance, and customize with requirements needed.

Required

Using



Installation

  • 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.go

If running normally, you can access http://0.0.0.0:7070


Dockerize


Use docker to compile and start

# 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>

Run From Docker hub

# 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

Rest API

  1. 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
  2. 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
    }
    

    Gopher image


    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
    }
    

    Gopher image


    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
    ##
    

    Gopher image


    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"
            }
        ]
    }
    

    Gopher image


    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"
        ]
    }
    

    Gopher image


    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
    }
    

    Gopher image


    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
    }
    

    Gopher image
    Gopher image



Validation

Add validation to all request API for better experience.


Authors

  • Ax7-cmd - Initial work - Ax7.

About

Golang Simple Restfull API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages