45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi import HTTPException
|
|
from fastapi import Request
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from app.inspection import InspectionError, UnsafeUrlError, inspect_url
|
|
from app.models import InspectionRequest, InspectionResponse
|
|
|
|
app = FastAPI(
|
|
title="Media Ingest",
|
|
description="Private broadcast media-ingest service.",
|
|
version="0.1.0",
|
|
)
|
|
|
|
app_directory = Path(__file__).resolve().parent
|
|
templates = Jinja2Templates(directory=app_directory / "templates")
|
|
app.mount("/static", StaticFiles(directory=app_directory / "static"), name="static")
|
|
|
|
|
|
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
|
|
def index(request: Request) -> HTMLResponse:
|
|
return templates.TemplateResponse(request=request, name="index.html")
|
|
|
|
|
|
@app.get("/health", tags=["system"])
|
|
def health() -> dict[str, str]:
|
|
"""Report whether the application process can serve requests."""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
@app.post("/api/inspect", response_model=InspectionResponse, tags=["inspection"])
|
|
async def inspect_source(request: InspectionRequest) -> dict[str, object]:
|
|
"""Inspect a public video URL without downloading media."""
|
|
try:
|
|
return await run_in_threadpool(inspect_url, request.url)
|
|
except UnsafeUrlError as error:
|
|
raise HTTPException(status_code=400, detail=str(error)) from error
|
|
except InspectionError as error:
|
|
raise HTTPException(status_code=422, detail=str(error)) from error
|