forked from qa-apprenticeships/devops-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
28 lines (25 loc) · 929 Bytes
/
app.py
File metadata and controls
28 lines (25 loc) · 929 Bytes
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
from flask import Flask, request, send_from_directory
from flask import Response
import pymysql.cursors
import os
import json
app = Flask(__name__, static_url_path='')
def get_connection():
return pymysql.connect(host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
db=os.getenv("MYSQL_DATABASE"),
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
@app.route('/api/books')
def get_all_books():
books = []
connection = get_connection()
try:
with connection.cursor() as cursor:
sql = "SELECT * FROM Books;"
cursor.execute(sql)
books = cursor.fetchall()
return Response(json.dumps(books), mimetype='application/json')
finally:
connection.close()