annotate web/templog.py @ 555:9dea75bd765f

merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Jun 2015 22:33:04 +0800
parents d16afb5b5cd9
children 03e540c3ec24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
3 import binascii
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
4 import json
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
5 import hmac
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
6 import zlib
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
7 from datetime import datetime, timedelta
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
8 import time
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
9 import urllib
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
10 import sys
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
11 import os
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
12 import traceback
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
13 import fcntl
541
bde53484df3c sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 540
diff changeset
14 import hashlib
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
15
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 import bottle
337
f575ef538f5d - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 336
diff changeset
17 from bottle import route, request, response
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
19 import config
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
20 import log
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
21 import secure
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents: 488
diff changeset
22 import atomicfile
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
23
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
24 DATE_FORMAT = '%Y%m%d-%H.%M'
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
25 ZOOM_SCALE = 2.0
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
26
539
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
27 class TemplogBottle(bottle.Bottle):
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
28 def run(*args, **argm):
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
29 argm['server'] = 'gevent'
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
30 super(TemplogBottle, self).run(*args, **argm)
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
31
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
32 bottle.default_app.push(TemplogBottle())
539
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
33
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
34 secure.setup_csrf()
b78961d2e3a3 gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 538
diff changeset
35
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 @route('/update', method='post')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def update():
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
38 js_enc = request.forms.data
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
39 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
40
541
bde53484df3c sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 540
diff changeset
41 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest()
bde53484df3c sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 540
diff changeset
42 if h != mac:
391
5026780c2dc7 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 383
diff changeset
43 raise bottle.HTTPError(code = 403, output = "Bad key")
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
44
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
45 js = zlib.decompress(binascii.a2b_base64(js_enc))
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
46
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
47 params = json.loads(js)
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
48
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 409
diff changeset
49 log.parse(params)
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
50
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
51 return "OK"
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
542
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
53 def make_graph(length, end):
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
54 length_minutes = int(length)
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
55 end = datetime.strptime(end, DATE_FORMAT)
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
56 start = end - timedelta(minutes=length_minutes)
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
57
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
58 start_epoch = time.mktime(start.timetuple())
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
59 return log.graph_png(start_epoch, length_minutes * 60)
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
60
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
61 def encode_data(data, mimetype):
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
62 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip())
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
63
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 @route('/graph.png')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 def graph():
336
ba4c4df13487 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
66 response.set_header('Content-Type', 'image/png')
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
67 minutes, endstr = get_request_zoom()
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
68 return make_graph(minutes, endstr)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
70 @route('/set/update', method='post')
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
71 def set_update():
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
72 post_json = json.loads(request.forms.data)
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
73
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
74 csrf_blob = post_json['csrf_blob']
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
75
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
76 if not secure.check_csrf_blob(csrf_blob):
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
77 response.status = 403
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
78 return "Bad csrf"
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
79
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
80 ret = log.update_params(post_json['params'])
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
81 if not ret is True:
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
82 response.status = 403
492
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
83 return ret
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
84
23c6cf01d237 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
85 return "Good"
487
931408ce71d9 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 485
diff changeset
86
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
87 @route('/set')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
88 def set():
505
ad846b9bdd10 key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents: 501
diff changeset
89 allowed = ["false", "true"][secure.check_user_hash(config.ALLOWED_USERS)]
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 492
diff changeset
90 response.set_header('Cache-Control', 'no-cache')
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
91 return bottle.template('set',
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
92 inline_data = log.get_params(),
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
93 csrf_blob = secure.get_csrf_blob(),
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
94 allowed = allowed)
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
95
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
96 def get_request_zoom():
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
97 """ returns (length, end) tuple.
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
98 length is in minutes, end is a DATE_FORMAT string """
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
99 minutes = int(request.query.get('length', 26*60))
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
100
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
101 if 'end' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
102 end = datetime.strptime(request.query.end, DATE_FORMAT)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
103 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
104 end = datetime.now()
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
105
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
106 if 'zoom' in request.query:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
107 orig_start = end - timedelta(minutes=minutes)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
108 orig_end = end
548
b3dc81bee5b3 scaled zooming works
Matt Johnston <matt@ucc.asn.au>
parents: 542
diff changeset
109 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH
b3dc81bee5b3 scaled zooming works
Matt Johnston <matt@ucc.asn.au>
parents: 542
diff changeset
110 xpos = int(request.query.x) / scale
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
111 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
112
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
113 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM:
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
114 click_time = orig_start \
383
a032051475fe ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 375
diff changeset
115 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes)
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
116 minutes = int(minutes / ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
117
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
118 end = click_time + timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
119 else:
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
120 # zoom out
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
121 minutes = int(minutes*ZOOM_SCALE)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
122 end += timedelta(minutes=minutes/2)
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
123
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
124 if end > datetime.now():
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
125 end = datetime.now()
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
126
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
127 endstr = end.strftime(DATE_FORMAT)
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
128 return (minutes, endstr)
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
129
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
130 @route('/')
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
131 def top():
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
132 minutes, endstr = get_request_zoom()
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
133
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
134 request.query.replace('length', minutes)
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
135 request.query.replace('end', endstr)
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
136
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
137 urlparams = urllib.urlencode(request.query)
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
138 graphdata = encode_data(make_graph(minutes, endstr), 'image/png')
375
f22427bcfda8 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 367
diff changeset
139 return bottle.template('top', urlparams=urlparams,
549
0baa57b6d9ca working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 548
diff changeset
140 end = endstr,
542
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
141 length = minutes,
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
142 graphwidth = config.GRAPH_WIDTH,
30628aa50a10 use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 541
diff changeset
143 graphdata = graphdata)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144
409
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
145 @route('/debug')
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
146 def debuglog():
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
147 response.set_header('Content-Type', 'text/plain')
1854abee5a59 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 391
diff changeset
148 return log.tail_debug_log()
344
ea1779d27641 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 337
diff changeset
149
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
150 @route('/env')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
151 def env():
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
152 response.set_header('Content-Type', 'text/plain')
488
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
153 #return '\n'.join(traceback.format_stack())
4792e9910cde watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 487
diff changeset
154 return '\n'.join(("%s %s" % k) for k in request.environ.items())
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
155 #return str(request.environ)
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
156 #yield "\n"
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
157 #var_lookup = environ['mod_ssl.var_lookup']
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
158 #return var_lookup("SSL_SERVER_I_DN_O")
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
159
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
160 @route('/get_settings')
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
161 def get_settings():
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
162 response.set_header('Cache-Control', 'no-cache')
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
163 req_etag = request.headers.get('etag', None)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
164 if req_etag:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
165 # wait for it to change
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
166 if not log.fridge_settings.wait(req_etag, timeout=LONG_POLL_TIMEOUT):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
167 response.status = 304
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
168 return "Nothing happened"
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
169
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
170 response.set_header('Content-Type', 'application/json')
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
171 contents, epoch_tag = client_settings.get()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
172 return json.dumps({'params': contents, 'epoch_tag': epoch_tag})
552
a335760ad447 uwsgi bits
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
173
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
174 @bottle.get('/<filename:re:.*\.js>')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
175 def javascripts(filename):
485
d68af9e84485 working
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
176 response.set_header('Cache-Control', "public, max-age=1296000")
482
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
177 return bottle.static_file(filename, root='static')
9950ffa9a79b set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
178
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
179
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 def main():
552
a335760ad447 uwsgi bits
Matt Johnston <matt@ucc.asn.au>
parents: 549
diff changeset
181 """ for standalone testing """
367
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
182 #bottle.debug(True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
183 #bottle.run(reloader=True)
3db118498b97 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 362
diff changeset
184 bottle.run(server='cgi', reloader=True)
346
d6219df77c41 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
185 #bottle.run(port=9999, reloader=True)
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 if __name__ == '__main__':
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 main()
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189