Merge pull request 'Add saving IP addresses of producers' (#5) from dev-prodip into dev
This commit is contained in:
commit
f39fa6f936
@ -1,9 +1,31 @@
|
|||||||
from flask import request, current_app, Response
|
from flask import request, current_app, Response
|
||||||
from flask_classful import FlaskView
|
from flask_classful import FlaskView
|
||||||
|
from db import redis_client
|
||||||
|
|
||||||
|
|
||||||
class LogView(FlaskView):
|
class LogView(FlaskView):
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
|
# 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:
|
||||||
|
current_app.logger.info(f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
|
||||||
|
|
||||||
|
# update expirity
|
||||||
|
redis_client.set(prod_key, remote_ip.encode('utf-8'))
|
||||||
|
redis_client.expire(prod_key, 240)
|
||||||
|
|
||||||
|
# print out message
|
||||||
current_app.logger.info(f"New message: {request.json['message']}")
|
current_app.logger.info(f"New message: {request.json['message']}")
|
||||||
return Response(status = 204)
|
|
||||||
|
return Response(status=204)
|
||||||
|
Reference in New Issue
Block a user