Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (37 loc) · 1.4 KB
/
Copy pathapp.py
File metadata and controls
54 lines (37 loc) · 1.4 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
import os
from datetime import datetime
from flask import Flask, render_template, make_response
app = Flask(__name__)
def get_app_debug_info():
cfg_items = {k: v for k, v in os.environ.items()}
cfg_items['datetime'] = datetime.now().isoformat()
return cfg_items
@app.route('/')
def welcome():
return {
'msg': 'Hello World! This is a simple Python app using Flask! But wait there is more!',
'endpoints': ['/', '/ping', '/debug', '/debug/ui']
}
@app.route('/ping')
def ping():
return {'msg': 'pong!'}
@app.route('/debug', methods=['GET'])
def debug():
cfg_items = get_app_debug_info()
response = make_response(cfg_items, 200)
# Enable CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
response.headers['Access-Control-Allow-Origin'] = '*' # allow all domains for now
response.headers['Access-Control-Allow-Methods'] = "GET"
return response
@app.route('/debug/ui', methods=['GET'])
def debug_ui():
cfg_map = get_app_debug_info()
# sort items by key
cfg_items = sorted([{'k': k, 'v': v} for k, v in cfg_map.items()], key=lambda x: x['k'].upper())
return render_template('debug.html', cfg_items=cfg_items, title='Hello Python Debug!')
@app.errorhandler(404)
def not_found(e):
return {'err': 'Not found!'}, 404
if __name__ == '__main__':
port = os.environ.get('PORT', 5001)
app.run(debug=True, host='0.0.0.0', port=port)