-
Notifications
You must be signed in to change notification settings - Fork 125
/
app.py
60 lines (52 loc) · 1.85 KB
/
app.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, jsonify, render_template, request
import pymysql
app = Flask(__name__)
def get_db_connection():
connection = pymysql.connect(host='mydb.cylck8yh5jkc.eu-central-1.rds.amazonaws.com', # Replace with your RDS endpoint
user='dbuser', # Replace with your RDS username
password='dbpassword', # Replace with your RDS password
db='devprojdb', # Replace with your database name
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
@app.route('/health')
def health():
return "Up & Running"
@app.route('/create_table')
def create_table():
connection = get_db_connection()
cursor = connection.cursor()
create_table_query = """
CREATE TABLE IF NOT EXISTS example_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
)
"""
cursor.execute(create_table_query)
connection.commit()
connection.close()
return "Table created successfully"
@app.route('/insert_record', methods=['POST'])
def insert_record():
name = request.json['name']
connection = get_db_connection()
cursor = connection.cursor()
insert_query = "INSERT INTO example_table (name) VALUES (%s)"
cursor.execute(insert_query, (name,))
connection.commit()
connection.close()
return "Record inserted successfully"
@app.route('/data')
def data():
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute('SELECT * FROM example_table')
result = cursor.fetchall()
connection.close()
return jsonify(result)
# UI route
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')