annotate web/templog.py @ 287:97d99eb42d27 py3client

temp == 0 is a problem
author Matt Johnston <matt@ucc.asn.au>
date Wed, 26 Jun 2019 22:52:03 +0800
parents 0a1c02160e66
children 788d713f6b87 6bacd8ca9f8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
3 import binascii
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
4 import json
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
5 import hmac
29
048143905092 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 28
diff changeset
6 import zlib
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
7 from datetime import datetime, timedelta
31
5e75e08d20ac - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
8 import time
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
9 import urllib
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
10 import sys
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
11 import os
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
12 import traceback
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
13 import fcntl
240
c90190a380c6 sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 238
diff changeset
14 import hashlib
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
15
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 import bottle
31
5e75e08d20ac - Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
17 from bottle import route, request, response
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
19 import config
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
20 import log
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
21 import secure
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents: 189
diff changeset
22 import atomicfile
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
23
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
24 DATE_FORMAT = '%Y%m%d-%H.%M'
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
25 ZOOM_SCALE = 2.0
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
26
237
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
27 class TemplogBottle(bottle.Bottle):
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
28 def run(*args, **argm):
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
29 argm['server'] = 'gevent'
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
30 super(TemplogBottle, self).run(*args, **argm)
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
31
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
32 bottle.default_app.push(TemplogBottle())
237
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
33
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
34 secure.setup_csrf()
abc837a7e60f gevent seems to work
Matt Johnston <matt@ucc.asn.au>
parents: 236
diff changeset
35
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 @route('/update', method='post')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def update():
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
38 js_enc = request.forms.data
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
39 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
40
240
c90190a380c6 sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 238
diff changeset
41 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest()
c90190a380c6 sha256 hmac now
Matt Johnston <matt@ucc.asn.au>
parents: 238
diff changeset
42 if h != mac:
85
3cd1ca6d0489 - Fix HTTPError
Matt Johnston <matt@ucc.asn.au>
parents: 77
diff changeset
43 raise bottle.HTTPError(code = 403, output = "Bad key")
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
44
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
45 js = zlib.decompress(binascii.a2b_base64(js_enc))
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
46
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
47 params = json.loads(js)
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
48
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 103
diff changeset
49 log.parse(params)
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
50
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
51 return "OK"
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
244
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
53 def make_graph(length, end):
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
54 length_minutes = int(length)
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
55 end = datetime.strptime(end, DATE_FORMAT)
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
56 start = end - timedelta(minutes=length_minutes)
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
57
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
58 start_epoch = time.mktime(start.timetuple())
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
59 return log.graph_png(start_epoch, length_minutes * 60)
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
60
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
61 def encode_data(data, mimetype):
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
62 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip())
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
63
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 @route('/graph.png')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 def graph():
30
13fcf497f8b7 parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
66 response.set_header('Content-Type', 'image/png')
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
67 minutes, endstr = get_request_zoom()
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
68 return make_graph(minutes, endstr)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
70 @route('/set/update', method='post')
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
71 def set_update():
275
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
72 if not secure.check_user_hash(config.ALLOWED_USERS):
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
73 # the "Save" button should be disabled if the cert wasn't
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
74 # good
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
75 response.status = 403
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
76 return "No cert, dodginess"
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
77
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
78 post_json = json.loads(request.forms.data)
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
79
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
80 csrf_blob = post_json['csrf_blob']
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
81
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
82 if not secure.check_csrf_blob(csrf_blob):
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
83 response.status = 403
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
84 return "Bad csrf"
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
85
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
86 ret = log.update_params(post_json['params'])
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
87 if not ret is True:
275
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
88 response.status = 409 # Conflict
194
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
89 return ret
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
90
4fa8cbf31065 working kinda
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
91 return "Good"
188
ae5efca89001 working OK
Matt Johnston <matt@ucc.asn.au>
parents: 185
diff changeset
92
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
93 @route('/set')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
94 def set():
211
59379b2bd056 key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents: 202
diff changeset
95 allowed = ["false", "true"][secure.check_user_hash(config.ALLOWED_USERS)]
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 194
diff changeset
96 response.set_header('Cache-Control', 'no-cache')
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
97 return bottle.template('set',
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
98 inline_data = log.get_params(),
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
99 csrf_blob = secure.get_csrf_blob(),
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
100 allowed = allowed)
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
101
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
102 def get_request_zoom():
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
103 """ returns (length, end) tuple.
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
104 length is in minutes, end is a DATE_FORMAT string """
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
105 minutes = int(request.query.get('length', 26*60))
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
106
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
107 if 'end' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
108 end = datetime.strptime(request.query.end, DATE_FORMAT)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
109 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
110 end = datetime.now()
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
111
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
112 if 'zoom' in request.query:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
113 orig_start = end - timedelta(minutes=minutes)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
114 orig_end = end
249
c490de0cf17e scaled zooming works
Matt Johnston <matt@ucc.asn.au>
parents: 244
diff changeset
115 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH
c490de0cf17e scaled zooming works
Matt Johnston <matt@ucc.asn.au>
parents: 244
diff changeset
116 xpos = int(request.query.x) / scale
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
117 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
118
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
119 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM:
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
120 click_time = orig_start \
77
959e88c0bdfa ui tweaks
Matt Johnston <matt@ucc.asn.au>
parents: 69
diff changeset
121 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes)
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
122 minutes = int(minutes / ZOOM_SCALE)
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
123
264
205809a8872f integer division
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
124 end = click_time + timedelta(minutes=minutes//2)
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
125 else:
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
126 # zoom out
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
127 minutes = int(minutes*ZOOM_SCALE)
264
205809a8872f integer division
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
128 end += timedelta(minutes=minutes//2)
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
129
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
130 if end > datetime.now():
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
131 end = datetime.now()
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
132
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
133 endstr = end.strftime(DATE_FORMAT)
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
134 return (minutes, endstr)
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
135
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
136 @route('/')
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
137 def top():
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
138 minutes, endstr = get_request_zoom()
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
139
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
140 request.query.replace('length', minutes)
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
141 request.query.replace('end', endstr)
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
142
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
143 urlparams = urllib.urlencode(request.query)
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
144 graphdata = encode_data(make_graph(minutes, endstr), 'image/png')
69
a8ff20f15734 a bunch of web fiddli
Matt Johnston <matt@ucc.asn.au>
parents: 61
diff changeset
145 return bottle.template('top', urlparams=urlparams,
250
141948a400a6 working zoom
Matt Johnston <matt@ucc.asn.au>
parents: 249
diff changeset
146 end = endstr,
244
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
147 length = minutes,
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
148 graphwidth = config.GRAPH_WIDTH,
2071d939e4ff use base64 data, faster
Matt Johnston <matt@ucc.asn.au>
parents: 240
diff changeset
149 graphdata = graphdata)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
103
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
151 @route('/debug')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
152 def debuglog():
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
153 response.set_header('Content-Type', 'text/plain')
ef59da811986 add debug url
Matt Johnston <matt@ucc.asn.au>
parents: 85
diff changeset
154 return log.tail_debug_log()
37
8da0fdadc8d7 - Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents: 31
diff changeset
155
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
156 @route('/env')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
157 def env():
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
158 response.set_header('Content-Type', 'text/plain')
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
159 #return '\n'.join(traceback.format_stack())
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents: 188
diff changeset
160 return '\n'.join(("%s %s" % k) for k in request.environ.items())
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
161 #return str(request.environ)
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
162 #yield "\n"
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
163 #var_lookup = environ['mod_ssl.var_lookup']
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
164 #return var_lookup("SSL_SERVER_I_DN_O")
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
165
275
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
166 @route('/h')
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
167 def headers():
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
168 response.set_header('Content-Type', 'text/plain')
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
169 return '\n'.join("%s: %s" % x for x in request.headers.items())
9be8464e4295 Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
170
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
171 @route('/get_settings')
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
172 def get_settings():
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
173 response.set_header('Cache-Control', 'no-cache')
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
174 req_etag = request.headers.get('etag', None)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
175 if req_etag:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
176 # wait for it to change
258
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
177 # XXX this is meant to return True if it has been woken up
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
178 # but it isn't working. Instead compare epochtag below.
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
179 log.fridge_settings.wait(req_etag, timeout=config.LONG_POLL_TIMEOUT)
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
180
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
181 contents, epoch_tag = log.fridge_settings.get()
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
182 if epoch_tag == req_etag:
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
183 response.status = 304
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 255
diff changeset
184 return "Nothing happened"
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
185
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
186 response.set_header('Content-Type', 'application/json')
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
187 return json.dumps({'params': contents, 'epoch_tag': epoch_tag})
252
3e6f82347eab uwsgi bits
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
188
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
189 @bottle.get('/<filename:re:.*\.js>')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
190 def javascripts(filename):
185
adbf70d1449f working
Matt Johnston <matt@ucc.asn.au>
parents: 182
diff changeset
191 response.set_header('Cache-Control', "public, max-age=1296000")
182
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
192 return bottle.static_file(filename, root='static')
e731c0d30b09 set nearly working
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
193
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
194
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 def main():
252
3e6f82347eab uwsgi bits
Matt Johnston <matt@ucc.asn.au>
parents: 250
diff changeset
196 """ for standalone testing """
61
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
197 #bottle.debug(True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
198 #bottle.run(reloader=True)
62112fc2af21 sort out voltage logging
Matt Johnston <matt@ucc.asn.au>
parents: 56
diff changeset
199 bottle.run(server='cgi', reloader=True)
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 37
diff changeset
200 #bottle.run(port=9999, reloader=True)
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 if __name__ == '__main__':
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 main()
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204