From d23acd0d6d03d822e4505c64f71587f55e8dc2b6 Mon Sep 17 00:00:00 2001 From: mar77i Date: Tue, 11 Jul 2023 19:38:53 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 +++ hub/__init__.py | 0 hub/app.py | 16 +++++++++++++ hub/cryptresource.py | 46 ++++++++++++++++++++++++++++++++++++ hubapps/first/index.html.j2 | 14 +++++++++++ hubapps/first/index.js | 13 ++++++++++ hubapps/first/master.html.j2 | 14 +++++++++++ hubapps/first/master.js | 15 ++++++++++++ run_server.py | 6 +++++ 9 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 hub/__init__.py create mode 100644 hub/app.py create mode 100644 hub/cryptresource.py create mode 100644 hubapps/first/index.html.j2 create mode 100644 hubapps/first/index.js create mode 100644 hubapps/first/master.html.j2 create mode 100644 hubapps/first/master.js create mode 100755 run_server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07da734 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +__pycache__/ +venv/ diff --git a/hub/__init__.py b/hub/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hub/app.py b/hub/app.py new file mode 100644 index 0000000..24212d0 --- /dev/null +++ b/hub/app.py @@ -0,0 +1,16 @@ +from argparse import ArgumentParser + +from falcon.asgi import App +from falcon.constants import MEDIA_HTML + +from .cryptresource import CryptResource + + +def get_app(): + app = App(media_type=MEDIA_HTML) + ap = ArgumentParser() + ap.add_argument("--secret") + args = ap.parse_args() + cr = CryptResource(args.secret) + cr.register_on(app) + return app diff --git a/hub/cryptresource.py b/hub/cryptresource.py new file mode 100644 index 0000000..ce2dc5c --- /dev/null +++ b/hub/cryptresource.py @@ -0,0 +1,46 @@ +from base64 import urlsafe_b64encode +from hashlib import sha3_512 +from pathlib import Path +from secrets import token_urlsafe + +from falcon.constants import MEDIA_JS +from jinja2 import Environment, FileSystemLoader + + +class CryptResource: + @staticmethod + def scramble(secret, value): + h = sha3_512() + h.update(f"{secret}{value}".encode()) + return urlsafe_b64encode(h.digest()).rstrip(b'=').decode('ascii') + + def __init__(self, secret=None): + self.current_hubapp = "first" + self.secret = secret or token_urlsafe(64) + self.master_uri = f"/{self.scramble(self.secret, 'index.html')}" + self.master_js_uri = f"/{self.scramble(self.secret, 'master.js')}" + hubapp_dir = Path(__file__).parents[1] / "hubapps" / self.current_hubapp + self.env = Environment(loader=FileSystemLoader(hubapp_dir)) + with open(hubapp_dir / "index.js", "rb") as fh: + self._index_js = fh.read() + with open(hubapp_dir / "master.js", "rb") as fh: + self._master_js = fh.read() + + async def on_get(self, req, resp): + resp.data = self.env.get_template("index.html.j2").render({}).encode() + + async def on_get_index_js(self, req, resp): + resp.data = self.env.get_template("index.js").render({}).encode() + + async def on_get_master(self, req, resp): + resp.data = self.env.get_template("master.html.j2").render({"master_js": self.master_js_uri}).encode() + + async def on_get_master_js(self, req, resp): + resp.data = self._master_js + resp.content_type = MEDIA_JS + + def register_on(self, app): + app.add_route("/", self) + app.add_route("/index.js", self, suffix="index_js") + app.add_route(self.master_uri, self, suffix="master") + app.add_route(self.master_js_uri, self, suffix="master_js") diff --git a/hubapps/first/index.html.j2 b/hubapps/first/index.html.j2 new file mode 100644 index 0000000..dfc36eb --- /dev/null +++ b/hubapps/first/index.html.j2 @@ -0,0 +1,14 @@ + + + + + + + Index + + + + + hello, this is index.html, the "controller" + + diff --git a/hubapps/first/index.js b/hubapps/first/index.js new file mode 100644 index 0000000..20d7101 --- /dev/null +++ b/hubapps/first/index.js @@ -0,0 +1,13 @@ +"use strict"; + +(function () { + document.addEventListener("readystatechange", function (event) { + if (!event) { + event = window.event; + } + if (event.target.readyState !== "complete") { + return; + } + document.body.append(document.createTextNode("(this is index.js) ")); + }); +})(); diff --git a/hubapps/first/master.html.j2 b/hubapps/first/master.html.j2 new file mode 100644 index 0000000..12dfc85 --- /dev/null +++ b/hubapps/first/master.html.j2 @@ -0,0 +1,14 @@ + + + + + + + Master + + + + + hello, this is master.html, the "playing field" + + diff --git a/hubapps/first/master.js b/hubapps/first/master.js new file mode 100644 index 0000000..e33e9fe --- /dev/null +++ b/hubapps/first/master.js @@ -0,0 +1,15 @@ +"use strict"; + +(function () { + document.addEventListener("readystatechange", function (event) { + if (!event) { + event = window.event; + } + if (event.target.readyState !== "complete") { + return; + } + document.body.append(document.createTextNode("(this is master.js) ")); + // hide real url + history.pushState({}, "", new URL(window.location.origin + "/")); + }); +})(); diff --git a/run_server.py b/run_server.py new file mode 100755 index 0000000..818817d --- /dev/null +++ b/run_server.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +import uvicorn + +if __name__ == "__main__": + uvicorn.run("hub.app:get_app", factory=True, port=5000, log_level="info") -- 2.45.2