hwdocker/web/app.py

30 lines
657 B
Python
Raw Normal View History

2024-11-23 14:00:50 +00:00
from flask import Flask, jsonify
import pymysql
import os
app = Flask(__name__)
def get_db_connection():
return pymysql.connect(
host='db',
user=os.getenv('MYSQL_USER'),
password=os.getenv('MYSQL_PASSWORD'),
db=os.getenv('MYSQL_DATABASE')
)
@app.route('/')
def index():
conn = get_db_connection()
with conn.cursor() as cursor:
cursor.execute("SELECT name, age FROM data")
result = cursor.fetchall()
return jsonify(result)
@app.route('/health')
def health():
return jsonify({"status": "OK"})
@app.errorhandler(404)
def not_found(e):
return jsonify({"error": "Not found"}), 404