On boot, run_migrations() checks the Alembic revision: - no revision + tables exist → stamp head (db.sql was run manually) - no revision + no tables → upgrade head (fresh install) - revision present → upgrade head (no-op if already current) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
980 B
Python
45 lines
980 B
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from app.db import check_db, run_migrations
|
|
from app.routers import filaments, pricing, print_jobs, printers, todos
|
|
|
|
logger = logging.getLogger("pops")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
ok, msg = check_db()
|
|
if ok:
|
|
logger.info("Database connection OK")
|
|
run_migrations()
|
|
else:
|
|
logger.warning("Database unreachable: %s", msg)
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="POPS", lifespan=lifespan)
|
|
|
|
app.include_router(printers.router)
|
|
app.include_router(filaments.router)
|
|
app.include_router(print_jobs.router)
|
|
app.include_router(todos.router)
|
|
app.include_router(pricing.router)
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"message": "Hello from POPS"}
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
ok, msg = check_db()
|
|
return {
|
|
"status": "ok" if ok else "degraded",
|
|
"db": "connected" if ok else "unreachable",
|
|
"detail": msg,
|
|
}
|