diff --git a/Pipfile b/Pipfile index 830c70e828ea94de913153e67d7e8b437d5688f8..9510ffcab80bc3b23d4c2141c37355aededd7388 100644 --- a/Pipfile +++ b/Pipfile @@ -11,6 +11,7 @@ flask = "*" flask-restful = "*" marshmallow = "*" bson = "*" +flask-cors = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 6bfa164eac8b2c1023b1195ad342d66842e4f891..a8acc9b998a4e4731ce211fa97a9695ef7110ee0 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6197445b839f98b90944ea771c7c0f63db31f0e455b325be3435fa2d17a89517" + "sha256": "1147de24391cc36d2688e229ffc68c4a093efa85ef0a957f89206ba6784dc2b3" }, "pipfile-spec": 6, "requires": { @@ -54,6 +54,14 @@ "index": "pypi", "version": "==2.2.2" }, + "flask-cors": { + "hashes": [ + "sha256:74efc975af1194fc7891ff5cd85b0f7478be4f7f59fe158102e91abb72bb4438", + "sha256:b60839393f3b84a0f3746f6cdca56c1ad7426aa738b70d6c61375857823181de" + ], + "index": "pypi", + "version": "==3.0.10" + }, "flask-restful": { "hashes": [ "sha256:4970c49b6488e46c520b325f54833374dc2b98e211f1b272bd4b0c516232afe2", diff --git a/api.py b/api.py index 825c204f582d2371c6ac5487f587976f411bfea7..fad511ca008c99d83db4e8094d9ca5495f33b460 100644 --- a/api.py +++ b/api.py @@ -5,9 +5,11 @@ from endpoints.clients import Client, ClientList from endpoints.receive import Receive from endpoints.send import Send from endpoints.notify import Notify +from flask_cors import CORS app = Flask(__name__) api = Api(app) +CORS(app, resources={r"*": {"origins": "http://localhost:8086"}}) api.add_resource(HelloWorld, "/") api.add_resource(ClientList, "/client") @@ -17,4 +19,4 @@ api.add_resource(Send, "/send") api.add_resource(Notify, "/notify") if __name__ == "__main__": - app.run(debug=True) + app.run(debug=True, port=8087) diff --git a/endpoints/notify.py b/endpoints/notify.py index 3e0c552569b7ec208a46763b1d5acbf8d0dc71f9..754b7f697f564bd3ece047e1431046810b362192 100644 --- a/endpoints/notify.py +++ b/endpoints/notify.py @@ -1,5 +1,6 @@ from flask_restful import Resource, request, abort from marshmallow import Schema, fields +import pika import json class NotifySchema(Schema): @@ -12,7 +13,7 @@ class Notify(Resource): schema = None def __init__(self): - self.schema = ReceiveSchema() + self.schema = NotifySchema() with open("clients.json", "r") as clients_file: self.clients = json.load(clients_file) @@ -22,9 +23,12 @@ class Notify(Resource): if errors: abort(400, message=str(errors)) - messages = [] allow = False body = args.get("body") + message = { + 'topic': 'broadcast', + 'message': body, + } client_id = args.get("client_id") notify_queue = client_id + "-notify" if client_id in self.clients: @@ -36,5 +40,5 @@ class Notify(Resource): connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) channel = connection.channel() channel.queue_declare(queue=notify_queue, durable=True) - channel.basic_publish(exchange="", routing_key=notify_queue, body=body) - deliver_connection.close() + channel.basic_publish(exchange="", routing_key=notify_queue, body=json.dumps(message)) + connection.close() diff --git a/endpoints/send.py b/endpoints/send.py index d755186e7a80e05f6c86f90d8ea9cc32f82a8351..17a958e0c15bf2998671a2ceb610767a0b90fa38 100644 --- a/endpoints/send.py +++ b/endpoints/send.py @@ -1,5 +1,6 @@ from flask_restful import Resource, request, abort from marshmallow import Schema, fields +import pika import json class SendSchema(Schema): @@ -13,7 +14,7 @@ class Send(Resource): schema = None def __init__(self): - self.schema = ReceiveSchema() + self.schema = SendSchema() with open("clients.json", "r") as clients_file: self.clients = json.load(clients_file) @@ -23,7 +24,6 @@ class Send(Resource): if errors: abort(400, message=str(errors)) - messages = [] allow = False body = args.get("body") topic = args.get("topic") @@ -37,10 +37,10 @@ class Send(Resource): if allow: connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) channel = connection.channel() - channel.queue_declare(queue=notify_queue, durable=True) + channel.queue_declare(queue=outbox_queue, durable=True) message = { 'topic': topic, 'message': body, } - channel.basic_publish(exchange="", routing_key=notify_queue, body=message) - deliver_connection.close() + channel.basic_publish(exchange="", routing_key=outbox_queue, body=json.dumps(message)) + connection.close()