From aa9737498aecbc7cd392d1e35e7062e405c2c7e8 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 23 Feb 2020 18:32:37 +0100 Subject: [PATCH] add controller options to allow multiple routes per controller --- owrx/controllers/__init__.py | 9 ++++++--- owrx/controllers/api.py | 2 +- owrx/controllers/assets.py | 2 +- owrx/controllers/metrics.py | 2 +- owrx/controllers/status.py | 6 ++---- owrx/controllers/template.py | 6 +++--- owrx/controllers/websocket.py | 2 +- owrx/http.py | 20 +++++++++----------- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/owrx/controllers/__init__.py b/owrx/controllers/__init__.py index 04f7954..ff687ed 100644 --- a/owrx/controllers/__init__.py +++ b/owrx/controllers/__init__.py @@ -3,9 +3,10 @@ from datetime import datetime class Controller(ABC): - def __init__(self, handler, request): + def __init__(self, handler, request, options): self.handler = handler self.request = request + self.options = options def send_response(self, content, code=200, content_type="text/html", last_modified: datetime = None, max_age=None): self.handler.send_response(code) @@ -20,6 +21,8 @@ class Controller(ABC): content = content.encode() self.handler.wfile.write(content) - @abstractmethod def handle_request(self): - pass + action = "indexAction" + if "action" in self.options: + action = self.options["action"] + getattr(self, action)() diff --git a/owrx/controllers/api.py b/owrx/controllers/api.py index 122363a..4e7a966 100644 --- a/owrx/controllers/api.py +++ b/owrx/controllers/api.py @@ -4,6 +4,6 @@ import json class ApiController(Controller): - def handle_request(self): + def indexAction(self): data = json.dumps(FeatureDetector().feature_report()) self.send_response(data, content_type="application/json") diff --git a/owrx/controllers/assets.py b/owrx/controllers/assets.py index a3b093d..4c55e17 100644 --- a/owrx/controllers/assets.py +++ b/owrx/controllers/assets.py @@ -35,7 +35,7 @@ class AssetsController(Controller): except FileNotFoundError: self.send_response("file not found", code=404) - def handle_request(self): + def indexAction(self): filename = self.request.matches.group(1) self.serve_file(filename) diff --git a/owrx/controllers/metrics.py b/owrx/controllers/metrics.py index 33f05b1..e817e9b 100644 --- a/owrx/controllers/metrics.py +++ b/owrx/controllers/metrics.py @@ -4,6 +4,6 @@ import json class MetricsController(Controller): - def handle_request(self): + def indexAction(self): data = json.dumps(Metrics.getSharedInstance().getMetrics()) self.send_response(data, content_type="application/json") diff --git a/owrx/controllers/status.py b/owrx/controllers/status.py index 3b7f9ac..bb513ee 100644 --- a/owrx/controllers/status.py +++ b/owrx/controllers/status.py @@ -8,7 +8,7 @@ import json class StatusController(Controller): - def handle_request(self): + def indexAction(self): pm = PropertyManager.getSharedInstance() # TODO keys that have been left out since they are no longer simple strings: sdr_hw, bands, antenna vars = { @@ -25,8 +25,6 @@ class StatusController(Controller): } self.send_response("\n".join(["{key}={value}".format(key=key, value=value) for key, value in vars.items()])) - -class StatusJsonController(Controller): def getProfileStats(self, profile): return { "name": profile["name"], @@ -43,7 +41,7 @@ class StatusJsonController(Controller): } return stats - def handle_request(self): + def jsonAction(self): pm = PropertyManager.getSharedInstance() gps = pm["receiver_gps"] diff --git a/owrx/controllers/template.py b/owrx/controllers/template.py index cbcf116..3938719 100644 --- a/owrx/controllers/template.py +++ b/owrx/controllers/template.py @@ -25,16 +25,16 @@ class WebpageController(TemplateController, metaclass=ABCMeta): class IndexController(WebpageController): - def handle_request(self): + def indexAction(self): self.serve_template("index.html", **self.template_variables()) class MapController(WebpageController): - def handle_request(self): + def indexAction(self): # TODO check if we have a google maps api key first? self.serve_template("map.html", **self.template_variables()) class FeatureController(WebpageController): - def handle_request(self): + def indexAction(self): self.serve_template("features.html", **self.template_variables()) diff --git a/owrx/controllers/websocket.py b/owrx/controllers/websocket.py index 2886b54..f242f2c 100644 --- a/owrx/controllers/websocket.py +++ b/owrx/controllers/websocket.py @@ -4,7 +4,7 @@ from owrx.connection import WebSocketMessageHandler class WebSocketController(Controller): - def handle_request(self): + def indexAction(self): conn = WebSocketConnection(self.handler, WebSocketMessageHandler()) # enter read loop conn.handle() diff --git a/owrx/http.py b/owrx/http.py index d06baca..60e4e8d 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -1,7 +1,4 @@ -from owrx.controllers.status import ( - StatusJsonController, - StatusController -) +from owrx.controllers.status import StatusController from owrx.controllers.template import ( IndexController, MapController, @@ -48,8 +45,9 @@ class Request(object): class Route(ABC): - def __init__(self, controller): + def __init__(self, controller, controllerOptions = None): self.controller = controller + self.controllerOptions = controllerOptions if controllerOptions is not None else {} @abstractmethod def matches(self, request): @@ -57,18 +55,18 @@ class Route(ABC): class StaticRoute(Route): - def __init__(self, route, controller): + def __init__(self, route, controller, controllerOptions = None): self.route = route - super().__init__(controller) + super().__init__(controller, controllerOptions) def matches(self, request): return request.path == self.route class RegexRoute(Route): - def __init__(self, regex, controller): + def __init__(self, regex, controller, controllerOptions = None): self.regex = re.compile(regex) - super().__init__(controller) + super().__init__(controller, controllerOptions) def matches(self, request): matches = self.regex.match(request.path) @@ -82,7 +80,7 @@ class Router(object): self.routes = [ StaticRoute("/", IndexController), StaticRoute("/status", StatusController), - StaticRoute("/status.json", StatusJsonController), + StaticRoute("/status.json", StatusController, {"action": "jsonAction"}), RegexRoute("/static/(.+)", OwrxAssetsController), RegexRoute("/aprs-symbols/(.+)", AprsSymbolsController), StaticRoute("/ws/", WebSocketController), @@ -106,6 +104,6 @@ class Router(object): route = self.find_route(request) if route is not None: controller = route.controller - controller(handler, request).handle_request() + controller(handler, request, route.controllerOptions).handle_request() else: handler.send_error(404, "Not Found", "The page you requested could not be found.")