- app/models/: Printer, Filament, PrintJob, Todo, PricingConfig with full SQLAlchemy 2.x mapped columns and relationships - app/routers/: list + get-by-id endpoints for all five resources - app/db.py: SQLAlchemy engine + SessionLocal + get_db dependency - alembic/: env.py reads MYSQL_* env vars; 0001_initial_schema mirrors db.sql - alembic.ini: script_location configured, URL injected at runtime Since db.sql already created the tables, stamp the DB before running future migrations: alembic stamp head Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
44 lines
939 B
Python
44 lines
939 B
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from app.db import check_db
|
|
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")
|
|
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,
|
|
}
|