From 6f3ef28fb5af4c110b3428d6b3567c34c1556610 Mon Sep 17 00:00:00 2001 From: marcsello Date: Sun, 29 Mar 2020 18:12:16 +0200 Subject: [PATCH 01/10] 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) From b04d0d60d071ab5749e2c401d1d0b843f39f38df Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:32:33 +0200 Subject: [PATCH 02/10] consumer and log views --- consumer_api/views/consumers_view.py | 6 +++++- consumer_api/views/log_view.py | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index feb59ef..5f0c6f5 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -1,7 +1,11 @@ +import json +from flask import request, current_app, jsonify +from db import redis_client from flask_classful import FlaskView class ConsumersView(FlaskView): def get(self): - return "ConsumersView" + consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + return [v['ip'] for k, v in consumer_list.items()] diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index fc589f4..eda8437 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -1,7 +1,9 @@ from flask_classful import FlaskView - +from flask import request, current_app, jsonify class LogView(FlaskView): def get(self): - return "LogView" \ No newline at end of file + remote_uuid = request.json['uuid'] + current_app.logger.info(f"New message from {remote_uuid}: {request.message}") + return jsonify(success = True) From 244b779107e3c3e7aa9654d0992a8b567a875fdc Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:35:54 +0200 Subject: [PATCH 03/10] return 200 --- consumer_api/views/log_view.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index eda8437..99c762e 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -6,4 +6,5 @@ class LogView(FlaskView): def get(self): remote_uuid = request.json['uuid'] current_app.logger.info(f"New message from {remote_uuid}: {request.message}") - return jsonify(success = True) + return 200 + \ No newline at end of file From 0c3161ef034116b6e6c692efce6ad18256fbaf9f Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:36:33 +0200 Subject: [PATCH 04/10] 204 --- consumer_api/views/log_view.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index 99c762e..1ce37d1 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -1,10 +1,9 @@ from flask_classful import FlaskView -from flask import request, current_app, jsonify +from flask import request, current_app, Response class LogView(FlaskView): def get(self): remote_uuid = request.json['uuid'] current_app.logger.info(f"New message from {remote_uuid}: {request.message}") - return 200 - \ No newline at end of file + return Response(status=204) From 3c8229238c9e9f110486966a05ce40cfc7d84f5c Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:37:12 +0200 Subject: [PATCH 05/10] jsonify --- consumer_api/views/consumers_view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index 5f0c6f5..d8d9fa1 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -8,4 +8,4 @@ class ConsumersView(FlaskView): def get(self): consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) - return [v['ip'] for k, v in consumer_list.items()] + return jsonify([v['ip'] for k, v in consumer_list.items()]) From 68e2eaffcba9023b9befeff266545d92a2a5eaa4 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:52:15 +0200 Subject: [PATCH 06/10] message --- consumer_api/views/log_view.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index 1ce37d1..808c98e 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -4,6 +4,5 @@ from flask import request, current_app, Response class LogView(FlaskView): def get(self): - remote_uuid = request.json['uuid'] - current_app.logger.info(f"New message from {remote_uuid}: {request.message}") + current_app.logger.info(f"New message from: {request.json['message']}") return Response(status=204) From 0c18cf9bf1552afb0c1c43b938aeae0ba8366799 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 16:56:29 +0200 Subject: [PATCH 07/10] message --- consumer_api/views/log_view.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index 808c98e..c63e682 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -3,6 +3,6 @@ from flask import request, current_app, Response class LogView(FlaskView): - def get(self): - current_app.logger.info(f"New message from: {request.json['message']}") + def post(self): + current_app.logger.info(f"New message: {request.json['message']}") return Response(status=204) From f8feb4072350520e94df185a68e888ee68756270 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 30 Mar 2020 17:00:59 +0200 Subject: [PATCH 08/10] minor --- consumer_api/views/consumers_view.py | 3 ++- consumer_api/views/log_view.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index d8d9fa1..c12739c 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -1,6 +1,7 @@ import json -from flask import request, current_app, jsonify + from db import redis_client +from flask import jsonify from flask_classful import FlaskView diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index c63e682..abfff93 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -1,8 +1,9 @@ -from flask_classful import FlaskView from flask import request, current_app, Response +from flask_classful import FlaskView + class LogView(FlaskView): def post(self): current_app.logger.info(f"New message: {request.json['message']}") - return Response(status=204) + return Response(status = 204) From 269c08e5dee8f112264bc2996217c6188df1a912 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 1 Apr 2020 00:28:04 +0200 Subject: [PATCH 09/10] port 80 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 03d458c..273e267 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,5 +9,5 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN pip3 install -r requirements.txt -EXPOSE 8000 -CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"] +EXPOSE 80 +CMD ["gunicorn", "-b", "0.0.0.0:80", "app:app"] From 93beb10c49538938e826a7fff406c07127a22fac Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 1 Apr 2020 00:31:13 +0200 Subject: [PATCH 10/10] dev --- consumer_api/views/consumers_view.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index c12739c..0582bc4 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -1,7 +1,7 @@ import json from db import redis_client -from flask import jsonify +from flask import jsonify, current_app from flask_classful import FlaskView @@ -9,4 +9,5 @@ class ConsumersView(FlaskView): def get(self): consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + current_app.logger.warning(jsonify([v['ip'] for k, v in consumer_list.items()])) return jsonify([v['ip'] for k, v in consumer_list.items()])