diff --git a/src/eddn/Bouncer.py b/src/eddn/Bouncer.py index 4cbb6bd..730f14e 100644 --- a/src/eddn/Bouncer.py +++ b/src/eddn/Bouncer.py @@ -258,7 +258,7 @@ class EnableCors(object): name = 'enable_cors' api = 2 - def apply(self, fn: Callable, context: str): + def apply(self, fn: Callable): """ Apply CORS headers to the calling bottle app. @@ -286,7 +286,7 @@ class CustomLogging(object): name = 'custom_logging' api = 2 - def apply(self, fn: Callable, context: str): + def apply(self, fn: Callable): """ Apply custom logging to bottle request. diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index b937700..a5155d7 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -119,7 +119,7 @@ def configure() -> None: for binding in Settings.GATEWAY_SENDER_BINDINGS: sender.bind(binding) - for schema_ref, schema_file in Settings.GATEWAY_JSON_SCHEMAS.iteritems(): + for schema_ref, schema_file in Settings.GATEWAY_JSON_SCHEMAS.items(): validator.add_schema_resource(schema_ref, resource_string('eddn.Gateway', schema_file)) @@ -137,7 +137,7 @@ def push_message(parsed_message: Dict, topic: str) -> None: # announcers with schema as topic compressed_msg = zlib.compress(string_message) - send_message = f"{str(topic)!r} |-| {compressed_msg!r}" + send_message = f"{str(topic)!r} |-| {compressed_msg!r}".encode('utf8') sender.send(send_message) stats_collector.tally("outbound") @@ -345,34 +345,26 @@ class MalformedUploadError(Exception): pass -class EnableCors(object): - """Handle enabling CORS headers in all responses.""" - - name = 'enable_cors' - api = 2 - - @staticmethod - def apply(self, fn: Callable, context: str): - """ - Apply CORS headers to the calling bottle app. - - :param fn: - :param context: - :return: - """ - def _enable_cors(*args, **kwargs): - # set CORS headers - response.headers['Access-Control-Allow-Origin'] = '*' - response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' - response.headers['Access-Control-Allow-Headers'] = \ - 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' - - if request.method != 'OPTIONS': - # actual request; reply with the actual response - return fn(*args, **kwargs) - - return _enable_cors +def apply_cors() -> None: + """ + Apply CORS headers to the calling bottle app. + :param fn: + :param context: + :return: + """ + response.set_header( + 'Access-Control-Allow-Origin', + '*' + ) + response.set_header( + 'Access-Control-Allow-Methods', + 'GET, POST, PUT, OPTIONS' + ) + response.set_header( + 'Access-Control-Allow-Headers', + 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' + ) def main() -> None: """Handle setting up and running the bottle app.""" @@ -383,7 +375,7 @@ def main() -> None: load_config(cl_args) configure() - app.install(EnableCors()) + app.add_hook('after_request', apply_cors) app.run( host=Settings.GATEWAY_HTTP_BIND_ADDRESS, port=Settings.GATEWAY_HTTP_PORT, diff --git a/src/eddn/Monitor.py b/src/eddn/Monitor.py index dce2afb..3e2b36f 100644 --- a/src/eddn/Monitor.py +++ b/src/eddn/Monitor.py @@ -285,7 +285,7 @@ class EnableCors(object): api = 2 @staticmethod - def apply(self, fn: Callable, context: str): + def apply(self, fn: Callable): """ Apply a CORS handler. diff --git a/src/eddn/Relay.py b/src/eddn/Relay.py index 4c84d0e..af3280f 100644 --- a/src/eddn/Relay.py +++ b/src/eddn/Relay.py @@ -118,9 +118,9 @@ class Relay(Thread): # Filters on topics or not... if Settings.RELAY_RECEIVE_ONLY_GATEWAY_EXTRA_JSON is True: - for schema_ref, schema_file in Settings.GATEWAY_JSON_SCHEMAS.iteritems(): + for schema_ref, schema_file in Settings.GATEWAY_JSON_SCHEMAS.items(): receiver.setsockopt(zmq.SUBSCRIBE, schema_ref) - for schema_ref, schema_file in Settings.RELAY_EXTRA_JSON_SCHEMAS.iteritems(): + for schema_ref, schema_file in Settings.RELAY_EXTRA_JSON_SCHEMAS.items(): receiver.setsockopt(zmq.SUBSCRIBE, schema_ref) else: receiver.setsockopt(zmq.SUBSCRIBE, '') @@ -195,7 +195,7 @@ class EnableCors(object): api = 2 @staticmethod - def apply(self, fn: Callable, context: str): + def apply(self, fn: Callable): """ Apply a CORS handler. diff --git a/src/eddn/conf/Settings.py b/src/eddn/conf/Settings.py index d61cd31..13012d5 100644 --- a/src/eddn/conf/Settings.py +++ b/src/eddn/conf/Settings.py @@ -140,7 +140,7 @@ class _Settings(object): def load_from(self, file_name: str) -> None: f = open(file_name, 'r') conf = simplejson.load(f) - for key, value in conf.iteritems(): + for key, value in conf.items(): if key in dir(self): self.__setattr__(key, value)