Rest-and-go-master - A basic online store API written to learn Go Programming Language

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 Heroku - https://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#community
  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 Name - Index
  • Method - GET
  • 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 Name - GetProduct
  • Method - GET
  • 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 Name - SearchProduct
  • Method - GET
  • 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 Name - GetToken
  • Method - POST
  • 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 Name - AddProduct
  • Method - POST
  • 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 Name - UpdateProduct
  • Method - PUT
  • 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 Name - DeleteProduct
  • Method - DELETE
  • 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
Similar Resources

Building basic API with go, gin and gorm

Project Description Terima kasih sudah berkunjung ke halaman repositori ini, repositori ini berisi basic RESTFUL API dengan menggunakan teknologi seba

Nov 20, 2021

A REST web-service sample project written in Golang using go-fiber, GORM and PostgreSQL

backend A REST web-service sample project written in Golang using go-fiber, GORM and PostgreSQL How to run Make sure you have Go installed (download).

Jan 1, 2023

Go WhatsApp REST API Implementation Using Fiber And Swagger

Go WhatsApp REST API Implementation Using Fiber And Swagger

Go WhatsApp REST API Implementation Using Fiber And Swagger Package cooljar/go-whatsapp-fiber Implements the WhatsApp Web API using Fiber web framewor

May 9, 2022

REST API boilerplate built with go and clean architecture - Echo Framework

GO Boilerplate Prerequisite Install go-migrate for running migration https://github.com/golang-migrate/migrate App requires 2 database (postgreSQL an

Jan 2, 2023

Example Golang API backend rest implementation mini project Point Of Sale using Gin Framework and Gorm ORM Database.

Example Golang API backend rest implementation mini project Point Of Sale using Gin Framework and Gorm ORM Database.

Dec 23, 2022

Go REST API - Bucket list - built with go-chi, Docker, and PostgreSQL

Go REST API - Bucket list - built with go-chi, Docker, and PostgreSQL Requirements Docker and Go golang-migrate/migrate Usage Clone the repository wit

Dec 14, 2021

Simple REST-API implementation using Golang with several packages (Echo, GORM) and Docker

Simple REST-API Boilerplate This is a simple implementation of REST-API using Golang and several packages (Echo and GORM). By default, I use PostgreSQ

Sep 13, 2022

Kubewrap - kubewrap is an kubernetes command line utility with a focus to explore the kubernetes REST API's leveraging the go libraries available along with golang and cobra packages.

Kubewrap kubewrap is an kubernetes cli wrapper with a focus to explore the kubernetes REST API's leveraging the go libraries available along with gola

Nov 20, 2022

Opinionated Go starter with gin for REST API, logrus for logging, viper for config with added graceful shutdown

go-gin-starter An opinionated starter for Go Backend projects using: gin-gonic/gin as the REST framework logrus for logging viper for configs Docker f

Dec 2, 2022
REST API made using native Golang libraries. This API resembles the basic working of Instagram.
REST API made using native Golang libraries. This API resembles the basic working of Instagram.

Golang RESTful API for Instagram A Go based REST API built using native libraries. The API has been thoroughly worked through with Postman. Routes inc

Mar 16, 2022
Short basic introduction to Go programming language.
Short basic introduction to Go programming language.

Go, Go! Short basic introduction to Go v1.17.6 programming language Go Logo is Copyright 2018 The Go Authors. All rights reserved. About This work was

May 30, 2022
REST Layer, Go (golang) REST API framework
REST Layer, Go (golang) REST API framework

REST Layer REST APIs made easy. REST Layer is an API framework heavily inspired by the excellent Python Eve. It helps you create a comprehensive, cust

Dec 16, 2022
A very basic REST service for JSON data - enough for prototyping and MVPs!

caffeine - minimum viable backend A very basic REST service for JSON data - enough for prototyping and MVPs! Features: no need to set up a database, a

Dec 24, 2022
REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base
REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base

Backend API Example FiberGo Framework Docs : https://github.com/gofiber Info This application using firebase ecosystem Firebase Auth Cloud Storage Fir

May 31, 2022
Go-app is a package to build progressive web apps with Go programming language and WebAssembly.
Go-app is a package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package to build progressive web apps with Go programming language and WebAssembly.

Dec 30, 2022
beego is an open-source, high-performance web framework for the Go programming language.
beego is an open-source, high-performance web framework for the Go programming language.

Beego Beego is used for rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services. It is inspired by To

Jan 1, 2023
beego is an open-source, high-performance web framework for the Go programming language.
beego is an open-source, high-performance web framework for the Go programming language.

Beego Beego is used for rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services. It is inspired by To

Jan 8, 2023
letgo is an open-source, high-performance web framework for the Go programming language.

high-performance Lightweight web framework for the Go programming language. golang web framework,高可用golang web框架,go语言 web框架 ,go web

Sep 23, 2022