1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-15 15:50:06 +02:00
copilot/docker/api/server.js

107 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-06-23 19:24:25 +03:00
'use strict';
const Brule = require('brule');
const Good = require('good');
const Hapi = require('hapi');
const Toppsy = require('toppsy');
2017-07-31 23:10:04 +03:00
const Portal = require('portal-api');
2017-07-17 17:45:14 +03:00
const Path = require('path');
const Fs = require('fs');
const Url = require('url');
2017-06-23 19:24:25 +03:00
const server = new Hapi.Server();
2017-07-17 17:45:14 +03:00
server.connection({ port: 3000 });
2017-06-23 19:24:25 +03:00
const {
DOCKER_HOST,
DOCKER_CERT_PATH,
SDC_URL,
SDC_ACCOUNT,
SDC_KEY_ID
} = process.env;
2017-07-17 17:45:14 +03:00
const DOCKER_HOST_URL = DOCKER_HOST ? Url.parse(DOCKER_HOST) : {};
2017-06-23 19:24:25 +03:00
const portalOptions = {
data: {
db: {
host: process.env.RETHINK_HOST || 'localhost'
},
docker: {
2017-07-17 17:45:14 +03:00
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
},
triton: {
url: SDC_URL,
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID
2017-06-23 19:24:25 +03:00
}
},
watch: {
url: SDC_URL,
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID
}
};
const goodOptions = {
ops: {
interval: 1000
},
reporters: {
2017-07-17 17:45:14 +03:00
consoleReporter: [
{
module: 'good-squeeze',
name: 'Squeeze',
2017-07-31 23:10:04 +03:00
args: [{ response: '*', error: '*' }]
2017-07-17 17:45:14 +03:00
},
{
module: 'good-console'
},
'stdout'
]
2017-06-23 19:24:25 +03:00
}
};
2017-07-17 17:45:14 +03:00
server.register(
[
Brule,
{
register: Good,
options: goodOptions
},
{
register: Portal,
options: portalOptions
},
{
register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' }
}
],
err => {
2017-06-23 19:24:25 +03:00
handlerError(err);
2017-07-17 17:45:14 +03:00
server.start(err => {
2017-06-23 19:24:25 +03:00
handlerError(err);
console.log(`server started at http://localhost:${server.info.port}`);
});
}
);
2017-07-17 17:45:14 +03:00
function handlerError(error) {
2017-06-23 19:24:25 +03:00
if (error) {
console.error(error);
process.exit(1);
}
}