working basic site admin
This commit is contained in:
+35
-2
@@ -1,14 +1,31 @@
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
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.models import Base, Program, StationConfig
|
||||
from app.user_models import User
|
||||
from app.api import programs, events, tiers, auth
|
||||
from app.api import programs, events, tiers, auth, station_config
|
||||
|
||||
|
||||
async def _ensure_station_config(session):
|
||||
"""Ensure the singleton station config row exists (idempotent)."""
|
||||
result = await session.execute(
|
||||
select(StationConfig).where(StationConfig.key == "default")
|
||||
)
|
||||
if result.scalar_one_or_none() is None:
|
||||
print(" → Station config missing — seeding defaults...")
|
||||
from seed import seed as run_seed
|
||||
# Only seed the station config, not the full dataset
|
||||
from seed import STATION_CONFIG, _upsert_station_config
|
||||
await _upsert_station_config(session, STATION_CONFIG)
|
||||
await session.commit()
|
||||
print(" ✓ Station config seeded")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -25,6 +42,11 @@ async def lifespan(app: FastAPI):
|
||||
print(" → Database is empty — running seed...")
|
||||
from seed import seed as run_seed
|
||||
await run_seed()
|
||||
return # Full seed already included station config
|
||||
|
||||
# Ensure station config exists (runs even if DB already had data)
|
||||
async with async_session() as session:
|
||||
await _ensure_station_config(session)
|
||||
|
||||
# Bootstrap admin user if configured
|
||||
if settings.ADMIN_USERNAME and settings.ADMIN_PASSWORD:
|
||||
@@ -46,6 +68,10 @@ async def lifespan(app: FastAPI):
|
||||
await session.commit()
|
||||
print(f" → Bootstrap admin created: {settings.ADMIN_USERNAME}")
|
||||
|
||||
# Ensure uploads directory exists
|
||||
uploads_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "uploads")
|
||||
os.makedirs(uploads_dir, exist_ok=True)
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -69,6 +95,13 @@ app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
||||
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"])
|
||||
app.include_router(station_config.router, prefix="/api/station-config", tags=["station-config"])
|
||||
|
||||
# Serve uploaded files (dev only — Nginx handles this in production)
|
||||
if settings.ENV != "production":
|
||||
uploads_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "uploads")
|
||||
if os.path.exists(uploads_dir):
|
||||
app.mount("/uploads", StaticFiles(directory=uploads_dir), name="uploads")
|
||||
|
||||
# Dev-only admin router (disabled in production)
|
||||
if settings.ENV != "production":
|
||||
|
||||
Reference in New Issue
Block a user