Python Microservice Scaffold is an example of how to structure a Flask Microservice Project. This Scaffold is build over PyMS package. PyMS is a Microservice chassis pattern like Spring Boot (Java) or Gizmo (Golang). PyMS is a collection of libraries, best practices and recommended ways to build microservices with Python which handles cross-cutting concerns:
- Externalized configuration
- Logging
- Health checks
- Metrics
- Distributed tracing
git clone [email protected]:purwowd/microservices-scaffold.git
cd microservices-scaffold
virtualenv --python=python[3.6|3.7|3.8] venv
source venv/bin/activate
pip install -r requirements.txt
pip install pipenv
pipenv install
virtualenv -p python3 venv
source venv/bin/activate
pip3 install -r requirements.txt
python manage.py runserver
Pipenv generates two files: a Pipfile
and a Pipfile.lock
.
Pipfile
: Is a high level declaration of the dependencies of your project. It can contain "dev" dependencies (usually test related stuff) and "standard" dependencies which are the ones you'll need for your project to functionPipfile.lock
: Is the "list" of all the dependencies your Pipfile has installed, along with their version and their hashes. This prevents two things: Conflicts between dependencies and installing a malicious module.
For a more in-depth explanation please refer to the official documentation.
python manage.py runserver
Your default endpoints will be in this url:
http://127.0.0.1:5000/films
http://127.0.0.1:5000/actors
This URL is set in your config.yml
:
pyms:
config:
DEBUG: false
TESTING: false
APP_NAME: Template
APPLICATION_ROOT : "" # <!---
You can acceded to a swagger ui in the next url:
http://127.0.0.1:5000/ui/
This PATH is set in your config.yml
:
pyms:
services:
swagger:
path: "swagger"
file: "swagger.yaml"
url: "/ui/" # <!---
Read more info in the documentation page: https://microservices-scaffold.readthedocs.io/en/latest/
You can dockerize this microservice with these steps:
-
Create and push the image
docker build -t films -f Dockerfile .
-
Run the image:
docker run -d -p 5000:5000 films
You can run this microservice in a Kubernetes cluster with:
kubectl apply -f service.yaml
We appreciate opening issues and pull requests to make PyMS even more stable & useful! See This doc for more details