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
andGET /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
go run main.go
# or via docker-compose
docker-compose up
make audit test
- 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 -X GET 'localhost:8080/books'
curl -X GET 'localhost:8080/books/search?tag=nasa'
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}'
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}'
curl -X DELETE 'localhost:8080/books/c8n5pb2kq9ndfcl9os7g'
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