diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-04 14:04:29 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-04 14:04:29 +0000 |
commit | d8638dd91c6d0a0d5d3934bea8e9df95d02248cf (patch) | |
tree | f0b5eaa70f74be2ed7da46880df0550294aa8f5c | |
parent | 1f20790defb64755c1e87e3c0e75f1edf4313eb7 (diff) | |
download | lorry-controller-d8638dd91c6d0a0d5d3934bea8e9df95d02248cf.tar.gz |
Add /1.0/status-html
-rwxr-xr-x | lorry-controller-webapp | 43 | ||||
-rw-r--r-- | yarns.webapp/020-status.yarn | 11 | ||||
-rw-r--r-- | yarns.webapp/900-implementations.yarn | 6 |
3 files changed, 47 insertions, 13 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 7d06897..913275d 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -109,12 +109,11 @@ class LorryControllerRoute(object): raise NotImplementedError() -class Status(LorryControllerRoute): +class StatusRenderer(object): - http_method = 'GET' - path = '/1.0/status' + '''Helper class for rendering service status as JSON/HTML''' - def run(self, **kwargs): + def render_as_dict(self, statedb): quotes = [ "Never get drunk unless you're willing to pay for it - " "the next day.", @@ -123,15 +122,41 @@ class Status(LorryControllerRoute): import random status = { 'quote': '%s' % random.choice(quotes), - 'running-queue': self.statedb.get_running_queue(), + 'running-queue': statedb.get_running_queue(), 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'), } - self.write_static_status_page(status) return status - def write_static_status_page(self, status): - with open(self.app_settings['status-html'], 'w') as f: - f.write(STATUS_HTML_TEMPLATE.format(**status)) + def render_as_html(self, statedb): + status = self.render_as_dict(statedb) + return STATUS_HTML_TEMPLATE.format(**status) + + def write_as_html(self, statedb, filename): + html = self.render_as_html(statedb) + with open(filename, 'w') as f: + f.write(html) + + +class Status(LorryControllerRoute): + + http_method = 'GET' + path = '/1.0/status' + + def run(self, **kwargs): + renderer = StatusRenderer() + renderer.write_as_html(self.statedb, self.app_settings['status-html']) + return renderer.render_as_dict(self.statedb) + + +class StatusHTML(LorryControllerRoute): + + http_method = 'GET' + path = '/1.0/status-html' + + def run(self, **kwargs): + renderer = StatusRenderer() + renderer.write_as_html(self.statedb, self.app_settings['status-html']) + return renderer.render_as_html(self.statedb) class StartQueue(LorryControllerRoute): diff --git a/yarns.webapp/020-status.yarn b/yarns.webapp/020-status.yarn index 4f0ce02..ae5a779 100644 --- a/yarns.webapp/020-status.yarn +++ b/yarns.webapp/020-status.yarn @@ -8,8 +8,17 @@ it has no Lorry or Trove specs. SCENARIO WEBAPP is idle when it starts GIVEN a running WEBAPP WHEN admin makes request GET /1.0/status - THEN response is JSON + THEN response is application/json AND response has running-queue set to false AND static status page got updated FINALLY WEBAPP terminates +As an alternative, we can request the HTML rendering of the status +directly with `/1.0/status-html`. + + SCENARIO WEBAPP provide HTML status directly + GIVEN a running WEBAPP + WHEN admin makes request GET /1.0/status-html + THEN response is text/html + AND static status page got updated + FINALLY WEBAPP terminates diff --git a/yarns.webapp/900-implementations.yarn b/yarns.webapp/900-implementations.yarn index 2d24c44..0e3aa9e 100644 --- a/yarns.webapp/900-implementations.yarn +++ b/yarns.webapp/900-implementations.yarn @@ -96,11 +96,11 @@ scenario doesn't need ot check that separately. cat "$DATADIR/response.body" head -n1 "$DATADIR/response.headers" | grep '^HTTP/1\.[01] 200 ' -Check the Content-Type of the response is JSON. +Check the Content-Type of the response has the desired type. - IMPLEMENTS THEN response is JSON + IMPLEMENTS THEN response is (\S+) cat "$DATADIR/response.headers" - grep -i '^Content-Type: application/json' "$DATADIR/response.headers" + grep -i "^Content-Type: $MATCH_1" "$DATADIR/response.headers" A JSON response can then be queried further. The JSON is expected to be a dict, so that values are accessed by name from the dict. The |