mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
chore(modernize): Modernize HealthCheckController
This commit is contained in:
parent
6a76d30516
commit
3903015cbb
@ -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;
|
||||
|
@ -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) => {
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user