A simple web server streaming up-to-date exchange rates to the subscribed clients via websocket streaming
Subscribe to the server by sending a websocket connection message
- Rename
compose/envs/sample.common.env
intocompose/envs/common.env
- Set the missing .env variables with values required for the project to run, e.g.,
EMCONT_EXCHANGE_RATES_URL
Pull the images
docker compose pull
Build the images
docker compose build
Run the docker compose configuration
docker compose up
Run asynchornous tasks manually:
docker compose exec backend bash
poetry run python async_tasks/async_periodic_tasks.py
Connect to the Mongo DB instance:
docker compose exec db sh
mongosh $MONGO_CONNECTION_URI
show dbs
to list all the databases
use db
to switch to the backend database
The only application endpoint is
Endpoint: "/"
message: "{"action": "assets", "message": {}}"
Response sample:
{
"action": "assets",
"message": {
"assets": [
{
"id": 1,
"name": "EURUSD"
},
{
"id": 2,
"name": "USDJPY"
}
]
}
}
Description:
Subscribe to the incoming Exchange Rates records live data by:
1. Retreiving Exchange Rates for the last 30 minutes;
2. Receiving Exchange Rate data live.
Endpoint: "/"
Message: "{"action": "subscribe", "message": {"assetId": 1}}"
Response:
- The initial response is exchange rate records per second for last 30 minutes or 1800 seconds.
Response sample:
{
"message": {
"points": [
{
"assetName": "EURUSD",
"time": 1455883484,
"assetId": 1,
"value": 1.110481
},
{
"assetName": "EURUSD",
"time": 1455883485,
"assetId": 1,
"value": 1.110948
}
]
},
"action": "asset_history"
}
- The incoming live Exchange Rate records:
Response per each row:
{
"action": "point",
"message": {
"assetName": "EURUSD",
"time": 1455883484,
"assetId": 1,
"value": 1.110481
}
}
- Python
- Poetry
- FastAPI
- Beanie
- Motor
- pytest / pytest-asyncio
- MongoDB
- Docker / Docker-compose
The app components are technically described by the docker-compose.yaml
file.
The main FastAPI web server to handle incomming connections.
The only websocket URI it serves is ws://0.0.0.0:8080/
- the websocket connection function described in Websocket actions.
The asynchronous tasks running in a single thread to:
- fetch the exchange rates data from an externcal resource;
- save the results into the databasse.
The primary goal of the asynchonorous tasks is to provide fresh exchange rate records per each second.
A MongoDB DBMS instance to store and serve the application data in form of documents.
Install pre-commit
poetry add pre-commit
Init pre-commit by installing the hooks
poetry run pre-commit install
Test with Mypy against the code (src/
) depending on the automatically imported mypy.ini
file:
poetry run mypy src