-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·86 lines (69 loc) · 2.88 KB
/
server.py
File metadata and controls
executable file
·86 lines (69 loc) · 2.88 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
82
83
84
85
86
#!/usr/bin/env python2
from flask import Flask, url_for, jsonify, request, abort, has_request_context
from client_lib import RedisClientManager
import logging
from logging.handlers import RotatingFileHandler
app = Flask(__name__)
client_manager = RedisClientManager()
class CustomFormatter(logging.Formatter):
def format(self, record):
if has_request_context():
record.path = request.path
record.endpoint = request.endpoint
record.remote_addr = request.remote_addr
record.access_route = request.access_route
record.headers = request.headers
record.data = request.get_data(as_text=True)
return super(CustomFormatter, self).format(record)
def setup_logging():
handler = RotatingFileHandler('logs/app.log', maxBytes=10*1024*1024, backupCount=20)
handler.setLevel(logging.INFO)
custom_format = '''%(levelname)s %(name)s %(path)s %(endpoint)s %(remote_addr)s %(access_route)s %(message)s\n%(headers)s\n%(data)s\n *******''' # noqa E105
handler.setFormatter(CustomFormatter(fmt=custom_format))
app.logger.addHandler(handler)
@app.after_request
def add_header(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
return response
@app.route('/advanced', methods=['GET'])
def advanced():
client = client_manager.new_client()
title, path = client.get_next_path()
next_url = url_for('step', step_id=path,
_external=True)
app.logger.warning('client id {}'.format(client.id))
output = {title: next_url, 'token': client.id}
return jsonify(**output)
@app.route('/', methods=['GET'])
def root():
client = client_manager.new_client(easy=True)
title, path = client.get_next_path()
next_url = url_for('step', step_id=path,
_external=True)
app.logger.warning('client id {}'.format(client.id))
output = {title: next_url, 'token': client.id}
return jsonify(**output)
@app.route('/steps/<step_id>', methods=['POST'])
def step(step_id):
req_json = request.get_json()
if req_json is None or 'token' not in req_json:
app.logger.warning('returning 400 due to no json or no token')
abort(400)
token = req_json['token']
client = client_manager.get_client(token)
if not client:
app.logger.warning('returning 401 due to client token not in the db')
abort(401)
app.logger.warning('found token in db')
next_path = client.get_next_path(step_id)
if next_path:
title, path = next_path
next_url = url_for('step', step_id=path,
_external=True)
outgoing = {title: next_url, 'token': token}
else: # no path left, they are at the end!
outgoing = {'answer': 42, 'greeting': 'So long, and thanks for all the fish!'}
return jsonify(**outgoing)
setup_logging()
if __name__ == '__main__':
app.run()