joyent-portal/packages/portal-api/server.js

114 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-04-28 00:59:03 +03:00
'use strict';
const Brule = require('brule');
const Good = require('good');
2017-04-28 00:59:03 +03:00
const Hapi = require('hapi');
const HapiSwagger = require('hapi-swagger');
2017-04-28 00:59:03 +03:00
const Inert = require('inert');
const Toppsy = require('toppsy');
2017-04-28 00:59:03 +03:00
const Vision = require('vision');
const Pack = require('./package');
const Portal = require('./lib');
const Path = require('path');
const Fs = require('fs');
const Url = require('url');
2017-04-28 00:59:03 +03:00
const server = new Hapi.Server();
server.connection({ port: 3000 });
2017-04-28 00:59:03 +03:00
2017-06-19 17:37:38 +03:00
const swaggerOptions = {
2017-04-28 00:59:03 +03:00
info: {
'title': 'Portal API Documentation',
'version': Pack.version
}
};
const {
DOCKER_HOST,
DOCKER_CERT_PATH,
SDC_URL,
SDC_ACCOUNT,
SDC_KEY_ID
} = process.env;
const DOCKER_HOST_URL = DOCKER_HOST ? Url.parse(DOCKER_HOST) : {};
2017-06-19 17:37:38 +03:00
const portalOptions = {
data: {
db: {
host: process.env.RETHINK_HOST || 'localhost'
},
docker: {
protocol: 'https',
host: DOCKER_HOST_URL.hostname,
port: DOCKER_HOST_URL.port,
ca: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem')) :
undefined,
cert: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem')) :
undefined,
key: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem')) :
undefined
2017-06-19 17:37:38 +03:00
}
},
watch: {
url: SDC_URL,
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID
2017-06-19 17:37:38 +03:00
}
};
const goodOptions = {
ops: {
2017-06-22 12:57:03 +03:00
interval: 1000
},
reporters: {
consoleReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*', error: '*' }]
}, {
module: 'good-console'
}, 'stdout']
}
};
2017-04-28 00:59:03 +03:00
server.register([
Brule,
2017-04-28 00:59:03 +03:00
Inert,
Vision,
{
register: Good,
options: goodOptions
},
2017-06-19 17:37:38 +03:00
{
register: Portal,
options: portalOptions
},
2017-04-28 00:59:03 +03:00
{
register: HapiSwagger,
2017-06-19 17:37:38 +03:00
options: swaggerOptions
},
{
register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' }
}
2017-06-21 01:02:40 +03:00
],
2017-04-28 00:59:03 +03:00
(err) => {
handlerError(err);
server.start((err) => {
handlerError(err);
console.log(`server started at http://localhost:${server.info.port}`);
});
}
);
function handlerError (error) {
if (error) {
console.error(error);
process.exit(1);
}
}