2016-07-12 07:33:56 +08:00
|
|
|
// require('newrelic');
|
2016-07-12 01:56:08 +08:00
|
|
|
|
2016-06-19 18:02:32 +08:00
|
|
|
const Hapi = require('hapi');
|
|
|
|
const HapiSwagger = require('hapi-swagger');
|
2016-06-26 16:44:39 +08:00
|
|
|
const HapiBoom = require('hapi-boom-decorators')
|
2016-06-19 18:02:32 +08:00
|
|
|
const HapiBasicAuth = require('hapi-auth-basic');
|
|
|
|
const Inert = require('inert');
|
|
|
|
const Vision = require('vision');
|
|
|
|
const Package = require('./package');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2016-07-09 08:13:30 +08:00
|
|
|
const {DatabaseConnector, SchedulerUtils, Logger} = require(`nylas-core`);
|
2016-06-19 18:02:32 +08:00
|
|
|
|
2016-07-09 08:13:30 +08:00
|
|
|
global.Logger = Logger.createLogger('nylas-k2-api')
|
2016-06-24 06:52:45 +08:00
|
|
|
|
2016-07-12 08:42:49 +08:00
|
|
|
const onUnhandledError = (err) => global.Logger.fatal(err, 'Unhandled error')
|
|
|
|
process.on('uncaughtException', onUnhandledError)
|
|
|
|
process.on('unhandledRejection', onUnhandledError)
|
|
|
|
|
2016-06-29 04:56:57 +08:00
|
|
|
const server = new Hapi.Server({
|
|
|
|
connections: {
|
|
|
|
router: {
|
|
|
|
stripTrailingSlash: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-07-08 07:05:44 +08:00
|
|
|
server.connection({ port: process.env.PORT });
|
2016-06-19 18:02:32 +08:00
|
|
|
|
2016-06-26 16:44:39 +08:00
|
|
|
const plugins = [Inert, Vision, HapiBasicAuth, HapiBoom, {
|
2016-06-19 18:02:32 +08:00
|
|
|
register: HapiSwagger,
|
|
|
|
options: {
|
|
|
|
info: {
|
|
|
|
title: 'Nylas API Documentation',
|
|
|
|
version: Package.version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}];
|
|
|
|
|
|
|
|
let sharedDb = null;
|
|
|
|
|
|
|
|
const validate = (request, username, password, callback) => {
|
2016-07-07 07:22:32 +08:00
|
|
|
let getSharedDb = null;
|
|
|
|
if (sharedDb) {
|
|
|
|
getSharedDb = Promise.resolve(sharedDb)
|
|
|
|
} else {
|
|
|
|
getSharedDb = DatabaseConnector.forShared()
|
|
|
|
}
|
|
|
|
|
|
|
|
getSharedDb.then((db) => {
|
|
|
|
sharedDb = db;
|
|
|
|
const {AccountToken} = db;
|
|
|
|
|
|
|
|
AccountToken.find({
|
|
|
|
where: {
|
|
|
|
value: username,
|
|
|
|
},
|
|
|
|
}).then((token) => {
|
|
|
|
if (!token) {
|
2016-07-01 03:32:19 +08:00
|
|
|
callback(null, false, {});
|
2016-07-07 07:22:32 +08:00
|
|
|
return
|
2016-06-30 07:10:39 +08:00
|
|
|
}
|
2016-07-07 07:22:32 +08:00
|
|
|
token.getAccount().then((account) => {
|
|
|
|
if (!account) {
|
|
|
|
callback(null, false, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SchedulerUtils.markAccountIsActive(account.id)
|
|
|
|
callback(null, true, account);
|
|
|
|
});
|
2016-06-19 18:02:32 +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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
server.register(plugins, (err) => {
|
|
|
|
if (err) { throw err; }
|
|
|
|
|
|
|
|
attach('./routes/')
|
|
|
|
attach('./decorators/')
|
|
|
|
|
|
|
|
server.auth.strategy('api-consumer', 'basic', { validateFunc: validate });
|
|
|
|
server.auth.default('api-consumer');
|
|
|
|
|
|
|
|
server.start((startErr) => {
|
|
|
|
if (startErr) { throw startErr; }
|
2016-07-09 08:13:30 +08:00
|
|
|
global.Logger.info({url: server.info.uri}, 'API running');
|
2016-06-19 18:02:32 +08:00
|
|
|
});
|
|
|
|
});
|