55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy import func, select
|
|
|
|
from app.config import settings
|
|
from app.database import async_session, engine
|
|
from app.models import Base, Program
|
|
from app.api import programs, events, tiers
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Create tables on startup
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
# Auto-seed if database is empty
|
|
async with async_session() as session:
|
|
result = await session.execute(select(func.count(Program.id)))
|
|
count = result.scalar()
|
|
if count == 0:
|
|
print(" → Database is empty — running seed...")
|
|
from seed import seed as run_seed
|
|
await run_seed()
|
|
|
|
yield
|
|
|
|
|
|
app = FastAPI(
|
|
title="KMountain Flower Radio API",
|
|
description="Backend API for the KMountain Flower Radio Station website.",
|
|
version="1.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Register routers
|
|
app.include_router(programs.router, prefix="/api/programs", tags=["programs"])
|
|
app.include_router(events.router, prefix="/api/events", tags=["events"])
|
|
app.include_router(tiers.router, prefix="/api/tiers", tags=["tiers"])
|
|
|
|
# Dev-only admin router (disabled in production)
|
|
if settings.ENV != "production":
|
|
from app.api import admin
|
|
app.include_router(admin.router, prefix="/api/admin", tags=["admin"])
|