30 lines
657 B
Python
30 lines
657 B
Python
|
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
|