Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SMART
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UNI-KLU
SMART
Commits
25716ce5
Commit
25716ce5
authored
Aug 13, 2019
by
Alexander Lercher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rest Gateway: Displaying received messages via rest
parent
067ea2ee
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
16 deletions
+49
-16
consumer.py
message-broker/rabbit-mq-test/consumer.py
+3
-2
MessageList.py
rest-gateway/app/MessageList.py
+25
-0
swagger.yml
rest-gateway/app/configs/swagger.yml
+11
-1
debug.py
rest-gateway/app/debug.py
+4
-1
main.py
rest-gateway/app/main.py
+6
-12
No files found.
message-broker/rabbit-mq-test/consumer.py
View file @
25716ce5
...
...
@@ -2,14 +2,15 @@ import pika
EXCHANGE_NAME
=
'rest-gateway'
EXCHANGE_TYPE
=
'direct'
ROUTING_KEY
=
'rest-gateway'
connection
=
pika
.
BlockingConnection
(
pika
.
ConnectionParameters
(
'143.205.173.36'
,
30302
,
heartbeat
=
600
,
blocked_connection_timeout
=
300
))
channel
=
connection
.
channel
()
channel
.
exchange_declare
(
exchange
=
EXCHANGE_NAME
,
exchange_type
=
EXCHANGE_TYPE
)
result
=
channel
.
queue_declare
(
queue
=
'rest-gateway'
,
exclusive
=
True
,
auto_delete
=
Tru
e
)
result
=
channel
.
queue_declare
(
queue
=
ROUTING_KEY
,
exclusive
=
False
,
auto_delete
=
Fals
e
)
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
):
print
(
f
"### Received: {body}"
)
...
...
rest-gateway/app/MessageList.py
0 → 100644
View file @
25716ce5
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
rest-gateway/app/configs/swagger.yml
View file @
25716ce5
...
...
@@ -29,4 +29,14 @@ paths:
responses
:
200
:
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
rest-gateway/app/debug.py
View file @
25716ce5
from
MessageList
import
MessageList
from
flask
import
request
def
echo
():
return
request
.
json
\ No newline at end of file
def
get_messages
():
messages
=
MessageList
.
getInstance
()
.
getMessages
()
return
str
(
messages
)
rest-gateway/app/main.py
View file @
25716ce5
...
...
@@ -17,21 +17,22 @@ import connexion
from
multiprocessing
import
Process
from
messaging.MessageReceiver
import
MessageReceiver
from
messaging.MessageSender
import
MessageSender
from
MessageList
import
MessageList
RABBIT_MQ_DNS_NAME
=
'rabbit-mq'
RABBIT_MQ_PORT
=
'5672'
message
_sender
=
None
message
s
=
MessageList
.
getInstance
()
# init message handler
def
message_received_callback
(
channel
,
method
,
properties
,
body
):
messages
.
appendMessage
(
body
)
print
(
f
"### Received: {body}"
)
def
pika_error_callback
(
error
):
# TODO gracefully handle error
print
(
"Rabbit MQ error!"
)
global
flask_server
flask_server
.
terminate
()
sys
.
exit
()
os
.
_exit
(
1
)
# load swagger config
app
=
connexion
.
App
(
__name__
,
specification_dir
=
'configs/'
)
...
...
@@ -41,9 +42,6 @@ app.add_api('swagger.yml')
def
api_root
():
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
if
__name__
==
'__main__'
:
message_rec
=
MessageReceiver
(
...
...
@@ -51,8 +49,4 @@ if __name__ == '__main__':
exchange_name
=
'rest-gateway'
,
exchange_type
=
'direct'
,
queue_name
=
'rest-gateway'
,
auto_ack
=
True
)
message_rec
.
start
(
message_received_callback
,
pika_error_callback
)
global
flask_server
flask_server
=
Process
(
target
=
run_flask
)
flask_server
.
start
()
flask_server
.
join
()
\ No newline at end of file
app
.
run
(
host
=
'0.0.0.0'
,
port
=
5000
,
debug
=
True
,
use_reloader
=
False
)
# disable reloader so only subscribed once to rabbitmq
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment