Implement primitive StatusHandler.

This version simply returns a JSON object based on the values passed
into the initialize method.
This commit is contained in:
Dave Shawley 2017-02-19 21:11:06 -05:00
parent 718bf1b9ae
commit 0f63e9675b
8 changed files with 118 additions and 0 deletions

5
docs/api.rst Normal file
View file

@ -0,0 +1,5 @@
===
API
===
.. autoclass:: sprockets_status.handlers.StatusHandler
:members:

View file

@ -10,6 +10,7 @@ extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx']
master_doc = 'index'
source_suffix = '.rst'
html_sidebars = {'**': ['about.html', 'navigation.html']}
html_static_path = ['.']
intersphinx_mapping = {
'python': ('http://docs.python.org/3/', None),

View file

@ -1,3 +1,6 @@
h1.logo {
font-size: 1em;
}
.field-name {
-webkit-hyphens: none;
hyphens: none;

View file

@ -4,6 +4,7 @@ Release History
`Next Release`_
---------------
- Added :class:`sprockets_status.handlers.StatusHandler`
.. _Next Release: https://github.com/sprockets/sprockets-status/compare/0.0.0...HEAD

View file

@ -3,4 +3,5 @@
.. toctree::
:hidden:
api
history

24
examples/app.py Normal file
View file

@ -0,0 +1,24 @@
from tornado import ioloop, web
import sprockets_status.handlers
name = 'my-application'
version = '1.2.3'
def make_app(**settings):
return web.Application([
web.url('/status', sprockets_status.handlers.StatusHandler,
{'name': name, 'version': version}),
], **settings)
if __name__ == '__main__':
app = make_app()
iol = ioloop.IOLoop.current()
try:
app.listen(8888)
iol.start()
except KeyboardInterrupt:
iol.stop()

View file

@ -0,0 +1,48 @@
import json
from tornado import web
class StatusHandler(web.RequestHandler):
"""
Simple handler that returns application status information.
"""
def __init__(self, *args, **kwargs):
self.name = 'UNKNOWN'
self.version = '0.0.0'
self.status = 'ok'
super(StatusHandler, self).__init__(*args, **kwargs)
def initialize(self, name, version):
"""
Sets the static information.
:param str name: name of the application
:param str version: application version number
"""
self.name = name
self.version = version
def get(self):
"""
Returns a JSON object containing the application status.
**Sample Response**
.. code-block:: json
{
"name": "application-name",
"version": "1.2.3",
"status": "ok"
}
"""
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(json.dumps({'name': self.name,
'version': self.version,
'status': self.status}).encode('utf-8'))
self.finish()

View file

@ -0,0 +1,35 @@
import json
from tornado import testing
import examples.app
class SimpleStatusTests(testing.AsyncHTTPTestCase):
def get_app(self):
return examples.app.make_app(debug=True)
def test_that_content_type_is_set(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
self.assertTrue(
response.headers['Content-Type'].startswith('application/json'))
def test_that_application_name_is_included(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['name'], examples.app.name)
def test_that_application_version_is_included(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['version'], examples.app.version)
def test_that_application_status_is_included(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['status'], 'ok')