classifier/backend/main.py

82 lines
No EOL
2.5 KiB
Python

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, RedirectResponse
from config import EPT_DIR
from routes import upload, viewer, admin
from utils.disk import get_disk_usage, get_entwine_path
import subprocess
ENTWINE_PATH = get_entwine_path()
app = FastAPI(title="PointCloud Backend")
app.mount("/ept_data", StaticFiles(directory=str(EPT_DIR)), name="ept_data")
app.mount("/static", StaticFiles(directory="static"), name="static")
app.include_router(upload.router)
app.include_router(viewer.router)
app.include_router(admin.router)
@app.get("/api")
def home():
return RedirectResponse(url="/docs")
@app.get("/health")
def health():
return {
"ok": True,
"entwine_available": ENTWINE_PATH is not None,
"entwine_path": ENTWINE_PATH,
"disk_free_gb": get_disk_usage(),
}
def get_pdal_info() -> dict:
"""Retourne version et path de pdal via subprocess."""
info = {"path": None, "version": None}
try:
result = subprocess.run(
["which", "pdal"],
capture_output=True, text=True, check=False
)
if result.returncode == 0:
info["path"] = result.stdout.strip()
except Exception:
pass
try:
result = subprocess.run(
["pdal", "--version"],
capture_output=True, text=True, check=False
)
if result.returncode == 0:
# Sortie : "---\npdal 2.10.0 (git-version: 22e6b2)\n---"
lines = [l.strip() for l in result.stdout.strip().splitlines()
if l.strip() and not l.startswith("-")]
if lines:
info["version"] = lines[0]
except Exception:
pass
return info
@app.get("/", response_class=HTMLResponse)
def home():
entwine_path = get_entwine_path()
pdal = get_pdal_info()
return f"""
<h2>Backend PointCloud OK ✅</h2>
<ul>
<li><a href="/docs">/docs</a> — Documentation API</li>
<li><a href="/health">/health</a> — État du service</li>
<li><a href="/list">/list</a> — Liste des nuages</li>
</ul>
<h3>Configuration</h3>
<ul>
<li>entwine : {"" + entwine_path if entwine_path else "❌ Non trouvé"}</li>
<li>pdal path : {"" + pdal["path"] if pdal["path"] else "❌ Non trouvé"}</li>
<li>pdal version : {pdal["version"] if pdal["version"] else "❌ Inconnue"}</li>
<li>Espace disque : {get_disk_usage()} GB libres</li>
</ul>
"""