rest-and-go(Not maintained actively)

A basic online store API written to learn Go Programming Language

This API is a pretty basic implementation of an online(e-commerce) store.

  • You can perform basic CRUD(CREATE, READ, UPDATE and DELETE) operations
  • SEARCH on a predefined database of products
  • Only Authenticated users can Add, Update and Delete products from database
  • Authentication is based on JWT(JSON web Tokens) Tokens
  • API is backed by a predefined Mongo DB database hosted on mLab
  • This API also lives on Herokuhttps://gruesome-monster-22811.herokuapp.com/

See API Documentation and Usage below on how to use it.

Directory Structure

rest-and-go/
    |- Godeps/             - Contains info about all dependencies of the project
    |- store/              - Contains main API logic files 
        |- controller.go  - Defines methods handling calls at various endpoints
        |- model.go       - User and Product models
        |- repository.go  - Methods interacting with the database
        |- router.go      - Defines routes and endpoints
    |- vendor/             - Dependency packages, necessary for deployment
    |- .gitignore
    |- LICENSE
    |- Procfile             - Procfile for herkou deployment
    |- README.md
    |- dummyData.js         - Script to populate local mongodb with dummy data
    |- main.go              - Entry point of the API
  

Setup

Golang Development Setup

You can use this bash script to automate the Golang development setup – https://github.com/canha/golang-tools-install-script

Steps

  1. Download the repository using wget
    wget https://raw.githubusercontent.com/canha/golang-tools-install-script/master/goinstall.sh
  2. According to the OS you’re on
    • Linux 64 bit -> bash goinstall.sh --64
    • Linux 32 bit -> bash goinstall.sh --32
    • macOS -> bash goinstall.sh --darwin

You can also follow the official docs of installation if you want to know the complete process.

Project setup

  1. Clone the repository in your $GOPATH/src/ directory. If you have used the bash script for setup, your $GOPATH variable should point to $HOME/go
  2. Follow the steps 2-6 only if you have to set-up databse by yourself. The MongoDB database is hosted on mLab free trial account for now and might expire. In that case, you’ll need the steps below.
  3. To run project locally, Install Mongo DB – https://www.mongodb.com/download-center?jmp=nav
  4. After installing Mongo DB, start it’s server by typing mongod in Terminal.
  5. Open a new tab in terminal and type mongo < dummyData.js to insert the dummmy product data.
  6. Open file store/repository.go, find the SERVER variable and replace the URL.
const SERVER = "http://localhost:27017"
  1. Last thing required to run the project, install all the go dependencies

// Library to handle jwt authentication 
$ go get "github.com/dgrijalva/jwt-go"

// Libraries to handle network routing
$ go get "github.com/gorilla/mux"
$ go get "github.com/gorilla/context"
$ go get "github.com/gorilla/handlers"

// mgo library for handling Mongo DB
$ go get "gopkg.in/mgo.v2"

Yay! Now we’re ready to run the API ?
8. Type export PORT=8000 in Terminal and open http://localhost:8000 in your browser to see the products.

API Documentation and Usage

It is recommended to install some extension to beautify JSON(like JSON Formatter) if you’re trying in a browser.

Important – Don’t forget to define $PORT in your shell variables.
Example: export PORT=8000

BASE_URL = "http://localhost:$PORT"
'OR'
BASE_URL = https://gruesome-monster-22811.herokuapp.com/

1. View Products

  • Endpoint NameIndex
  • MethodGET
  • URL Pattern/
  • Usage
    • Open BASE_URL in browser
    • Terminal/CURL
    curl -X GET BASE_URL
  • Expected Response – JSON containing all the products in database
  • Example
    Screenshot

2. View Single Product

  • Endpoint NameGetProduct
  • MethodGET
  • URL Pattern/products/{id}
  • Usage
    • Open BASE_URL/products/{id} in browser
    • Terminal/CURL
curl -X GET BASE_URL/products/{id} 
  • Expected Response – Product with the {id} in database
  • NOTE – There are only six(6) ids in the database, so 1 <= {id} <= 6
  • Example
    Screenshot

3. Search Product

  • Endpoint NameSearchProduct
  • MethodGET
  • URL Pattern/Search/{query}
  • Usage – Browser OR curl
  • BROWSER
    • Open BASE_URL/Search/{query} in browser
    • Terminal/CURL
    curl -X GET BASE_URL/Search/{query}
  • Expected Response – Products matching the search query
  • Example
    Screenshot

4. Authentication

For Adding, Updating and Deleting products from database you must send a JWT token in Authentication header.

  • Endpoint NameGetToken
  • MethodPOST
  • URL Pattern/get-token
  • Usage – CURL OR POSTMAN ONLY
    • Terminal/CURL

    curl -X POST \
    -H "Content-Type: application/json" \
    -d '{ username: "<YOUR_USERNAME>", password: "<RANDOM_PASSWORD>"}' \
    BASE_URL/get-token
  • Expected Response – A JWT Authentication Token as shown below
  • Example
    Screenshot

5. Add Product

  • Endpoint NameAddProduct
  • MethodPOST
  • URL Pattern/AddProduct
  • Usage – CURL OR POSTMAN ONLY
    • Terminal/CURL

    curl -X POST \
    -H "Authorization: Bearer <ACCESS_TOKEN>" \
    -d '{ "_id": 11, 
        "title": "Memes",
        "image": "I am selling memes, hehe.",          
        "price": 1,
        "rating": 5
        }' \
    BASE_URL/AddProduct
  • Expected Response – Addition successful without any error message. Check the logs in Terminal window which is running server.
  • Example
    Screenshot

6. Update Product

  • Endpoint NameUpdateProduct
  • MethodPUT
  • URL Pattern/UpdateProduct
  • Usage – CURL OR POSTMAN ONLY
    • Terminal/CURL

    curl -X PUT \
    -H "Authorization: Bearer <ACCESS_TOKEN>" \
    -d '{ "ID": 14, 
        "title": "Memes",
        "image": "I am not selling memes to you, hehe.",          
        "price": 1000,
        "rating": 5
        }' \
    BASE_URL/UpdateProduct
  • Expected Response – Update successful without any error message. Check the logs in Terminal window which is running server.
  • Example
    Screenshot

7. Delete Product

  • Endpoint NameDeleteProduct
  • MethodDELETE
  • URL Pattern/deleteProduct/{id}
  • Usage – CURL OR POSTMAN ONLY
    • Terminal/CURL

    curl -X DELETE \
    -H "Authorization: Bearer <ACCESS_TOKEN>" \
    BASE_URL/deleteProduct/{id}
  • Expected Response – Deletion successful without any error message. Check the logs in Terminal window which is running server.
  • Example
    Screenshot

TODO

  • Write unit tests to test every method
  • Improve the code by proper exception handling
  • Add repository badges like TravisCI, Better Code, Codacy etc.
  • Create a REST API server project using this package as a boilerplate
  • User and roles management
  • Session management using JWT tokens

GitHub

View Github