Quick example how to use ProxyMiddleware with Flask Apps
The purpose of this project is to present how to call different API versions using same host address.
It contains two Flask applications:
- one with
/v1/helloendpoint -app_v1.py - the other with
/v2/helloendpoint -app_v2.py
What we want to achieve is when we call these enpoints, the Middleware application will redirect our request to appriopriate Flask application.
- python3.10
- docker
- docker compose
Simple flask applications with one endpoint /hello.
Middleware layer - groups up our applications and redirects requests to them.
Install requirements:
pip install -r requirements.txtor for running tests:
pip install -r requirements.txt -r requirements-test.txtRun applications:
python app_v1.py
python app_v1.py
python app_middleware.pyThen run in browser:
http://127.0.0.1:5555/v1/hello- returns{"msg":"Hello from app v1"}http://127.0.0.1:5555/v2/hello- returns{"msg":"Hello from app v2"}
If you run this manually you can also check:
http://127.0.0.1:5000/v1/hellohttp://127.0.0.1:5001/v2/hello
Remember to check if ports 5000, 5001 and 5555 are available.
Build images and run compose:
docker compose build
docker compose up -dNow you can run in browser:
http://127.0.0.1:5555/v1/hello- returns{"msg":"Hello from app v1"}http://127.0.0.1:5555/v2/hello- returns{"msg":"Hello from app v2"}
In this case check if 5555 port is available, as app_v1 and app_v2 are not available outside the docker compose.