Skip to content

PauloPortugal/gin-gonic-rest-mongodb

Repository files navigation

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

Releases

No releases published

Packages

No packages published

Languages