@@ -5,6 +5,10 @@ const config = require('../init/configuration').full
55const paths = require ( '../init/routing/paths' )
66const db = require ( 'kth-node-mongo' )
77
8+ const Promise = require ( 'bluebird' )
9+ const registry = require ( 'component-registry' ) . globalRegistry
10+ const { IHealthCheck } = require ( 'kth-node-monitor' ) . interfaces
11+
812/**
913 * System controller for functions such as about and monitor.
1014 * Avoid making changes here in sub-projects.
@@ -45,8 +49,34 @@ function getAbout (req, res) {
4549 * Monitor page
4650 */
4751function getMonitor ( req , res ) {
48- res . type ( 'text' ) . render ( 'system/monitor' , {
49- dbStatus : db . isOk ( ) ? 'OK' : 'ERROR'
52+ // Check MongoDB
53+ const mongodbHealthUtil = registry . getUtility ( IHealthCheck , 'kth-node-mongodb' )
54+ const subSystems = [ mongodbHealthUtil . status ( db , { required : true } ) ]
55+
56+ // If we need local system checks, such as memory or disk, we would add it here.
57+ // Make sure it returns a promise which resolves with an object containing:
58+ // {statusCode: ###, message: '...'}
59+ // The property statusCode should be standard HTTP status codes.
60+ const localSystems = Promise . resolve ( { statusCode : 200 , message : 'OK' } )
61+
62+ /* -- You will normally not change anything below this line -- */
63+
64+ // Determine system health based on the results of the checks above. Expects
65+ // arrays of promises as input. This returns a promise
66+ const systemHealthUtil = registry . getUtility ( IHealthCheck , 'kth-node-system-check' )
67+ const systemStatus = systemHealthUtil . status ( localSystems , subSystems )
68+
69+ systemStatus . then ( ( status ) => {
70+ // Return the result either as JSON or text
71+ if ( req . headers [ 'accept' ] === 'application/json' ) {
72+ let outp = systemHealthUtil . renderJSON ( status )
73+ res . status ( status . statusCode ) . json ( outp )
74+ } else {
75+ let outp = systemHealthUtil . renderText ( status )
76+ res . type ( 'text' ) . status ( status . statusCode ) . send ( outp )
77+ }
78+ } ) . catch ( ( err ) => {
79+ res . type ( 'text' ) . status ( 500 ) . send ( err )
5080 } )
5181}
5282
0 commit comments