Faster wwwroot serving, uses RAM cache of brotli compressed data for all assets.
This commit is contained in:
parent
a26dc42d88
commit
742b05ed66
32
cista/app.py
32
cista/app.py
|
@ -63,12 +63,14 @@ www = {}
|
||||||
|
|
||||||
|
|
||||||
@app.before_server_start
|
@app.before_server_start
|
||||||
def load_wwwroot(app, _):
|
def load_wwwroot(app):
|
||||||
www.clear()
|
www.clear()
|
||||||
base = files("cista")
|
base = files("cista") / "wwwroot"
|
||||||
paths = ["wwwroot"]
|
paths = ["."]
|
||||||
while path := paths.pop(0):
|
while paths:
|
||||||
current = files("cista") / path
|
path = paths.pop(0)
|
||||||
|
current = base / path
|
||||||
|
print(">>>", current)
|
||||||
for p in current.iterdir():
|
for p in current.iterdir():
|
||||||
if p.is_dir():
|
if p.is_dir():
|
||||||
print(p)
|
print(p)
|
||||||
|
@ -80,18 +82,20 @@ def load_wwwroot(app, _):
|
||||||
br = brotli.compress(data)
|
br = brotli.compress(data)
|
||||||
if len(br) >= len(data):
|
if len(br) >= len(data):
|
||||||
br = False
|
br = False
|
||||||
|
print(name, len(data), len(br) if br else br, mime)
|
||||||
www[name] = data, br, mime
|
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"""
|
||||||
name = filename.sanitize(unquote(path)) if path else "index.html"
|
name = unquote(path) or "index.html"
|
||||||
try:
|
if name not in www:
|
||||||
index = files("cista").joinpath("wwwroot", name).read_bytes()
|
raise NotFound(f"File not found: /{path}", extra={"name": name})
|
||||||
except OSError as e:
|
data, br, mime = www[name]
|
||||||
raise NotFound(
|
headers = {}
|
||||||
f"File not found: /{path}", extra={"name": name, "exception": repr(e)}
|
# Brotli compressed?
|
||||||
)
|
if br and "br" in req.headers.accept_encoding.split(", "):
|
||||||
mime = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
headers["content-encoding"] = "br"
|
||||||
return raw(index, content_type=mime)
|
data = br
|
||||||
|
return raw(data, content_type=mime, headers=headers)
|
||||||
|
|
|
@ -15,6 +15,7 @@ classifiers = [
|
||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2-cffi",
|
"argon2-cffi",
|
||||||
|
"brotli",
|
||||||
"docopt",
|
"docopt",
|
||||||
"inotify",
|
"inotify",
|
||||||
"msgspec",
|
"msgspec",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user