Frontend created and rewritten a few times, with some backend fixes #1
20
cista/app.py
20
cista/app.py
|
@ -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 asyncio
|
||||||
import brotli
|
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
|
||||||
|
@ -64,27 +65,36 @@ www = {}
|
||||||
|
|
||||||
@app.before_server_start
|
@app.before_server_start
|
||||||
def load_wwwroot(app):
|
def load_wwwroot(app):
|
||||||
www.clear()
|
global www
|
||||||
|
wwwnew = {}
|
||||||
base = files("cista") / "wwwroot"
|
base = files("cista") / "wwwroot"
|
||||||
paths = ["."]
|
paths = ["."]
|
||||||
while paths:
|
while paths:
|
||||||
path = paths.pop(0)
|
path = paths.pop(0)
|
||||||
current = base / path
|
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)
|
|
||||||
paths.append(current / p.parts[-1])
|
paths.append(current / p.parts[-1])
|
||||||
continue
|
continue
|
||||||
name = p.relative_to(base).as_posix()
|
name = p.relative_to(base).as_posix()
|
||||||
mime = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
mime = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
||||||
data = p.read_bytes()
|
data = p.read_bytes()
|
||||||
|
# Use old data if not changed
|
||||||
|
if name in www and www[name][0] == data:
|
||||||
|
wwwnew[name] = www[name]
|
||||||
|
continue
|
||||||
|
# Precompress with Brotli
|
||||||
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)
|
wwwnew[name] = data, br, mime
|
||||||
www[name] = data, br, mime
|
www = wwwnew
|
||||||
|
|
||||||
|
@app.add_task
|
||||||
|
async def refresh_wwwroot():
|
||||||
|
while app.debug:
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
load_wwwroot(app)
|
||||||
|
|
||||||
@app.get("/<path:path>", static=True)
|
@app.get("/<path:path>", static=True)
|
||||||
async def wwwroot(req, path=""):
|
async def wwwroot(req, path=""):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user