Mercurial > templog
comparison web/templog.py @ 253:0a1b642e3086
long polling config updates
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 08 Jun 2015 22:29:46 +0800 |
parents | 141948a400a6 |
children | 8ef52f27cf95 |
comparison
equal
deleted
inserted
replaced
251:b6079cb0c665 | 253:0a1b642e3086 |
---|---|
26 | 26 |
27 class TemplogBottle(bottle.Bottle): | 27 class TemplogBottle(bottle.Bottle): |
28 def run(*args, **argm): | 28 def run(*args, **argm): |
29 argm['server'] = 'gevent' | 29 argm['server'] = 'gevent' |
30 super(TemplogBottle, self).run(*args, **argm) | 30 super(TemplogBottle, self).run(*args, **argm) |
31 print "ran custom bottle" | |
32 | 31 |
33 #bottle.default_app.push(TemplogBottle()) | 32 bottle.default_app.push(TemplogBottle()) |
34 | 33 |
35 secure.setup_csrf() | 34 secure.setup_csrf() |
36 | 35 |
37 @route('/update', method='post') | 36 @route('/update', method='post') |
38 def update(): | 37 def update(): |
60 return log.graph_png(start_epoch, length_minutes * 60) | 59 return log.graph_png(start_epoch, length_minutes * 60) |
61 | 60 |
62 def encode_data(data, mimetype): | 61 def encode_data(data, mimetype): |
63 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) | 62 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) |
64 | 63 |
65 | |
66 @route('/graph.png') | 64 @route('/graph.png') |
67 def graph(): | 65 def graph(): |
68 response.set_header('Content-Type', 'image/png') | 66 response.set_header('Content-Type', 'image/png') |
69 minutes, endstr = get_request_zoom() | 67 minutes, endstr = get_request_zoom() |
70 return make_graph(minutes, endstr) | 68 return make_graph(minutes, endstr) |
74 post_json = json.loads(request.forms.data) | 72 post_json = json.loads(request.forms.data) |
75 | 73 |
76 csrf_blob = post_json['csrf_blob'] | 74 csrf_blob = post_json['csrf_blob'] |
77 | 75 |
78 if not secure.check_csrf_blob(csrf_blob): | 76 if not secure.check_csrf_blob(csrf_blob): |
79 bottle.response.status = 403 | 77 response.status = 403 |
80 return "Bad csrf" | 78 return "Bad csrf" |
81 | 79 |
82 ret = log.update_params(post_json['params']) | 80 ret = log.update_params(post_json['params']) |
83 if not ret is True: | 81 if not ret is True: |
84 bottle.response.status = 403 | 82 response.status = 403 |
85 return ret | 83 return ret |
86 | 84 |
87 return "Good" | 85 return "Good" |
88 | 86 |
89 @route('/set') | 87 @route('/set') |
157 #return str(request.environ) | 155 #return str(request.environ) |
158 #yield "\n" | 156 #yield "\n" |
159 #var_lookup = environ['mod_ssl.var_lookup'] | 157 #var_lookup = environ['mod_ssl.var_lookup'] |
160 #return var_lookup("SSL_SERVER_I_DN_O") | 158 #return var_lookup("SSL_SERVER_I_DN_O") |
161 | 159 |
160 @route('/get_settings') | |
161 def get_settings(): | |
162 response.set_header('Cache-Control', 'no-cache') | |
163 req_etag = request.headers.get('etag', None) | |
164 if req_etag: | |
165 # wait for it to change | |
166 if not log.fridge_settings.wait(req_etag, timeout=LONG_POLL_TIMEOUT): | |
167 response.status = 304 | |
168 return "Nothing happened" | |
169 | |
170 response.set_header('Content-Type', 'application/json') | |
171 contents, epoch_tag = client_settings.get() | |
172 return json.dumps({'params': contents, 'epoch_tag': epoch_tag}) | |
173 | |
162 @bottle.get('/<filename:re:.*\.js>') | 174 @bottle.get('/<filename:re:.*\.js>') |
163 def javascripts(filename): | 175 def javascripts(filename): |
164 response.set_header('Cache-Control', "public, max-age=1296000") | 176 response.set_header('Cache-Control', "public, max-age=1296000") |
165 return bottle.static_file(filename, root='static') | 177 return bottle.static_file(filename, root='static') |
178 | |
166 | 179 |
167 def main(): | 180 def main(): |
168 #bottle.debug(True) | 181 #bottle.debug(True) |
169 #bottle.run(reloader=True) | 182 #bottle.run(reloader=True) |
170 bottle.run(server='cgi', reloader=True) | 183 bottle.run(server='cgi', reloader=True) |