Agents don't forget to come back; humans do. Make the whole pipeline drivable without the dashboard: - API keys (osk_...): sha256-stored tokens minted from the account page. get_current_user_optional resolves them like a session, so metering, entitlement, priority and job ownership apply to agents with zero endpoint changes. Key management refuses API-key auth (a leaked key cannot mint replacements). - MCP server at /mcp: stateless Streamable-HTTP JSON-RPC, no SDK dependency. Six tools (process_video, get_job_status, list_clips, get_quota, add_subtitles, publish_clip) that call back into this same app in-process via ASGITransport with the caller's auth headers, so MCP can never drift from REST behavior. Cloud requires a key; self-host stays BYOK-open. - Completion webhooks: webhook_url (+ optional HMAC webhook_secret) on /api/process, SSRF-checked at submit AND delivery, fired once after the R2 archive so payloads carry durable links, survives redeploys via the resume manifest. - Dashboard: API keys card in the account page (create once-shown key, list, revoke). - Tests pin BILLING_ENABLED=0 in conftest so the suite no longer depends on the developer's personal .env.
52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
"""Optional cloud (paid / managed-keys) mode for OpenShorts.
|
|
|
|
This whole package is dormant unless the ``BILLING_ENABLED`` env flag is set.
|
|
``app.py`` imports it only in that case, so self-hosters never need the extra
|
|
dependencies in ``requirements-billing.txt``.
|
|
|
|
Wiring is split in two because Starlette forbids adding middleware after the app
|
|
has started:
|
|
- ``setup_sync(app)`` -> runs at import time (middleware + routers)
|
|
- ``setup_async(app)`` -> runs in the lifespan (DB engine, sweeper, orphans)
|
|
|
|
Both are built up phase by phase; each phase appends its own wiring.
|
|
"""
|
|
from .config import is_enabled, settings, validate_required # noqa: F401
|
|
|
|
|
|
def setup_sync(app):
|
|
"""Synchronous wiring done at import time (before the app serves).
|
|
|
|
Middleware and routers must be attached here. Fails fast on missing config
|
|
so a misconfigured deploy never boots half-enabled.
|
|
"""
|
|
validate_required()
|
|
|
|
# SessionMiddleware backs the OAuth state handshake.
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
app.add_middleware(SessionMiddleware, secret_key=settings.jwt_secret)
|
|
|
|
from . import auth, oauth, billing, social_profiles, videos, api_keys
|
|
oauth.register()
|
|
billing._init_stripe()
|
|
app.include_router(auth.router)
|
|
app.include_router(oauth.router)
|
|
app.include_router(billing.router)
|
|
app.include_router(social_profiles.router)
|
|
app.include_router(videos.router)
|
|
app.include_router(api_keys.router)
|
|
|
|
|
|
async def setup_async(app, keep_reservation_ids=None):
|
|
"""Async initialization done inside the FastAPI lifespan.
|
|
|
|
DB engine, orphaned-reservation cleanup and the metering sweeper live here.
|
|
``keep_reservation_ids`` belong to jobs being resumed after a restart and
|
|
must not be refunded (see app._resume_interrupted_jobs).
|
|
"""
|
|
from . import database, metering, videos
|
|
await database.init_engine()
|
|
await metering.release_orphaned_reservations(keep_ids=keep_reservation_ids)
|
|
metering.start_sweeper()
|
|
videos.start_sweeper()
|
|
print("☁️ Cloud billing mode ENABLED (DB ready, metering active).")
|