Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python3
from abc import ABC
from typing import Any

import tornado.ioloop
import tornado.web
Expand All @@ -21,6 +22,21 @@

startup_timestamp = datetime.now()

class BaseCORSHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with, content-type")
self.set_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")

def options(self, *args, **kwargs):
self.set_default_headers()
self.set_status(204)
self.finish()

def write_error(self, status_code: int, **kwargs: Any) -> None:
self.set_default_headers()
super().write_error(status_code, **kwargs)


class HealthHandler(tornado.web.RequestHandler, ABC):
# noinspection PyAttributeOutsideInit
Expand Down Expand Up @@ -74,17 +90,17 @@ def get(self):
self.finish()


class SpaceAPIHandler(tornado.web.RequestHandler, ABC):
class SpaceAPIHandler(BaseCORSHandler, ABC):
# noinspection PyAttributeOutsideInit
def initialize(self, observer):
self.observer = observer

def get(self):
self.set_header("Content-Type", "application/json")
self.set_header("Content-Type", "application/json; charset=utf-8")
self.write(json.dumps(self.observer.get_space_api_entry(), indent=4))
self.finish()

class SpaceStateTextHandler(tornado.web.RequestHandler, ABC):
class SpaceStateTextHandler(BaseCORSHandler, ABC):
# noinspection PyAttributeOutsideInit
def initialize(self, observer):
self.observer = observer
Expand All @@ -95,7 +111,7 @@ def get(self):
self.finish()


class PictureHandler(tornado.web.RequestHandler, ABC):
class PictureHandler(BaseCORSHandler, ABC):
# noinspection PyAttributeOutsideInit
def initialize(self, picture_manager):
self.picture_manager = picture_manager
Expand Down
Loading