diff --git a/src/main.py b/src/main.py index 31e14aa..5c0cb61 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import hashlib import logging import logging.handlers import os @@ -56,6 +57,15 @@ from routers.admins import router as admins_router from routers.stats import router as stats_router from routers.telegram import router as telegram_router +# ── Cache-bust hash (SHA-1 of all static file contents) ────────────────────── +def _static_hash(static_dir: str) -> str: + h = hashlib.sha1() + for root, _, files in sorted(os.walk(static_dir)): + for name in sorted(files): + with open(os.path.join(root, name), "rb") as f: + h.update(f.read()) + return h.hexdigest()[:8] + # ── App ─────────────────────────────────────────────────────────────────────── @asynccontextmanager async def lifespan(app: FastAPI): @@ -132,10 +142,10 @@ async def _serve_admin() -> FileResponse: @app.get("/sw.js", include_in_schema=False) -async def _serve_sw() -> FileResponse: - return FileResponse( - os.path.join(_STATIC_DIR, "sw.js"), media_type="application/javascript" - ) +async def _serve_sw() -> Response: + sw_path = os.path.join(_STATIC_DIR, "sw.js") + content = open(sw_path, encoding="utf-8").read().replace("__STATIC_HASH__", _static_hash(os.path.join(os.path.dirname(os.path.abspath(__file__)), "static"))) + return Response(content=content, media_type="application/javascript") @app.get("/manifest.json", include_in_schema=False) diff --git a/src/routers/__init__.py b/src/routers/__init__.py index 9c8ddfa..e69de29 100644 --- a/src/routers/__init__.py +++ b/src/routers/__init__.py @@ -1 +0,0 @@ -# routers package diff --git a/src/services/__init__.py b/src/services/__init__.py index 9924c7a..e69de29 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -1,7 +0,0 @@ -from .avconnect import AVConnectAPI -from .gates import call_open_gate - -__all__ = [ - "AVConnectAPI", - "call_open_gate", -] \ No newline at end of file diff --git a/src/static/admin.html b/src/static/admin.html index b657c30..08e5f5f 100644 --- a/src/static/admin.html +++ b/src/static/admin.html @@ -136,9 +136,9 @@ diff --git a/src/static/sw.js b/src/static/sw.js index 12ffc8b..43b85f5 100644 --- a/src/static/sw.js +++ b/src/static/sw.js @@ -1,5 +1,5 @@ /* Service worker - Lagomare Gates */ -const CACHE = "lagomare-gates-v1"; +const CACHE = "lagomare-gates-__STATIC_HASH__"; const PRECACHE = ["/static/style.css", "/static/app.js", "/static/images/logo.svg", "/static/images/mobile_icon.png", "/manifest.json"]; self.addEventListener("install", event => {