-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
48 lines (33 loc) · 1.29 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
import os
from flask import Flask, jsonify, request, abort
from flask_cors import cross_origin
from trfind.find import get_all_trip_reports
from trfind.models import Peak
from trfind.weather import get_weather
app = Flask(__name__)
def _get_peak_from_request_or_400():
try:
return Peak(**request.json['data'])
except TypeError:
abort(400)
@app.route('/test', methods=['GET'])
@cross_origin('localhost')
def test():
peak = Peak(lat=48.51152, lon=-121.05789, name="Forbidden Peak")
trip_reports = get_all_trip_reports(peak)
return jsonify({'data': tuple(trip_report._asdict() for trip_report in trip_reports)})
@app.route('/find', methods=['POST'])
@cross_origin('http://www.climbplan.com', 'localhost')
def find():
peak = _get_peak_from_request_or_400()
trip_reports = get_all_trip_reports(peak)
return jsonify({'data': tuple(trip_report._asdict() for trip_report in trip_reports)})
@app.route('/weather', methods=['POST'])
@cross_origin('http://www.climbplan.com', 'localhost')
def weather():
peak = _get_peak_from_request_or_400()
weathers = get_weather(peak)
return jsonify({'data': tuple(weather._asdict() for weather in weathers)})
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)