-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
26 lines (19 loc) · 871 Bytes
/
server.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
""" Web server based on Flask
Reference: https://github.com/reactjs/react-tutorial/blob/master/server.py
"""
import json
import os
from flask import Flask, Response, request
app = Flask(__name__, static_url_path='', static_folder='public')
app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html'))
@app.route('/comments.json', methods=['GET', 'POST'])
def comments_handler():
with open('comments.json', 'r') as file:
comments = json.loads(file.read())
if request.method == 'POST':
comments.append(request.form.to_dict())
with open('comments.json', 'w') as file:
file.write(json.dumps(comments, indent=2, separators=(',', ': ')))
return Response(json.dumps(comments), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
if __name__ == '__main__':
app.run(port=int(os.environ.get('PORT', 3000)), debug=True)