diff --git a/cista/app.py b/cista/app.py index 6bc09e6..0dbe4d8 100755 --- a/cista/app.py +++ b/cista/app.py @@ -2,6 +2,7 @@ import mimetypes from importlib.resources import files from urllib.parse import unquote +import brotli from sanic import Blueprint, Sanic, raw from sanic.exceptions import Forbidden, NotFound @@ -58,6 +59,30 @@ def http_fileserver(app, _): app.blueprint(bp) +www = {} + + +@app.before_server_start +def load_wwwroot(app, _): + www.clear() + base = files("cista") + paths = ["wwwroot"] + while path := paths.pop(0): + current = files("cista") / path + for p in current.iterdir(): + if p.is_dir(): + print(p) + paths.append(current / p.parts[-1]) + continue + name = p.relative_to(base).as_posix() + mime = mimetypes.guess_type(name)[0] or "application/octet-stream" + data = p.read_bytes() + br = brotli.compress(data) + if len(br) >= len(data): + br = False + www[name] = data, br, mime + + @app.get("/", static=True) async def wwwroot(req, path=""): """Frontend files only"""