-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (53 loc) · 1.87 KB
/
Copy pathapp.py
File metadata and controls
60 lines (53 loc) · 1.87 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
from flask import Flask, jsonify, request, abort
import json
import os
app = Flask(__name__)
USERNAME = "<INSERT YOUR NAME HERE>"
@app.route('/', methods=['GET'])
def home():
#Return simple string for home page
return 'R&I 6'
@app.route('/posts', methods=['GET'], defaults={'postid': None})
@app.route('/posts/<int:postid>', methods=['GET'])
def query_posts(postid):
filename = os.path.join(app.static_folder, 'posts.json')
with open(filename) as f:
records = json.load(f)
if postid:
for item in records:
if item['id'] == postid:
return jsonify(item)
abort(404)
else:
return jsonify(records)
@app.route('/comments', methods=['GET'], defaults={'comment_id': None})
@app.route('/comments/<int:comment_id>', methods=['GET'])
def query_comments(comment_id):
filename = os.path.join(app.static_folder, 'comments.json')
with open(filename) as f:
records = json.load(f)
if comment_id:
for item in records:
if item['id'] == comment_id:
return jsonify(item)
abort(404)
else:
return jsonify(records)
@app.route('/users', methods=['GET'], defaults={'user_id': None})
@app.route('/users/<int:user_id>', methods=['GET'])
def query_users(user_id):
filename = os.path.join(app.static_folder, 'users.json')
with open(filename) as f:
records = json.load(f)
if user_id:
for item in records:
if item['id'] == user_id:
return jsonify(item)
abort(404)
else:
return jsonify(records)
## ADD COMMENTS AND USERS METHODS HERE
if __name__ == '__main__':
#Run app on port 5000 in debug mode. Host is specified as Flask needs you to give a host for apps that would
#need to be externally visible - in this case, from outside the container
app.run(debug=True, host='0.0.0.0', port=5000)