-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
95 lines (76 loc) · 2.92 KB
/
app.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from flask import Flask, json, jsonify, request
import psycopg2
from routing import routing_request
from isochrone import isochrone_data
app = Flask(__name__, instance_relative_config=True)
# Get default config (main app dir config.py) or environment variables
app.config.from_object('config')
# Get instance config (hidden from git, is in app dir/instance/config.py)
# Overrides default config.py and environment variables settings
try:
app.config.from_pyfile('config.py')
except IOError:
pass
# Enables debug traceback for logging
app.config['PROPAGATE_EXCEPTIONS'] = True
DB_HOST = app.config['DB_HOST']
DB_USER = app.config['DB_USER']
DB_PASS = app.config['DB_PASS']
DB_NAME = app.config['DB_NAME']
DB_PORT = app.config['DB_PORT']
ROUTING_TABLE = app.config['ROUTING_TABLE']
@app.route('/')
def index():
return 'Hello World!'
@app.route('/route.json', methods=['GET'])
def route():
#####################
# Process arguments #
#####################
# TODO: input validation - return reasonable HTTP errors on bad input.
# latlon (required!)
waypoints_input = request.args.get('waypoints', None)
if waypoints_input is None:
return 'Bad request - waypoints parameter is required.'
waypoints_input_list = json.loads(waypoints_input)
# Consume in pairs
waypoints = zip(waypoints_input_list[0::2], waypoints_input_list[1::2])
# Default distance cost constant is 1
kdist = request.args.get('dist', 1)
# Default elevation cost constant is 4
kele = request.args.get('ele', 1e10)
#################
# request route #
#################
conn = psycopg2.connect(host=DB_HOST, dbname=DB_NAME,
user=DB_USER, password=DB_PASS,
connect_timeout=15.)
route_response = routing_request(conn, ROUTING_TABLE, waypoints, kdist,
kele)
return jsonify(route_response)
@app.route('/isochrone.json', methods=['GET'])
def isochrone():
#####################
# Process arguments #
#####################
lon = float(request.args.get('lon'))
lat = float(request.args.get('lat'))
if lon is None or lat is None:
return 'Bad request - lon and lat parameters are required.'
##########################
# request isochrone_data #
##########################
conn = psycopg2.connect(host=DB_HOST, dbname=DB_NAME,
user=DB_USER, password=DB_PASS,
connect_timeout=15.)
data = isochrone_data(conn, [lon, lat])
return jsonify(data)
@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,OPTIONS')
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)