Commit 25716ce5 authored by Alexander Lercher's avatar Alexander Lercher

Rest Gateway: Displaying received messages via rest

parent 067ea2ee
...@@ -2,14 +2,15 @@ import pika ...@@ -2,14 +2,15 @@ import pika
EXCHANGE_NAME = 'rest-gateway' EXCHANGE_NAME = 'rest-gateway'
EXCHANGE_TYPE = 'direct' EXCHANGE_TYPE = 'direct'
ROUTING_KEY = 'rest-gateway'
connection = pika.BlockingConnection(pika.ConnectionParameters('143.205.173.36', 30302, heartbeat=600, blocked_connection_timeout=300)) connection = pika.BlockingConnection(pika.ConnectionParameters('143.205.173.36', 30302, heartbeat=600, blocked_connection_timeout=300))
channel = connection.channel() channel = connection.channel()
channel.exchange_declare(exchange=EXCHANGE_NAME, exchange_type=EXCHANGE_TYPE) channel.exchange_declare(exchange=EXCHANGE_NAME, exchange_type=EXCHANGE_TYPE)
result = channel.queue_declare(queue='rest-gateway', exclusive=True, auto_delete=True) result = channel.queue_declare(queue=ROUTING_KEY, exclusive=False, auto_delete=False)
queue_name = result.method.queue queue_name = result.method.queue
channel.queue_bind(exchange=EXCHANGE_NAME, queue=queue_name, routing_key='rest-gateway') channel.queue_bind(exchange=EXCHANGE_NAME, queue=queue_name, routing_key=ROUTING_KEY)
def callback(ch, method, properties, body): def callback(ch, method, properties, body):
print(f"### Received: {body}") print(f"### Received: {body}")
......
from __future__ import annotations
class MessageList:
__instance = None
_messages = []
@staticmethod
def getInstance() -> MessageList:
""" Static access method. """
if MessageList.__instance == None:
MessageList.__instance = MessageList()
return MessageList.__instance
def __init__(self):
""" Virtually private constructor. """
if MessageList.__instance != None:
raise Exception("This class is a singleton!")
else:
MessageList.__instance = self
def appendMessage(self, message):
self._messages.append(message)
def getMessages(self):
return self._messages
\ No newline at end of file
...@@ -29,4 +29,14 @@ paths: ...@@ -29,4 +29,14 @@ paths:
responses: responses:
200: 200:
description: "Successful echo of request data" description: "Successful echo of request data"
\ No newline at end of file /messages:
get:
operationId: "debug.get_messages"
tags:
- "Received Messages"
summary: "List of all received messages"
description: "Lists all messages received from Rabbit MQ."
responses:
200:
description: "Message list"
\ No newline at end of file
from MessageList import MessageList
from flask import request from flask import request
def echo(): def echo():
return request.json return request.json
def get_messages():
\ No newline at end of file messages = MessageList.getInstance().getMessages()
return str(messages)
...@@ -17,21 +17,22 @@ import connexion ...@@ -17,21 +17,22 @@ import connexion
from multiprocessing import Process from multiprocessing import Process
from messaging.MessageReceiver import MessageReceiver from messaging.MessageReceiver import MessageReceiver
from messaging.MessageSender import MessageSender from messaging.MessageSender import MessageSender
from MessageList import MessageList
RABBIT_MQ_DNS_NAME = 'rabbit-mq' RABBIT_MQ_DNS_NAME = 'rabbit-mq'
RABBIT_MQ_PORT = '5672' RABBIT_MQ_PORT = '5672'
message_sender = None messages = MessageList.getInstance()
# init message handler # init message handler
def message_received_callback(channel, method, properties, body): def message_received_callback(channel, method, properties, body):
messages.appendMessage(body)
print(f"### Received: {body}") print(f"### Received: {body}")
def pika_error_callback(error): def pika_error_callback(error):
# TODO gracefully handle error
print("Rabbit MQ error!") print("Rabbit MQ error!")
global flask_server os._exit(1)
flask_server.terminate()
sys.exit()
# load swagger config # load swagger config
app = connexion.App(__name__, specification_dir='configs/') app = connexion.App(__name__, specification_dir='configs/')
...@@ -41,9 +42,6 @@ app.add_api('swagger.yml') ...@@ -41,9 +42,6 @@ app.add_api('swagger.yml')
def api_root(): def api_root():
return 'Endpoint of SMART RESTful API Gateway!' return 'Endpoint of SMART RESTful API Gateway!'
def run_flask():
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False) # disable reloader so only subscribed once to rabbitmq
# start app # start app
if __name__ == '__main__': if __name__ == '__main__':
message_rec = MessageReceiver( message_rec = MessageReceiver(
...@@ -51,8 +49,4 @@ if __name__ == '__main__': ...@@ -51,8 +49,4 @@ if __name__ == '__main__':
exchange_name='rest-gateway', exchange_type='direct', queue_name='rest-gateway', auto_ack=True) exchange_name='rest-gateway', exchange_type='direct', queue_name='rest-gateway', auto_ack=True)
message_rec.start(message_received_callback, pika_error_callback) message_rec.start(message_received_callback, pika_error_callback)
global flask_server app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False) # disable reloader so only subscribed once to rabbitmq
flask_server = Process(target=run_flask) \ No newline at end of file
flask_server.start()
flask_server.join()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment