This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
consumer-api/consumer_api/views/sync_view.py
Fabian 60c6698495
All checks were successful
continuous-integration/drone/push Build is passing
comments added
2020-04-04 13:53:26 +02:00

35 lines
1.2 KiB
Python

import json
from flask import request, current_app, jsonify
from flask_classful import FlaskView
from db import redis_client
class SyncView(FlaskView):
def post(self):
remote_uuid = request.json['uuid']
remote_ip = request.remote_addr
# load the currently available consumer list from the redis database
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
if remote_uuid not in consumer_list.keys():
# display newly registered consumer
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
else:
if consumer_list[remote_uuid]['ip'] != remote_ip:
# log address changes
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
# update consumer list redis databasse
consumer_list.update(
{remote_uuid: {"ip": remote_ip}}
)
redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8'))
# return with the current UUID
response = {
"uuid": current_app.config['LOCAL_UUID']
}
return jsonify(response)