diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-01-30 15:38:17 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-01-30 15:38:17 +0000 |
commit | 8083085044a2cdae0e9d30e0b37ca449f65f4692 (patch) | |
tree | a7ff25d835e530db9816bf30994a6fbb37f65012 | |
parent | d12048b792fae19cee1fa8e8b652e1a2c0ac735b (diff) | |
download | lorry-controller-8083085044a2cdae0e9d30e0b37ca449f65f4692.tar.gz |
Write out a static HTML status page
-rwxr-xr-x | lorry-controller-webapp | 43 | ||||
-rw-r--r-- | yarns.webapp/020-status.yarn | 1 | ||||
-rw-r--r-- | yarns.webapp/900-implementations.yarn | 21 |
3 files changed, 61 insertions, 4 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 6acbac0..3963853 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -26,6 +26,27 @@ import cliapp from flup.server.fcgi import WSGIServer +STATUS_HTML_TEMPLATE = ''' +<!DOCTYPE HTML> +<html> + <head> + <title>Lorry Controller status</title> + </head> + <body> + <blockquote>{quote}</blockquote> + + <h1>Status of Lorry Controller</h1> + + <p>Running queue: {running-queue}.</p> + + <hr> + + <p>Updated: {timestamp}</p> + </body> +</html> +''' + + class StateDB(object): '''A wrapper around raw Sqlite for STATEDB.''' @@ -75,7 +96,8 @@ class LorryControllerRoute(object): ''' - def __init__(self, statedb): + def __init__(self, app_settings, statedb): + self.app_settings = app_settings self.statedb = statedb def run(self, **kwargs): @@ -94,10 +116,17 @@ class Status(LorryControllerRoute): "I'm giving her all she's got, Captain!", ] import random - return { - 'quote': '%s\n' % random.choice(quotes), + status = { + 'quote': '%s' % random.choice(quotes), 'running-queue': self.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)) class StartQueue(LorryControllerRoute): @@ -128,6 +157,12 @@ class WEBAPP(cliapp.Application): 'use FILE as the state database', metavar='FILE') + self.settings.string( + ['status-html'], + 'write a static HTML page to FILE to describe overall status', + metavar='FILE', + default='/dev/null') + self.settings.boolean( ['wsgi'], 'run in wsgi mode (default is debug mode, for development)') @@ -172,7 +207,7 @@ class WEBAPP(cliapp.Application): webapp = bottle.Bottle() for route_class in self.find_routes(): - route = route_class(statedb) + route = route_class(self.settings, statedb) webapp.route( path=route.path, method=route.http_method, diff --git a/yarns.webapp/020-status.yarn b/yarns.webapp/020-status.yarn index cf0c634..4f0ce02 100644 --- a/yarns.webapp/020-status.yarn +++ b/yarns.webapp/020-status.yarn @@ -10,5 +10,6 @@ it has no Lorry or Trove specs. WHEN admin makes request GET /1.0/status THEN response is JSON AND response has running-queue set to false + AND static status page got updated FINALLY WEBAPP terminates diff --git a/yarns.webapp/900-implementations.yarn b/yarns.webapp/900-implementations.yarn index fd93123..4a0c22b 100644 --- a/yarns.webapp/900-implementations.yarn +++ b/yarns.webapp/900-implementations.yarn @@ -36,6 +36,7 @@ be running until it crashes or is explicitly killed. -b -p "$DATADIR/webapp.pid" -m --verbose \ -- \ --statedb "$DATADIR/webapp.db" \ + --status-html "$DATADIR/lc-status.html" \ --log-level debug \ --log "$DATADIR/webapp.log" \ --debug-host 127.0.0.1 \ @@ -83,6 +84,10 @@ scenario doesn't need ot check that separately. rm -f "$DATADIR/response.headers" rm -f "$DATADIR/response.body" port=$(cat "$DATADIR/webapp.port") + + # The timestamp is needed by "THEN static status page got updated" + touch "$DATADIR/request.timestamp" + curl \ -D "$DATADIR/response.headers" \ -o "$DATADIR/response.body" \ @@ -117,3 +122,19 @@ value is expresssed as a JSON value in the step. key=key, value=value, expected=expected)) sys.exit(1) ' < "$DATADIR/response.body" + + +Status web page +--------------- + +WEBAPP is expected to update a static HTML pages whenever the +`/1.0/status` request is made. We configure WEBAPP to write it to +`$DATADIR/lc-status.html`. We don't test the contents of the page, but +we do test that it gets updated. We test for the updates by comparing +the modification time of the file with the time of the request. We +know the time of the request thanks to the "WHEN admin makes a +request" step updating the modification time of a file for this +purpose. + + IMPLEMENTS THEN static status page got updated + test "$DATADIR/lc-status.html" -nt "$DATADIR/request.timestamp" |