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/log_view.py

33 lines
1.1 KiB
Python
Raw Normal View History

2020-03-30 16:36:33 +02:00
from flask import request, current_app, Response
2020-03-30 17:00:59 +02:00
from flask_classful import FlaskView
2020-04-17 16:21:42 +02:00
from db import redis_client
2020-03-30 17:00:59 +02:00
2020-03-29 17:08:53 +02:00
class LogView(FlaskView):
2020-03-30 16:56:29 +02:00
def post(self):
2020-04-17 16:21:42 +02:00
# Record the IP address of the producer
remote_uuid = request.json['uuid']
remote_ip = request.remote_addr
prod_key = f"producer_{remote_uuid}"
last_known_remote_ip = redis_client.get(prod_key)
if last_known_remote_ip:
last_known_remote_ip = last_known_remote_ip.decode('utf-8')
if not last_known_remote_ip:
current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}")
elif last_known_remote_ip != remote_ip:
2020-05-08 21:04:13 +02:00
current_app.logger.info(
f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
2020-04-17 16:21:42 +02:00
# update expirity
redis_client.set(prod_key, remote_ip.encode('utf-8'))
2020-05-08 19:44:42 +02:00
redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"])
2020-04-17 16:21:42 +02:00
# print out message
2020-03-30 16:56:29 +02:00
current_app.logger.info(f"New message: {request.json['message']}")
2020-04-17 16:21:42 +02:00
2020-05-08 21:04:13 +02:00
return Response(status = 204)