-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathindex.py
69 lines (54 loc) · 1.69 KB
/
index.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
#!/usr/bin/env python
from flask import Flask, request, jsonify
from waitress import serve
import os
from function import handler
app = Flask(__name__)
class Event:
def __init__(self):
self.body = request.get_data()
self.headers = request.headers
self.method = request.method
self.query = request.args
self.path = request.path
class Context:
def __init__(self):
self.hostname = os.environ['HOSTNAME']
def format_status_code(resp):
if 'statusCode' in resp:
return resp['statusCode']
return 200
def format_body(resp):
if 'body' not in resp:
return ""
elif type(resp['body']) == dict:
return jsonify(resp['body'])
else:
return str(resp['body'])
def format_headers(resp):
if 'headers' not in resp:
return []
elif type(resp['headers']) == dict:
headers = []
for key in resp['headers'].keys():
header_tuple = (key, resp['headers'][key])
headers.append(header_tuple)
return headers
return resp['headers']
def format_response(resp):
if resp == None:
return ('', 200)
statusCode = format_status_code(resp)
body = format_body(resp)
headers = format_headers(resp)
return (body, statusCode, headers)
@app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
@app.route('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
def call_handler(path):
event = Event()
context = Context()
response_data = handler.handle(event, context)
resp = format_response(response_data)
return resp
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=5000)