-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (49 loc) · 1.56 KB
/
app.py
File metadata and controls
65 lines (49 loc) · 1.56 KB
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
61
62
63
64
65
from flask import Flask, jsonify, request, g
import sqlite3, sys
app = Flask(__name__)
DATABASE = 'game.db'
@app.route("/")
def helloRoot():
return "hello"
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route("/post/<int:post_id>")
def hello(post_id):
id = '%d' % post_id
return jsonify({
"here is the id": id
})
@app.route('/api/games', methods=['GET'])
def get_games():
# Connect to the database
cur = get_db().cursor()
# Query the database for games, (game is the table we are querying)
query = "select * from game"
db_data = cur.execute(query).fetchall()
response_data = []
for row in db_data:
response_data.append(row[1])
print( " " + row[1] +" ", file=sys.stdout)
# Return the data as JSON
return jsonify({"data" :response_data})
@app.route('/api/test', methods=['POST'])
def get_home():
req_body = request.get_json()
print(request.get_json(), file=sys.stdout)
return jsonify({"Hello": "it works"})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == '__main__':
app.run(debug=True)