]> git.mar77i.info Git - hublib/commitdiff
initial commit
authormar77i <mar77i@protonmail.ch>
Tue, 11 Jul 2023 17:38:53 +0000 (19:38 +0200)
committermar77i <mar77i@protonmail.ch>
Tue, 11 Jul 2023 17:57:18 +0000 (19:57 +0200)
.gitignore [new file with mode: 0644]
hub/__init__.py [new file with mode: 0644]
hub/app.py [new file with mode: 0644]
hub/cryptresource.py [new file with mode: 0644]
hubapps/first/index.html.j2 [new file with mode: 0644]
hubapps/first/index.js [new file with mode: 0644]
hubapps/first/master.html.j2 [new file with mode: 0644]
hubapps/first/master.js [new file with mode: 0644]
run_server.py [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..07da734
--- /dev/null
@@ -0,0 +1,3 @@
+.idea/
+__pycache__/
+venv/
diff --git a/hub/__init__.py b/hub/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/hub/app.py b/hub/app.py
new file mode 100644 (file)
index 0000000..24212d0
--- /dev/null
@@ -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 (file)
index 0000000..ce2dc5c
--- /dev/null
@@ -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 (file)
index 0000000..dfc36eb
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Index</title>
+    <!-- <link rel="stylesheet" href="style.css"> -->
+  </head>
+  <body>
+       <script src="index.js"></script>
+    hello, this is index.html, the "controller"
+  </body>
+</html>
diff --git a/hubapps/first/index.js b/hubapps/first/index.js
new file mode 100644 (file)
index 0000000..20d7101
--- /dev/null
@@ -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 (file)
index 0000000..12dfc85
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Master</title>
+    <!-- <link rel="stylesheet" href="style.css"> -->
+  </head>
+  <body>
+       <script src="{{ master_js }}"></script>
+    hello, this is master.html, the "playing field"
+  </body>
+</html>
diff --git a/hubapps/first/master.js b/hubapps/first/master.js
new file mode 100644 (file)
index 0000000..e33e9fe
--- /dev/null
@@ -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 (executable)
index 0000000..818817d
--- /dev/null
@@ -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")