mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-02 09:58:08 +09:00
## What & why
Phase 1 of the **daily-free-community-track** initiative (BACKLOG:
*"Daily free community track via public voting board"*). A public,
Hacker-News-style voting board at **requests.nomadkaraoke.com** where
anyone signs in with an email magic link, submits a song request
(artist/title auto-corrected + deduped), and casts **one vote per
calendar day**. Publicly viewable so it can be linked from YouTube
descriptions to drive top-of-funnel awareness.
Ships the board first (this PR) so the linkable URL exists; the
automation (daily auto-picker, free-credit grant on pick, 24h ownership
handoff, voter publish-emails, and the trending-karaoke agent as a
fallback request source — one free track/day total) is **Phase 2**.
Design doc: `docs/archive/2026-09-02-requests-voting-board-plan.md`.
## Backend
- New `song_request` models + service (Firestore `song_requests` /
`song_request_votes`) with transactional vote arithmetic (create / move
/ flip / toggle-off), dedupe, and ranking. One-vote-per-day is enforced
structurally via the vote doc id (`{email}__{YYYY-MM-DD}`).
- New `/api/requests-board` router: `GET /requests` (public, annotates
the caller's vote), `POST /requests` (submit — auto-corrects via the
existing `match_judge`), `POST /requests/{id}/vote`, `GET /me`.
- Board sign-in (`purpose="requests_board"`): passwordless identity only
— **no welcome credit**, a **higher per-IP signup cap**
(`BOARD_MAX_SIGNUPS_PER_IP`, default 30) so a shared venue WiFi can
register many voters while the per-fingerprint cap stays strict, and
`redirect_path="/requests"`.
- New `POST /users/claim-welcome-credit` powers the "Make it yourself
now" conversion (grants the standard credit the normal way, idempotent).
## Frontend
- `app/[locale]/requests` board: submit form with "we tidied that to…"
feedback, ranked vote list with daily-vote UX, convert-to-gen CTA,
recently-made list with YouTube links.
- API client methods + types; verify page honors `redirect_path`; new
`requests` i18n namespace translated to **all 33 locales**.
- `requests` added to `nonTenantSubdomains`.
## Testing
- 20 backend unit tests (routes + pure logic + sign-in + board signup
limit) and 8 emulator tests exercising **real Firestore transactions**
(vote arithmetic, dedupe, idempotent resubmit, one-vote-per-day). Full
backend unit suite green (4247 passed); frontend typecheck clean.
- Reviewed locally with CodeRabbit CLI (3 cycles); all findings
addressed (auth headers, idempotent resubmit, signup-limit hardening,
blank-input validation, i18n particle, refresh race).
## Deploy note
The board is reachable at `gen.nomadkaraoke.com/en/requests` on deploy.
Wiring the vanity `requests.nomadkaraoke.com` is a **post-deploy
Cloudflare step** — see
`docs/runbooks/requests-subdomain-cloudflare.md`.
@coderabbitai ignore
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
248 lines
10 KiB
Python
248 lines
10 KiB
Python
"""
|
|
FastAPI application entry point for karaoke generation backend.
|
|
|
|
(Trivial comment touch to trigger backend CI on ephemeral runners after
|
|
the 2026-05-17 dispatcher e2 fix; safe to remove on next backend edit.)
|
|
"""
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from backend.config import settings, validate_production_config
|
|
from backend.api.routes import health, jobs, internal, file_upload, review, auth, audio_search, themes, users, admin, tenant, tenant_admin, tenant_bulk, rate_limits, push, catalog, encoding_worker, client_errors, bulk, parse_titles
|
|
from backend.services.tracing import setup_tracing, instrument_app, get_current_trace_id
|
|
from backend.services.structured_logging import setup_structured_logging
|
|
from backend.services.spacy_preloader import preload_spacy_model
|
|
from backend.services.nltk_preloader import preload_all_nltk_resources
|
|
from backend.services.langfuse_preloader import preload_langfuse_handler
|
|
from backend.middleware.audit_logging import AuditLoggingMiddleware
|
|
from backend.middleware.tenant import TenantMiddleware
|
|
from backend.middleware.edge_auth import EdgeAuthMiddleware
|
|
from backend.workers.registry import worker_registry
|
|
|
|
|
|
from backend.version import VERSION
|
|
|
|
|
|
# Configure structured logging (JSON in Cloud Run, human-readable locally)
|
|
# This must happen before any logging calls
|
|
setup_structured_logging()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Initialize OpenTelemetry tracing (must happen before app creation)
|
|
tracing_enabled = setup_tracing(
|
|
service_name="karaoke-backend",
|
|
service_version=VERSION,
|
|
enable_in_dev=False, # Set to True to enable tracing locally
|
|
)
|
|
|
|
|
|
async def validate_credentials_on_startup():
|
|
"""Validate OAuth credentials on startup and send alerts if needed."""
|
|
try:
|
|
from backend.services.credential_manager import get_credential_manager, CredentialStatus
|
|
|
|
manager = get_credential_manager()
|
|
results = manager.check_all_credentials()
|
|
|
|
invalid_services = [
|
|
result for result in results.values()
|
|
if result.status in (CredentialStatus.INVALID, CredentialStatus.EXPIRED)
|
|
]
|
|
|
|
if invalid_services:
|
|
logger.warning(f"Some OAuth credentials need attention:")
|
|
for result in invalid_services:
|
|
logger.warning(f" - {result.service}: {result.message}")
|
|
|
|
# Try to send Discord alert
|
|
discord_url = settings.get_secret("discord-alert-webhook") if hasattr(settings, 'get_secret') else None
|
|
if discord_url:
|
|
manager.send_credential_alert(invalid_services, discord_url)
|
|
logger.info("Sent credential alert to Discord")
|
|
else:
|
|
logger.info("All OAuth credentials validated successfully")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Failed to validate credentials on startup: {e}")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Lifespan event handler for startup and shutdown."""
|
|
# Startup
|
|
logger.info("Starting karaoke generation backend")
|
|
logger.info(f"Environment: {settings.environment}")
|
|
logger.info(f"GCS Bucket: {settings.gcs_bucket_name}")
|
|
logger.info(f"Tracing enabled: {tracing_enabled}")
|
|
|
|
# Fail fast if required production config is missing, rather than silently
|
|
# running on dev defaults (wrong GCP project / GCS bucket / localhost worker
|
|
# URL). No-op outside production. (Fallback audit 2026-06-09, Theme 7.)
|
|
validate_production_config()
|
|
|
|
# Preload NLP models and resources to avoid cold start delays
|
|
# See docs/archive/2026-01-08-performance-investigation.md for background
|
|
|
|
# 1. SpaCy model (60+ second delay without preload)
|
|
try:
|
|
preload_spacy_model("en_core_web_sm")
|
|
except Exception as e:
|
|
logger.warning(f"SpaCy preload failed (will load lazily): {e}")
|
|
|
|
# 2. NLTK cmudict (50-100+ second delay without preload)
|
|
try:
|
|
preload_all_nltk_resources()
|
|
except Exception as e:
|
|
logger.warning(f"NLTK preload failed (will load lazily): {e}")
|
|
|
|
# 3. Langfuse callback handler (200+ second delay without preload)
|
|
try:
|
|
preload_langfuse_handler()
|
|
except Exception as e:
|
|
logger.warning(f"Langfuse preload failed (will initialize lazily): {e}")
|
|
|
|
# Validate OAuth credentials (non-blocking)
|
|
try:
|
|
await validate_credentials_on_startup()
|
|
except Exception as e:
|
|
logger.error(f"Credential validation failed: {e}")
|
|
|
|
yield
|
|
|
|
# Shutdown - wait for any active workers to complete before terminating
|
|
# This prevents Cloud Run from killing workers mid-processing.
|
|
#
|
|
# Cloud Run gen2 default termination grace period is 600s. We wait up to
|
|
# 480s, leaving ~120s for the cleanup pass below to write Firestore state
|
|
# before SIGKILL. Without that headroom, render jobs that miss the wait
|
|
# would sit in `rendering_video` forever waiting for an operator.
|
|
logger.info("Shutdown requested, checking for active workers...")
|
|
if worker_registry.has_active_workers():
|
|
active = worker_registry.get_active_workers()
|
|
logger.info(f"Active workers found: {active}")
|
|
logger.info("Waiting for workers to complete (timeout: 480s)...")
|
|
completed = await worker_registry.wait_for_completion(timeout=480)
|
|
if not completed:
|
|
logger.error(
|
|
"Shutdown timeout - some workers may not have completed cleanly. "
|
|
f"Remaining workers: {worker_registry.get_active_workers()}"
|
|
)
|
|
|
|
# Park any still-active render jobs so the auto-retry scheduler can
|
|
# recover them. Safe to call unconditionally — it's a no-op if nothing
|
|
# render-related is in flight.
|
|
try:
|
|
from backend.workers.render_video_worker import park_active_render_jobs_for_shutdown
|
|
parked = park_active_render_jobs_for_shutdown()
|
|
if parked:
|
|
logger.warning(
|
|
f"Parked {parked} in-flight render job(s) for auto-retry "
|
|
f"due to shutdown"
|
|
)
|
|
except Exception as exc:
|
|
logger.error(f"Render-job shutdown park failed: {exc!r}")
|
|
else:
|
|
logger.info("No active workers, proceeding with shutdown")
|
|
|
|
logger.info("Shutting down karaoke generation backend")
|
|
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title="Karaoke Generator API",
|
|
description="Backend API for web-based karaoke video generation",
|
|
version=VERSION,
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Instrument FastAPI with OpenTelemetry (adds automatic spans for all requests)
|
|
if tracing_enabled:
|
|
instrument_app(app)
|
|
|
|
# Configure CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=False,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Add audit logging middleware (captures all requests with request_id for correlation)
|
|
app.add_middleware(AuditLoggingMiddleware)
|
|
|
|
# Add tenant detection middleware (extracts tenant from subdomain/headers)
|
|
app.add_middleware(TenantMiddleware)
|
|
|
|
# Edge origin lock (added last = outermost, runs first). Rejects direct-to-origin
|
|
# requests that bypassed the Cloudflare edge, before tenant/audit middleware do
|
|
# any work. No-op unless EDGE_AUTH_MODE is warn/enforce. See
|
|
# backend/middleware/edge_auth.py.
|
|
app.add_middleware(EdgeAuthMiddleware)
|
|
|
|
# Include routers
|
|
app.include_router(health.router, prefix="/api")
|
|
app.include_router(jobs.router, prefix="/api")
|
|
app.include_router(file_upload.router, prefix="/api") # File upload endpoint
|
|
app.include_router(internal.router, prefix="/api") # Internal worker endpoints
|
|
app.include_router(encoding_worker.router, prefix="/api") # Encoding worker lifecycle
|
|
app.include_router(review.router, prefix="/api") # Review UI compatibility endpoints
|
|
app.include_router(auth.router, prefix="/api") # OAuth credential management
|
|
app.include_router(audio_search.router, prefix="/api") # Audio search (artist+title mode)
|
|
app.include_router(themes.router, prefix="/api") # Theme selection for styles
|
|
app.include_router(users.router, prefix="/api") # User auth, credits, and Stripe webhooks
|
|
from backend.api.routes import referrals
|
|
app.include_router(referrals.router, prefix="/api") # Referral links and payouts
|
|
app.include_router(admin.router, prefix="/api") # Admin dashboard and management
|
|
app.include_router(rate_limits.router, prefix="/api") # Rate limits admin management
|
|
app.include_router(push.router, prefix="/api") # Push notification subscription management
|
|
app.include_router(catalog.router, prefix="/api") # Catalog proxy for song/artist autocomplete
|
|
app.include_router(parse_titles.router, prefix="/api") # kjbox karaoke-filename parser
|
|
app.include_router(bulk.router, prefix="/api") # Bulk Mode: multi-job submission
|
|
from backend.api.routes import requests_board
|
|
app.include_router(requests_board.router, prefix="/api") # Public song-request voting board
|
|
app.include_router(client_errors.router, prefix="/api") # Frontend crash reports
|
|
app.include_router(tenant.router) # Tenant/white-label configuration (no /api prefix, router has it)
|
|
app.include_router(tenant_admin.router) # Admin tenant provisioning (router has /api prefix)
|
|
app.include_router(tenant_bulk.router) # Tenant bulk-upload filename analysis (router has /api prefix)
|
|
|
|
|
|
# Exception handlers
|
|
from fastapi import Request
|
|
from fastapi.responses import JSONResponse
|
|
from backend.exceptions import InsufficientCreditsError
|
|
from backend.i18n import t, get_locale_from_request
|
|
|
|
|
|
@app.exception_handler(InsufficientCreditsError)
|
|
async def insufficient_credits_exception_handler(request: Request, exc: InsufficientCreditsError):
|
|
"""Handle insufficient credits errors with 402 Payment Required status."""
|
|
locale = get_locale_from_request(request)
|
|
return JSONResponse(
|
|
status_code=402,
|
|
content={
|
|
"detail": t(locale, "errors.insufficientCredits"),
|
|
"credits_available": exc.credits_available,
|
|
"credits_required": exc.credits_required,
|
|
"buy_url": t(locale, "errors.insufficientCreditsBuyUrl"),
|
|
},
|
|
)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint."""
|
|
return {
|
|
"service": "karaoke-gen-backend",
|
|
"version": VERSION,
|
|
"status": "running"
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8080)
|
|
|