"""Testing API behind one shared token. One process; its background worker pool runs inference.""" from contextlib import asynccontextmanager import json import logging import secrets import time from fastapi import FastAPI, Header, HTTPException, Response from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, JSONResponse from app.catalog import Catalog from app.characters import Characters from app.core.body_limit import BodyLimit from app.core.config import Settings from app.db import Database from app.jobs import Jobs, TEST_USER from app.mcp import build_mcp from app.schemas import GenerationRequest from app.storage import Storage logging.basicConfig(level=logging.INFO, format='%(asctime)s %(name)s %(levelname)s %(message)s') log = logging.getLogger('mod.api') def create_app(settings=None, db=None, runtime=None): settings = settings or Settings.from_env() if settings.environment != 'testing': raise ValueError('This application is for testing only') storage = Storage(settings.storage_root) database = db or Database(settings.database_url) catalog = Catalog(settings.catalog_file, settings.environment) characters = Characters(settings.characters_dir) jobs = Jobs(database, catalog, settings, characters) # The token is the access control, so the SDK's localhost-only Host check is turned off. from mcp.server.transport_security import TransportSecuritySettings mcp_server = build_mcp(jobs, characters, settings) mcp_app = mcp_server.streamable_http_app( stateless_http=True, json_response=True, transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False)) if runtime is None and settings.worker_enabled: from app.worker import LocalRuntime runtime = LocalRuntime(database, storage, catalog, settings) @asynccontextmanager async def lifespan(app): database.migrate() if runtime: runtime.start() try: async with mcp_server.session_manager.run(): yield finally: if runtime: runtime.close() app = FastAPI(title='Mod Motion ยท Influencer video', version='0.3.0', lifespan=lifespan) app.state.jobs, app.state.storage = jobs, storage def authorized(request): path = request.url.path # Artifact links are unguessable IDs so a