-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathindex.py
More file actions
81 lines (61 loc) · 1.97 KB
/
index.py
File metadata and controls
81 lines (61 loc) · 1.97 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
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
#!/usr/bin/env python
from flask import Flask, request, jsonify
from waitress import serve
import os
import sys
import signal
from function import handler
app = Flask(__name__)
def SignalHandler(SignalNumber, Frame):
timeout = os.getenv("write_timeout")
sys.stderr.write('Function got SIGTERM, draining for up to: {}\n'.format(timeout))
sys.stderr.flush()
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.getenv('HOSTNAME', 'localhost')
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__':
signal.signal(signal.SIGTERM, SignalHandler)
serve(app, host='0.0.0.0', port=5000)