From 5d93b1d4f9fcbbd0ffe002b79c72cf3fac3e4fcc Mon Sep 17 00:00:00 2001 From: Petr Sloup Date: Wed, 10 May 2017 08:57:51 +0200 Subject: [PATCH] Add healthcheck endpoint (close #140) --- docs/endpoints.rst | 7 +++++++ src/server.js | 9 +++++++++ test/metadata.js | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/docs/endpoints.rst b/docs/endpoints.rst index 3128085..91371bf 100644 --- a/docs/endpoints.rst +++ b/docs/endpoints.rst @@ -64,3 +64,10 @@ Array of all TileJSONs is at ``/index.json`` (``/rendered.json``; ``/data.json`` List of available fonts ======================= Array of names of the available fonts is at ``/fonts.json`` + +Health check +============ +Endpoint reporting health status is at ``/health`` and currently returns: + + * ``503`` Starting - for a short period before everything is initialized + * ``200`` OK - when the server is running diff --git a/src/server.js b/src/server.js index 1ebd59c..9657193 100644 --- a/src/server.js +++ b/src/server.js @@ -375,8 +375,17 @@ function start(opts) { return data; }); + var startupComplete = false; var startupPromise = Promise.all(startupPromises).then(function() { console.log('Startup complete'); + startupComplete = true; + }); + app.get('/health', function(req, res, next) { + if (startupComplete) { + return res.status(200).send('OK'); + } else { + return res.status(503).send('Starting'); + } }); var server = app.listen(process.env.PORT || opts.port, process.env.BIND || opts.bind, function() { diff --git a/test/metadata.js b/test/metadata.js index 647ee92..1365400 100644 --- a/test/metadata.js +++ b/test/metadata.js @@ -38,6 +38,14 @@ var testTileJSON = function(url) { }; describe('Metadata', function() { + describe('/health', function() { + it('returns 200', function(done) { + supertest(app) + .get('/health') + .expect(200, done); + }); + }); + testTileJSONArray('/index.json'); testTileJSONArray('/rendered.json'); testTileJSONArray('/data.json');