From 3903015cbb02e67e51a013ff6cf10c552943574c Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 24 Nov 2018 12:43:46 +0100 Subject: [PATCH] chore(modernize): Modernize HealthCheckController --- lib/routes/health-check.js | 43 ++++++++++++++++++++++---------------- lib/routes/index.js | 4 ++-- lib/server-impl.js | 6 ++++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/routes/health-check.js b/lib/routes/health-check.js index 2d95c168dd..241e09903e 100644 --- a/lib/routes/health-check.js +++ b/lib/routes/health-check.js @@ -1,24 +1,31 @@ 'use strict'; -const logger = require('../logger')('health-check.js'); const { Router } = require('express'); +const logger = require('../logger')('health-check.js'); -exports.router = function(config) { - const router = Router(); +class HealthCheckController { + constructor(config) { + const app = Router(); - router.get('/', (req, res) => { - config.stores.db - .select(1) - .from('features') - .then(() => res.json({ health: 'GOOD' })) - .catch(err => { - logger.error( - 'Could not select from features, error was: ', - err - ); - res.status(500).json({ health: 'BAD' }); - }); - }); + this.app = app; + this.db = config.stores.db; - return router; -}; + app.get('/', (req, res) => this.index(req, res)); + } + + async index(req, res) { + try { + await this.db.select(1).from('features'); + res.json({ health: 'GOOD' }); + } catch (e) { + logger.error('Could not select from features, error was: ', e); + res.status(500).json({ health: 'BAD' }); + } + } + + router() { + return this.app; + } +} + +module.exports = HealthCheckController; diff --git a/lib/routes/index.js b/lib/routes/index.js index b81db01acf..b4c237d555 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -10,13 +10,13 @@ const adminApi = require('./admin-api'); const clientApi = require('./client-api'); const clientFeatures = require('./client-api/feature.js'); -const health = require('./health-check'); +const HealthCheckController = require('./health-check'); const backstage = require('./backstage.js'); exports.router = function(config) { const router = Router(); - router.use('/health', health.router(config)); + router.use('/health', new HealthCheckController(config).router()); router.use('/internal-backstage', backstage.router(config)); router.get('/api', (req, res) => { diff --git a/lib/server-impl.js b/lib/server-impl.js index f21b389859..1f74d5d5fa 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -2,6 +2,7 @@ const { EventEmitter } = require('events'); +const logFactory = require('./logger'); const logger = require('./logger')('server-impl.js'); const migrator = require('../migrator'); const getApp = require('./app'); @@ -21,8 +22,9 @@ function createApp(options) { { stores, eventBus, + logFactory, }, - options + options, ); const app = getApp(config); @@ -34,7 +36,7 @@ function createApp(options) { ); const server = app.listen({ port: options.port, host: options.host }, () => - logger.info(`Unleash started on port ${server.address().port}`) + logger.info(`Unleash started on port ${server.address().port}`), ); return new Promise((resolve, reject) => {