2016-06-24 06:52:45 +08:00
|
|
|
const Hapi = require('hapi');
|
|
|
|
const HapiWebSocket = require('hapi-plugin-websocket');
|
|
|
|
const Inert = require('inert');
|
2016-06-28 07:01:21 +08:00
|
|
|
const {DatabaseConnector, PubsubConnector, SchedulerUtils, NylasError} = require(`nylas-core`);
|
2016-06-24 06:52:45 +08:00
|
|
|
|
|
|
|
global.Promise = require('bluebird');
|
2016-06-28 07:01:21 +08:00
|
|
|
global.NylasError = NylasError;
|
2016-06-24 06:52:45 +08:00
|
|
|
|
|
|
|
const server = new Hapi.Server();
|
2016-06-24 09:17:04 +08:00
|
|
|
server.connection({ port: process.env.PORT / 1 + 1 || 5101 });
|
2016-06-24 06:52:45 +08:00
|
|
|
|
|
|
|
DatabaseConnector.forShared().then(({Account}) => {
|
|
|
|
server.register([HapiWebSocket, Inert], () => {
|
|
|
|
server.route({
|
|
|
|
method: "POST",
|
|
|
|
path: "/accounts",
|
|
|
|
config: {
|
|
|
|
plugins: {
|
|
|
|
websocket: {
|
|
|
|
only: true,
|
|
|
|
connect: (wss, ws) => {
|
2016-06-28 14:48:00 +08:00
|
|
|
Account.findAll().then((accounts) => {
|
|
|
|
accounts.forEach((acct) => {
|
2016-06-24 06:52:45 +08:00
|
|
|
ws.send(JSON.stringify({ cmd: "ACCOUNT", payload: acct }));
|
|
|
|
});
|
|
|
|
});
|
2016-07-01 03:33:08 +08:00
|
|
|
|
|
|
|
this.observable = PubsubConnector.observeAllAccounts().subscribe((accountId) => {
|
|
|
|
Account.find({where: {id: accountId}}).then((acct) => {
|
2016-06-24 06:52:45 +08:00
|
|
|
ws.send(JSON.stringify({ cmd: "ACCOUNT", payload: acct }));
|
|
|
|
});
|
|
|
|
});
|
2016-07-01 03:33:08 +08:00
|
|
|
|
|
|
|
this.pollInterval = setInterval(() => {
|
|
|
|
SchedulerUtils.listActiveAccounts().then((accountIds) => {
|
|
|
|
ws.send(JSON.stringify({ cmd: "ACTIVE", payload: accountIds}))
|
|
|
|
});
|
2016-06-24 06:52:45 +08:00
|
|
|
const assignments = {};
|
2016-07-01 03:33:08 +08:00
|
|
|
SchedulerUtils.forEachAccountList((identity, accountIds) => {
|
2016-06-24 06:52:45 +08:00
|
|
|
for (const accountId of accountIds) {
|
|
|
|
assignments[accountId] = identity;
|
|
|
|
}
|
|
|
|
}).then(() =>
|
|
|
|
ws.send(JSON.stringify({ cmd: "ASSIGNMENTS", payload: assignments}))
|
|
|
|
)
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
disconnect: () => {
|
2016-07-01 03:33:08 +08:00
|
|
|
clearInterval(this.pollInterval);
|
|
|
|
this.observable.dispose();
|
2016-06-24 06:52:45 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
handler: (request, reply) => {
|
|
|
|
if (request.payload.cmd === "PING") {
|
|
|
|
reply(JSON.stringify({ result: "PONG" }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/{param*}',
|
|
|
|
handler: {
|
|
|
|
directory: {
|
2016-06-24 09:17:04 +08:00
|
|
|
path: require('path').join(__dirname, 'public'),
|
2016-06-24 06:52:45 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
server.start((startErr) => {
|
|
|
|
if (startErr) { throw startErr; }
|
2016-06-24 09:17:04 +08:00
|
|
|
console.log('Dashboard running at:', server.info.uri);
|
2016-06-24 06:52:45 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|