2016-11-23 06:10:21 +08:00
|
|
|
const Metrics = require(`../nylas-metrics`)
|
2016-07-15 01:39:05 +08:00
|
|
|
Metrics.startCapturing('nylas-k2-dashboard')
|
2016-07-14 10:07:24 +08:00
|
|
|
|
2016-07-06 06:41:56 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2016-07-14 02:11:32 +08:00
|
|
|
const Inert = require('inert');
|
|
|
|
const Hapi = require('hapi');
|
|
|
|
const HapiWebSocket = require('hapi-plugin-websocket');
|
|
|
|
const {Logger} = require(`nylas-core`);
|
2016-06-24 06:52:45 +08:00
|
|
|
|
2016-07-14 10:07:24 +08:00
|
|
|
global.Metrics = Metrics
|
2016-07-09 08:13:30 +08:00
|
|
|
global.Logger = Logger.createLogger('nylas-k2-dashboard')
|
2016-06-24 06:52:45 +08:00
|
|
|
|
2016-07-14 10:07:24 +08:00
|
|
|
const onUnhandledError = (err) => {
|
|
|
|
global.Logger.fatal(err, 'Unhandled error')
|
|
|
|
global.Metrics.reportError(err)
|
|
|
|
}
|
|
|
|
process.on('uncaughtException', onUnhandledError)
|
|
|
|
process.on('unhandledRejection', onUnhandledError)
|
|
|
|
|
2016-06-24 06:52:45 +08:00
|
|
|
const server = new Hapi.Server();
|
2016-07-08 07:05:44 +08:00
|
|
|
server.connection({ port: process.env.PORT });
|
2016-07-06 06:41:56 +08:00
|
|
|
|
|
|
|
const attach = (directory) => {
|
|
|
|
const routesDir = path.join(__dirname, directory)
|
|
|
|
fs.readdirSync(routesDir).forEach((filename) => {
|
|
|
|
if (filename.endsWith('.js')) {
|
|
|
|
const routeFactory = require(path.join(routesDir, filename));
|
|
|
|
routeFactory(server);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-09 05:55:12 +08:00
|
|
|
server.register([HapiWebSocket, Inert], () => {
|
|
|
|
attach('./routes/')
|
2016-07-06 06:41:56 +08:00
|
|
|
|
2016-07-09 05:55:12 +08:00
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/ping',
|
|
|
|
config: {
|
|
|
|
auth: false,
|
|
|
|
},
|
|
|
|
handler: (request, reply) => {
|
2016-07-14 02:11:32 +08:00
|
|
|
global.Logger.info("---> Ping!")
|
2016-07-09 05:55:12 +08:00
|
|
|
reply("pong")
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/{param*}',
|
|
|
|
handler: {
|
|
|
|
directory: {
|
|
|
|
path: require('path').join(__dirname, 'public'),
|
2016-06-24 06:52:45 +08:00
|
|
|
},
|
2016-07-09 05:55:12 +08:00
|
|
|
},
|
|
|
|
});
|
2016-06-24 06:52:45 +08:00
|
|
|
|
2016-07-09 05:55:12 +08:00
|
|
|
server.start((startErr) => {
|
|
|
|
if (startErr) { throw startErr; }
|
2016-07-09 08:13:30 +08:00
|
|
|
global.Logger.info({uri: server.info.uri}, 'Dashboard running');
|
2016-06-24 06:52:45 +08:00
|
|
|
});
|
|
|
|
});
|