diff options
Diffstat (limited to 'lorry-controller-webapp')
-rwxr-xr-x | lorry-controller-webapp | 43 |
1 files changed, 34 insertions, 9 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): |