gin-gonic-rest-mongodb

command module
v0.0.0-...-6fbf478 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2022 License: MIT Imports: 12 Imported by: 0

README

gin-gonic REST API using MongoDB

A simple Gin Gonic REST API using MongoDB & Redis

This a gin-gonic application, to provide an example on how to create REST API, integrated with MongoDB in Dropwizard with an OpenAPI specification.

This example is a simple RESTful API to easily manage books I have read.

This is meant to be a playground project for all things gin-gonic, so the design or project structure is not something I would advocate.

This is my take on Building Distributed Applications in Gin repository and (a great) book by Mohamed Labouardy.

Dependencies used:

  • using go 1.19
  • using gin-gonic v1.7.7 web framework
  • using viper as a configuration solution
  • using mongo-db as NoSQL DB
  • using redis to cache GET /books and GET /books/:id resources
  • using gin/sessions to handle session cookies
  • using jwt-go to provide an implementation of JWT
  • using x/crypto, Go Cryptography package
  • using nancy, tool to check for vulnerabilities in your Golang dependencies

How to start the Gin-gonic application

go run main.go

# or via docker-compose
docker-compose up

How to run audit and tests

make audit test

Swagger OpenAPI specification

  • To generate the swagger spec file and have the API spec served from the Swagger UI
swagger generate spec --scan-models -o ./swagger.json && swagger serve  --port 8081 --path docs -F swagger ./swagger.json

CURL commands to interact with the REST API

Get all the books
curl -X GET 'localhost:8080/books'
Search book by tag
curl -X GET 'localhost:8080/books/search?tag=nasa'
Create a new book entry
curl -X POST 'localhost:8080/books' \
--data '{"name": "Moondust", "author": "Andrew Smith", "publisher": "Bloomsbury Publishing PLC", "published_at": {"month":"July", "year":"2009"}, "tags":["space exploration", "astronauts", "nasa"], "review":4.6}'
Update a book entry
curl -X PUT 'localhost:8080/books/c8n5pb2kq9ndfcl9os7g' \
--data '{"name": "Moondust", "author": "Andrew Smith", "publisher": "Bloomsbury Publishing PLC", "published_at": {"month":"July", "year":"2009"}, "tags":["space exploration", "astronauts", "nasa", "JPL"], "review":4.7}'
Delete a book entry
curl -X DELETE 'localhost:8080/books/c8n5pb2kq9ndfcl9os7g'

Required Docker images

Create a MongoDB container

docker pull mongo
docker create --name mongodb -it -p 27017:27017 mongo

Create a Redis container

docker pull redis
docker create --name redis -it -p 6379:6379 redis

Documentation

Overview

Documentation of our Books API

	   Simple Gin API

    Schemes: http
    Host: localhost:8080
    BasePath: /
    Version: 1.0.0
    Contact: Test User <some_email@example.com> http://github.com/

    Consumes:
    - application/json

    Produces:
    - application/json

    Security:
    - basic

   SecurityDefinitions:
   basic:
     type: basic

swagger:meta

Directories

Path Synopsis
web

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL