72 lines
No EOL
3 KiB
Python
72 lines
No EOL
3 KiB
Python
# main.py - FastAPI + JinjaX
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
import jinjax
|
|
|
|
from routes import upload, viewer, admin, crop
|
|
|
|
# -- Middleware ------------------------------------------------
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.requests import Request
|
|
from starlette.responses import Response
|
|
|
|
|
|
app = FastAPI(title="PointCloud Frontend")
|
|
|
|
# -- Middleware ------------------------------------------------
|
|
MAX_UPLOAD_BYTES = 10 * 1024 * 1024 * 1024 # 10 GB à ajuster
|
|
|
|
class LimitUploadSize(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next):
|
|
if request.method == "POST":
|
|
content_length = request.headers.get("content-length")
|
|
if content_length and int(content_length) > MAX_UPLOAD_BYTES:
|
|
return Response(
|
|
content=f"Fichier trop volumineux. Maximum : {MAX_UPLOAD_BYTES // (1024**3)} GB",
|
|
status_code=413,
|
|
)
|
|
return await call_next(request)
|
|
|
|
app.add_middleware(LimitUploadSize)
|
|
|
|
# ── Fichiers statiques ────────────────────────────────────────────────────────
|
|
app.mount(
|
|
"/static",
|
|
StaticFiles(directory=Path(__file__).parent / "static"),
|
|
name="static",
|
|
)
|
|
|
|
# ── JinjaX catalog pour les composants ───────────────────────────────────────
|
|
catalog = jinjax.Catalog()
|
|
catalog.add_folder(Path(__file__).parent / "components")
|
|
catalog.add_folder(Path(__file__).parent / "templates")
|
|
catalog.add_folder(Path(__file__).parent / "templates" / "partials")
|
|
|
|
# ── Jinja2 pour les pages et partials ────────────────────────────────────────
|
|
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
|
|
templates.env.filters["datetimeformat"] = lambda ts: (
|
|
datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M") if ts else "—"
|
|
)
|
|
|
|
# Connecte JinjaX au même environnement Jinja2 pour que les composants
|
|
# soient utilisables dans les templates HTML via {{ catalog.render(...) }}
|
|
catalog.jinja_env = templates.env
|
|
|
|
# Filtre personnalisé : timestamp unix → date lisible
|
|
catalog.jinja_env.filters["datetimeformat"] = lambda ts: (
|
|
datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M") if ts else "—"
|
|
)
|
|
|
|
# Rend le catalog accessible aux routes via app.state
|
|
app.state.catalog = catalog
|
|
app.state.templates = templates
|
|
|
|
# ── Routes ────────────────────────────────────────────────────────────────────
|
|
app.include_router(upload.router)
|
|
app.include_router(viewer.router)
|
|
app.include_router(admin.router)
|
|
app.include_router(crop.router) |