-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (44 loc) · 1.67 KB
/
Copy pathapp.py
File metadata and controls
58 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from core.storage.database import (
init_db,
)
from core.routers import ROUTERS
BASE_DIR = Path(__file__).resolve().parent
STATIC_DIR = BASE_DIR / "static"
PAGE_FILES = {
"/scan": STATIC_DIR / "scan.html",
"/duplicates": STATIC_DIR / "duplicates.html",
"/registry": STATIC_DIR / "registry.html",
"/cleanup": STATIC_DIR / "cleanup.html",
"/recycle": STATIC_DIR / "recycle.html",
"/logs": STATIC_DIR / "logs.html",
"/tools": STATIC_DIR / "tools.html",
"/user-directory": STATIC_DIR / "user-directory.html",
"/app-scan": STATIC_DIR / "app-scan.html",
}
NO_CACHE_PATHS = {"/", "/index.html", *PAGE_FILES.keys()}
NO_CACHE_SUFFIXES = (".html", ".js", ".css")
@asynccontextmanager
async def lifespan(_: FastAPI):
init_db()
yield
app = FastAPI(lifespan=lifespan)
class StaticNoCacheMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: Any) -> Response:
response = await call_next(request)
path = request.url.path
if path in NO_CACHE_PATHS or path.endswith(NO_CACHE_SUFFIXES):
response.headers["Cache-Control"] = "no-store, max-age=0"
response.headers["Pragma"] = "no-cache"
return response
for router in ROUTERS:
app.include_router(router)
app.add_middleware(StaticNoCacheMiddleware)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")