2015-01-07 02:59:39 +03:00
|
|
|
'use strict';
|
2014-12-11 02:16:36 +03:00
|
|
|
var Hapi = require('hapi');
|
2015-01-07 02:59:39 +03:00
|
|
|
|
2015-01-14 19:02:40 +03:00
|
|
|
// Polyfill for ES6's Promises, replaces the global namespace Promise object.
|
|
|
|
require('es6-promise').polyfill();
|
|
|
|
|
2015-01-07 02:59:39 +03:00
|
|
|
var db = require('./lib/db');
|
2014-12-11 02:16:36 +03:00
|
|
|
var routes = require('./api/routes');
|
2015-01-07 02:59:39 +03:00
|
|
|
var settings = require('./settings');
|
2014-08-06 07:35:25 +04:00
|
|
|
|
|
|
|
|
2015-01-03 03:40:11 +03:00
|
|
|
var server = module.exports = new Hapi.Server();
|
2014-12-23 02:52:42 +03:00
|
|
|
server.connection({
|
|
|
|
host: settings.HOST,
|
|
|
|
port: settings.PORT,
|
|
|
|
routes: {
|
|
|
|
validate: {
|
|
|
|
options: {
|
|
|
|
abortEarly: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-01-03 03:40:11 +03:00
|
|
|
|
2015-01-17 03:54:34 +03:00
|
|
|
server.route(routes);
|
2014-08-06 07:35:25 +04:00
|
|
|
|
2015-01-03 03:40:11 +03:00
|
|
|
// Do not start the server when this script is required by another script.
|
|
|
|
if (!module.parent) {
|
|
|
|
server.start(function () {
|
|
|
|
console.log('Listening on %s', server.info.uri);
|
|
|
|
});
|
|
|
|
}
|
2014-12-17 05:20:29 +03:00
|
|
|
|
2015-01-07 02:59:39 +03:00
|
|
|
|
|
|
|
var pgPlugin = {
|
|
|
|
register: function (server, options, next) {
|
|
|
|
db.connect(options.connectionString);
|
|
|
|
|
|
|
|
server.on('stop', function () {
|
|
|
|
db.disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pgPlugin.register.attributes = {
|
|
|
|
name: 'pgPlugin',
|
|
|
|
version: '1.0.0'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-12-17 05:20:29 +03:00
|
|
|
server.register({
|
2015-01-07 02:59:39 +03:00
|
|
|
register: pgPlugin,
|
2014-12-17 05:20:29 +03:00
|
|
|
options: {
|
|
|
|
connectionString: settings.POSTGRES_URL
|
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
2015-01-07 02:59:39 +03:00
|
|
|
console.error('Failed to load "pgPlugin" plugin: %s', err);
|
2014-12-17 05:20:29 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|