Merged something

This commit is contained in:
Leo Vasanko 2023-11-01 14:03:17 +00:00
parent acdd776b92
commit 9002afbc7e

View File

@ -2,6 +2,7 @@ import mimetypes
from importlib.resources import files from importlib.resources import files
from urllib.parse import unquote from urllib.parse import unquote
import brotli
from sanic import Blueprint, Sanic, raw from sanic import Blueprint, Sanic, raw
from sanic.exceptions import Forbidden, NotFound from sanic.exceptions import Forbidden, NotFound
@ -58,6 +59,30 @@ def http_fileserver(app, _):
app.blueprint(bp) 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("/<path:path>", static=True) @app.get("/<path:path>", static=True)
async def wwwroot(req, path=""): async def wwwroot(req, path=""):
"""Frontend files only""" """Frontend files only"""