-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
262 lines (182 loc) · 7.06 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
# -------------------------------------------------------------
# Notifications center - Delivery API
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Author: Sébastien Santoro aka Dereckson
# Project: Nasqueron
# Description: AMQP to HTTP gateway
# Created: 2017-05-22
# Dependencies: Pika, direct access to the broker
# -------------------------------------------------------------
"""
This module connects to the message broker, fires a web server,
and allows to interact through the broker from HTTP requests.
"""
from flask import Flask, abort, jsonify, request
import uuid
import pika
import os
import sys
# -------------------------------------------------------------
# Configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
app = Flask(__name__)
service = {}
endpoint = "/delivery"
def mandatory_environment_variables():
return ["BROKER_HOST", "BROKER_USERNAME", "BROKER_PASSWORD"]
def check_config():
"""Ensures configuration is complete."""
return set(mandatory_environment_variables()).issubset(os.environ)
def get_config():
return {
"Broker": get_broker_config(),
"DefaultExchange": get_default_exchange(),
}
def get_broker_config():
return {
"Host": os.environ["BROKER_HOST"],
"User": os.environ["BROKER_USERNAME"],
"Password": os.environ["BROKER_PASSWORD"],
"Vhost": get_broker_vhost(),
}
def get_broker_vhost():
if "BROKER_VHOST" in os.environ:
return os.environ["BROKER_VHOST"]
return "/"
def get_default_exchange():
if "DEFAULT_EXCHANGE" in os.environ:
return os.environ["DEFAULT_EXCHANGE"]
return None
# -------------------------------------------------------------
# Broker connection helper methods
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_credentials(config):
"""Gets credentials to connect to the broker from the configuration."""
return pika.PlainCredentials(
username=config['Broker']['User'],
password=config['Broker']['Password'],
erase_on_connect=True
)
def get_broker_connection_parameters(config):
return pika.ConnectionParameters(
host=config['Broker']['Host'],
credentials=get_credentials(config),
virtual_host=config['Broker']['Vhost'])
def get_broker_connection(config):
"""Connects to the broker."""
parameters = get_broker_connection_parameters(config)
return pika.BlockingConnection(parameters)
# -------------------------------------------------------------
# API helper methods
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_default_exchange_name():
"""Gets from the configuration the default exchange,
or return a 400 if undefined."""
if service['config']['DefaultExchange'] is None:
print("Client wants the default exchange, unspecified in config.")
abort(400)
return service['config']['DefaultExchange']
def generate_queue_key():
"""Generates an API key matching a specific queue."""
return str(uuid.uuid4())
def get_queue_name(queue_key):
"""Maps a queue name with its key."""
return "delivery-" + queue_key
def add_broker_queue(exchange_name, routing_key):
"""Adds a queue to the broker, bind to the exchange."""
queue_key = generate_queue_key()
queue_name = get_queue_name(queue_key)
connection = get_broker_connection(service['config'])
channel = connection.channel()
channel.queue_declare(durable=True,
queue=queue_name)
try:
channel.queue_bind(exchange=exchange_name,
queue=queue_name,
routing_key=routing_key)
except pika.exceptions.ChannelClosed as err:
# We've created a queue but we can't use it,
# so let's try to delete it through a new connection.
try:
print("Deleting not used queue: {0}".format(queue_name))
delete_broker_queue(queue_name, True)
except pika.exceptions.ChannelClosed:
pass
raise err
connection.close(reply_text="Operation done")
return queue_key
def delete_broker_queue(queue_name, force_queue_deletion):
connection = get_broker_connection(service['config'])
channel = connection.channel()
channel.queue_delete(queue=queue_name, if_empty=not force_queue_deletion)
connection.close(reply_text="Operation done")
def get_variable_from_request(key, default_value=None):
"""Gets variable from request JSON payload or a default value."""
if key in request.json:
return request.json[key]
return default_value
def get_exchange_from_request():
"""Gets the exchange name from the request, the environment, or aborts."""
exchange_name = get_variable_from_request("exchange")
if exchange_name is None:
return get_default_exchange_name()
return exchange_name
def error_handler(err):
"""Logs the exception, return 400"""
print("Channel error: {0}".format(err))
abort(400)
# -------------------------------------------------------------
# API methods
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@app.route(endpoint + "/status")
def status():
"""Determines if the application returns a 200 on GET request."""
return "ALIVE"
@app.route(endpoint + "/register_consumer", methods=['POST'])
def register_consumer():
"""Subscribes to an exchange, records the queue, sends queue key."""
# Reads request
if not request.json:
abort(400)
exchange_name = get_exchange_from_request()
routing_key = get_variable_from_request("routing-key", "*")
# Handles request
try:
queue_key = add_broker_queue(exchange_name, routing_key)
except pika.exceptions.ChannelClosed as err:
return error_handler(err)
# Returns result
return jsonify(key=queue_key)
@app.route(endpoint + "/unregister_consumer", methods=['POST'])
def unregister_consumer():
"""Unregister a queue key."""
# Reads request
if not request.json or "key" not in request.json:
abort(400)
queue = get_queue_name(request.json["key"])
force_delete = get_variable_from_request("force", False)
# Handles request
try:
delete_broker_queue(queue, force_delete)
return jsonify(key=queue, success=True,
result="Queue deletion request sent to the broker.")
except pika.exceptions.ChannelClosed as err:
return error_handler(err)
# -------------------------------------------------------------
# Start application
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def initialize_application():
"""Initializes a container with required services."""
return {
'config': get_config()
}
def run_application(web_application):
"""Runs the server."""
if not check_config():
sys.exit(1)
global service
service = initialize_application()
web_application.run(host="0.0.0.0")
if __name__ == '__main__':
run_application(app)