This repository contains a simple minimal REST microservice boilerplate written in python using Flask. It is meant to be a boilerplate/template to clone and quickly start a scalable microservice application.
The service implements an API that maps RESTful calls to a mongoDB database. It implements read, create, delete, update of accounts records
in mongoDB, but you can pass in input any JSON file you want to insert
into the database (only the field id
is mandatory). The REST API is created using Flask,
Flask-Injector and Connexion.
Connexion is a python library to use OpenAPI, formerly Swagger, to describe and document RESTful APIs
using a design-first approach.
|
├── /account-microservice
│ ├── /api
| | ├── __init__.py <- Python init file
| | └── accounts.py <- Python file where methods are injected in the API
│ ├── /providers
| | └── MongoProvider.py <- Python file where methods are implemented
│ ├── /swagger
| | └── accounts-service-docs.yaml <- API Swagger file
│ ├── app.py <- Main python file to run the app
│ ├── Dockerfile <- Dockerfile used to build the image of the microservice
│ └── requirements.txt <- Requirements file with the list of the libraries needed
│
├── /img <- Folder containing the images for this README file
├── LICENSE <- License file
├── README.md <- This Readme file
└── docker-compose.yml <- Docker compose file, used to run the microservice
Run the following command in the root folder of the project (need docker and docker-composed installed).
docker-compose up
To consult the API documentation just type the following address in a browser.
http://localhost:2020/v1.0/ui/
Endpoints available at http://localhost:2020/v1.0/{URI}
:
Method | URI | Description |
---|---|---|
GET | /accounts/{user_id} | Retrieve data from the DB given an id |
POST | /accounts/createUser | Insert data into the DB (Any JSON file, id mandatory) |
PUT | /accounts/updateUser | Update data in the DB |
DELETE | /accounts/{user_id} | Delete data from the DB |
To customize the microservice for your own purpose, you have just to modify:
MongoProvider.py
This file contains the implementation of the API methods. In this case it contains the methods to create, delete, update and read a record from mongoDB. Customize it with your desired implementation.accounts.py
: This file describes the API methods and what implementation should they run.accounts-service-docs.yaml
: This file contains the OpenAPI specification that describe the RESTful API. Modify this file according to your needs changing paths of the API methods and keeping in mind to modifyparameters
andoperationId
depending on what you edited in theaccounts.py
file. Consult the official documentation to learn more about OpenAPI.
Run your modified microservice with the command:
docker-compose build && docker-compose up
To create a new microservice:
- Duplicate the
account-microservice
folder and rename the itXXXX-microservice
with the name of your new microservice. Customize your new microservice following the instruction in the previous section. - Modify the
docker-compose.yml
file duplicating theaccount-microservice
specifications and renaming themXXXX-microservice
. Keep in mind to modify also the parameters- PORT=YYYY
andports
with your desired exposed port.
Once again, run the microservices with the command:
docker-compose build && docker-compose up