- app/main.py: root endpoint + /health that reports DB connectivity - app/db.py: pymysql connection check from MYSQL_* env vars, logged at startup - Dockerfile: python:3.12-slim, port 8720 - docker-compose.yml: mysql:8.2 with healthcheck, app waits for DB ready - requirements.txt: fastapi, uvicorn, pymysql Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
35 lines
682 B
Python
35 lines
682 B
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from app.db import check_db
|
|
|
|
logger = logging.getLogger("pops")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
if check_db():
|
|
logger.info("Database connection OK")
|
|
else:
|
|
logger.warning("Database unreachable — check MYSQL_* environment variables")
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="POPS", lifespan=lifespan)
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"message": "Hello from POPS"}
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
db_ok = check_db()
|
|
return {
|
|
"status": "ok" if db_ok else "degraded",
|
|
"db": "connected" if db_ok else "unreachable",
|
|
}
|