mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 13:03:05 +02:00
git-subtree-dir: Programming/PSD git-subtree-mainline:781bfce7b1git-subtree-split:cc1989eb7b
32 lines
826 B
Python
32 lines
826 B
Python
from flask import Flask, jsonify
|
|
from confluent_kafka import Consumer, KafkaError
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/alarms', methods=['GET'])
|
|
def get_alarms():
|
|
alarms = []
|
|
conf = {'bootstrap.servers': "localhost:9092",
|
|
'group.id': "alarm_group",
|
|
'auto.offset.reset': 'earliest'}
|
|
|
|
consumer = Consumer(**conf)
|
|
consumer.subscribe(['alarms'])
|
|
|
|
while True:
|
|
msg = consumer.poll(timeout=1.0)
|
|
if msg is None:
|
|
break
|
|
if msg.error():
|
|
if msg.error().code() == KafkaError._PARTITION_EOF:
|
|
continue
|
|
else:
|
|
return str(msg.error()), 500
|
|
alarms.append(json.loads(msg.value().decode('utf-8')))
|
|
|
|
consumer.close()
|
|
return jsonify(alarms)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|