From 6f3ef28fb5af4c110b3428d6b3567c34c1556610 Mon Sep 17 00:00:00 2001 From: marcsello Date: Sun, 29 Mar 2020 18:12:16 +0200 Subject: [PATCH] Implemented sync view --- consumer_api/app.py | 1 + consumer_api/views/sync_view.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/consumer_api/app.py b/consumer_api/app.py index a1ceecf..dbedea3 100644 --- a/consumer_api/app.py +++ b/consumer_api/app.py @@ -18,6 +18,7 @@ sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10") app = Flask(__name__) app.config['REDIS_URL'] = os.environ['REDIS_URL'] +app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID'] redis_client.init_app(app) diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index 6f17796..a6f8eb5 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -1,7 +1,32 @@ +import json +from flask import request, current_app, jsonify from flask_classful import FlaskView +from db import redis_client # ez nagyon otvar class SyncView(FlaskView): - def get(self): - return "SyncView" + def post(self): + remote_uuid = request.json['uuid'] + remote_ip = request.remote_addr + + consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + + # Log something about it + if remote_uuid not in consumer_list.keys(): + current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}") + else: # known + if consumer_list[remote_uuid]['ip'] != remote_ip: + current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}") + + consumer_list.update( + {remote_uuid: {"ip": remote_ip}} + ) + + redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8')) + + response = { + "uuid": current_app.config['LOCAL_UUID'] + } + + return jsonify(response) -- 2.45.2