Adds an optional cloud/paid mode behind a BILLING_ENABLED flag. With the flag off the app is byte-identical to today's self-hosted BYOK build (regression-zero). Backend (cloud/ package, only imported when BILLING_ENABLED): - Auth: magic link (Resend) + Google OAuth (authlib) + JWT sessions - Stripe: hosted Checkout with 3-day card trial, Customer Portal, idempotent order-safe webhooks; 3 plans (Starter/Creator/Pro) resolved by lookup_key - Postgres + SQLAlchemy async; atomic minute metering (reserve/commit/release, plan->topup FIFO, restart-safe) with sweeper - Managed keys: Gemini + Upload-Post server-side for entitled users; paid-only (BYOK core header ignored on hosted); Upload-Post white-label per-user profiles - Metering wired into /api/process (probe->reserve->402, priority queue) and thumbnail image generation (3 min/batch) - yt-dlp residential PROXY_URL support + 720p cap for hosted YouTube downloads Frontend: - AuthContext, LoginModal, PricingSection (3-day trial), AccountPage, UsageMeter, TopUpModal, TrialGate/AdvancedBanner - Paid-only UI: hide BYOK core inputs, trial prompts per tool - Included (Clip Generator, YouTube Studio) vs BYOK (AI Shorts/dubbing: fal.ai + ElevenLabs) clearly separated; ThumbnailStudio + SaaShortsTab managed-aware - Landing: free-self-host vs paid-hosted messaging Verified end-to-end against real Postgres + Stripe test mode.
47 lines
1.7 KiB
Python
47 lines
1.7 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
|
|
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)
|
|
|
|
|
|
async def setup_async(app):
|
|
"""Async initialization done inside the FastAPI lifespan.
|
|
|
|
DB engine, orphaned-reservation cleanup and the metering sweeper live here.
|
|
"""
|
|
from . import database, metering
|
|
await database.init_engine()
|
|
await metering.release_orphaned_reservations()
|
|
metering.start_sweeper()
|
|
print("☁️ Cloud billing mode ENABLED (DB ready, metering active).")
|