File size: 838 Bytes
39fa80a 7b7f7f6 39fa80a 7b7f7f6 ed85cc6 7b7f7f6 39fa80a 7b7f7f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from fastapi import FastAPI
import mariadb
import os
app = FastAPI()
# MariaDB connection setup
def get_db_connection():
conn = mariadb.connect(
user=os.getenv("MARIADB_USER", "root"),
password=os.getenv("MARIADB_PASSWORD", ""),
host=os.getenv("MARIADB_HOST", "127.0.0.1"), # Force TCP connection
port=int(os.getenv("MARIADB_PORT", 3306)),
database=os.getenv("MARIADB_DATABASE", "test")
)
return conn
@app.get("/")
def greet_json():
# Example: test DB connection
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT 1")
result = cur.fetchone()
cur.close()
conn.close()
return {"Hello": "World!", "db_test": result[0]}
except Exception as e:
return {"Hello": "World!", "db_error": str(e)} |