|
16 | 16 | app = Flask(__name__, static_url_path='', static_folder='public')
|
17 | 17 | app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html'))
|
18 | 18 |
|
| 19 | + |
19 | 20 | @app.route('/api/comments', methods=['GET', 'POST'])
|
20 | 21 | def comments_handler():
|
21 |
| - |
22 |
| - with open('comments.json', 'r') as file: |
23 |
| - comments = json.loads(file.read()) |
| 22 | + with open('comments.json', 'r') as f: |
| 23 | + comments = json.loads(f.read()) |
24 | 24 |
|
25 | 25 | if request.method == 'POST':
|
26 |
| - newComment = request.form.to_dict() |
27 |
| - newComment['id'] = int(time.time() * 1000) |
28 |
| - comments.append(newComment) |
| 26 | + new_comment = request.form.to_dict() |
| 27 | + new_comment['id'] = int(time.time() * 1000) |
| 28 | + comments.append(new_comment) |
| 29 | + |
| 30 | + with open('comments.json', 'w') as f: |
| 31 | + f.write(json.dumps(comments, indent=4, separators=(',', ': '))) |
29 | 32 |
|
30 |
| - with open('comments.json', 'w') as file: |
31 |
| - file.write(json.dumps(comments, indent=4, separators=(',', ': '))) |
| 33 | + return Response( |
| 34 | + json.dumps(comments), |
| 35 | + mimetype='application/json', |
| 36 | + headers={ |
| 37 | + 'Cache-Control': 'no-cache', |
| 38 | + 'Access-Control-Allow-Origin': '*' |
| 39 | + } |
| 40 | + ) |
32 | 41 |
|
33 |
| - return Response(json.dumps(comments), mimetype='application/json', headers={'Cache-Control': 'no-cache', 'Access-Control-Allow-Origin': '*'}) |
34 | 42 |
|
35 | 43 | if __name__ == '__main__':
|
36 |
| - app.run(port=int(os.environ.get("PORT",3000))) |
| 44 | + app.run(port=int(os.environ.get("PORT", 3000))) |
0 commit comments