forked from OpenShiftDemos/os-sample-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
68 lines (61 loc) · 2.17 KB
/
wsgi.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
from flask import Flask, request, Response, g
import os
import sys
import json
import uuid
import tempfile
application = Flask(__name__)
application.debug = True
def save_request(uuid, request):
req_data = {}
req_data['uuid'] = uuid
req_data['endpoint'] = request.endpoint
req_data['method'] = request.method
req_data['cookies'] = request.cookies.to_dict()
req_data['data'] = request.data.decode()
req_data['headers'] = dict(request.headers)
req_data['headers'].pop('Cookie', None)
req_data['args'] = request.args.to_dict()
req_data['form'] = request.form.to_dict()
req_data['remote_addr'] = request.remote_addr
files = []
for name, fs in request.files.items():
dst = tempfile.NamedTemporaryFile()
fs.save(dst)
dst.flush()
filesize = os.stat(dst.name).st_size
dst.close()
files.append({'name': name, 'filename': fs.filename, 'filesize': filesize,
'mimetype': fs.mimetype, 'mimetype_params': fs.mimetype_params})
req_data['files'] = files
return req_data
def save_response(uuid, resp):
resp_data = {}
resp_data['uuid'] = uuid
resp_data['status_code'] = resp.status_code
resp_data['status'] = resp.status
resp_data['headers'] = dict(resp.headers)
resp_data['data'] = b"".join(resp.response).decode()
return resp_data
@application.before_request
def before_request():
print(request.method, request.endpoint)
@application.after_request
def after_request(resp):
resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('Access-Control-Allow-Headers', 'Content-Type, X-Token')
resp.headers.add('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
resp_data = save_response(g.uuid, resp)
print(resp_data, flush=True)
print('Response:: ', json.dumps(resp_data, indent=4))
return resp
@application.route("/")
def hello():
g.uuid = uuid.uuid1().hex
req_data = save_request(g.uuid, request)
print(req_data, flush=True)
resp = Response(json.dumps(req_data, indent=4), mimetype='application/json')
resp.set_cookie('cookie-name', value='cookie-value')
return resp
if __name__ == "__main__":
application.run()