Add FastAPI hello-world structure with Docker and DB health check
- 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>
This commit is contained in:
22
app/db.py
Normal file
22
app/db.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
import pymysql
|
||||
|
||||
|
||||
def _params() -> dict:
|
||||
return {
|
||||
"host": os.environ["MYSQL_HOST"],
|
||||
"port": int(os.environ.get("MYSQL_PORT", 3306)),
|
||||
"user": os.environ["MYSQL_USER"],
|
||||
"password": os.environ["MYSQL_PASSWORD"],
|
||||
"database": os.environ["MYSQL_DATABASE"],
|
||||
"connect_timeout": 3,
|
||||
}
|
||||
|
||||
|
||||
def check_db() -> bool:
|
||||
try:
|
||||
conn = pymysql.connect(**_params())
|
||||
conn.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
Reference in New Issue
Block a user