From bd8e66519886e39bfa942841e4ab9838a1b01316 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Fri, 3 May 2019 22:59:24 +0200 Subject: [PATCH] add new webserver infrastructure --- htdocs/index.wrx | 54 ++++++++++++++++++++++----------------------- owrx/controllers.py | 43 ++++++++++++++++++++++++++++++++++++ owrx/http.py | 35 +++++++++++++++++++++++++++++ server.py | 6 +++++ 4 files changed, 111 insertions(+), 27 deletions(-) create mode 100644 owrx/controllers.py create mode 100644 owrx/http.py create mode 100644 server.py diff --git a/htdocs/index.wrx b/htdocs/index.wrx index 5e00506..2cd64da 100644 --- a/htdocs/index.wrx +++ b/htdocs/index.wrx @@ -39,40 +39,40 @@ var mathbox_waterfall_history_length=%[MATHBOX_WATERFALL_THIST]; var mathbox_waterfall_colors=%[MATHBOX_WATERFALL_COLORS]; - - - - - - - + + + + + + +
- +
%[RX_PHOTO_TITLE]
%[RX_PHOTO_DESC]
- - - - + + + +
%[RX_TITLE]
%[RX_LOC] | Loc: %[RX_QRA], ASL: %[RX_ASL] m, [maps]
- - + +
    -

  • Status
  • -

  • Log
  • -

  • Receiver
  • +

  • Status
  • +

  • Log
  • +

  • Receiver
@@ -110,23 +110,23 @@
-
+
-
+
-
+
-
+
-
-
-
-
-
+
+
+
+
+
0 dB
@@ -172,7 +172,7 @@
- +

Start OpenWebRX
diff --git a/owrx/controllers.py b/owrx/controllers.py new file mode 100644 index 0000000..c7989c1 --- /dev/null +++ b/owrx/controllers.py @@ -0,0 +1,43 @@ +import mimetypes + +class Controller(object): + def __init__(self, handler, matches): + self.handler = handler + self.matches = matches + def send_response(self, content, code = 200, content_type = "text/html"): + self.handler.send_response(code) + if content_type is not None: + self.handler.send_header("Content-Type", content_type) + self.handler.end_headers() + if (type(content) == str): + content = content.encode() + self.handler.wfile.write(content) + def serve_file(self, file): + try: + f = open('htdocs/' + file, 'rb') + data = f.read() + f.close() + + (content_type, encoding) = mimetypes.MimeTypes().guess_type(file) + self.send_response(data, content_type = content_type) + except FileNotFoundError: + self.send_response("file not found", code = 404) + def render_template(self, template, **variables): + f = open('htdocs/' + template) + data = f.read() + f.close() + + self.send_response(data) + +class StatusController(Controller): + def handle_request(self): + self.send_response("you have reached the status page!") + +class IndexController(Controller): + def handle_request(self): + self.render_template("index.wrx") + +class AssetsController(Controller): + def handle_request(self): + filename = self.matches.group(1) + self.serve_file(filename) \ No newline at end of file diff --git a/owrx/http.py b/owrx/http.py new file mode 100644 index 0000000..b5ac0ae --- /dev/null +++ b/owrx/http.py @@ -0,0 +1,35 @@ +from owrx.controllers import StatusController, IndexController, AssetsController +from http.server import BaseHTTPRequestHandler +import re + +class RequestHandler(BaseHTTPRequestHandler): + def __init__(self, request, client_address, server): + self.router = Router() + super().__init__(request, client_address, server) + def do_GET(self): + self.router.route(self) + +class Router(object): + mappings = [ + {"route": "/", "controller": IndexController}, + {"route": "/status", "controller": StatusController}, + {"regex": "/static/(.+)", "controller": AssetsController} + ] + def find_controller(self, path): + for m in Router.mappings: + if "route" in m: + if m["route"] == path: + return (m["controller"], None) + if "regex" in m: + regex = re.compile(m["regex"]) + matches = regex.match(path) + if matches: + return (m["controller"], matches) + def route(self, handler): + res = self.find_controller(handler.path) + #print("path: {0}, controller: {1}, matches: {2}".format(handler.path, controller, matches)) + if res is not None: + (controller, matches) = res + controller(handler, matches).handle_request() + else: + handler.send_error(404, "Not Found", "The page you requested could not be found.") diff --git a/server.py b/server.py new file mode 100644 index 0000000..2b14a52 --- /dev/null +++ b/server.py @@ -0,0 +1,6 @@ +from http.server import HTTPServer +from owrx.http import RequestHandler + +server = HTTPServer(('0.0.0.0', 3000), RequestHandler) +server.serve_forever() +