1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-01 07:30:07 +02:00
copilot/docker/api/server.js

131 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-06-23 19:24:25 +03:00
'use strict';
const Brule = require('brule');
const Fs = require('fs');
2017-06-23 19:24:25 +03:00
const Good = require('good');
const Hapi = require('hapi');
2017-07-17 17:45:14 +03:00
const Path = require('path');
const Piloted = require('piloted');
const Portal = require('portal-api');
const Toppsy = require('toppsy');
2017-07-17 17:45:14 +03:00
const Url = require('url');
2017-06-23 19:24:25 +03:00
2017-07-17 17:45:14 +03:00
let started = false;
let timeoutId;
const loadConfig = function () {
const docker = Piloted.service('docker-compose-api');
const rethink = Piloted.service('rethinkdb');
2017-06-23 19:24:25 +03:00
if (docker && rethink && !started) {
started = true;
startServer({ docker, rethink });
} else if (!started && !timeoutId) {
timeoutId = setTimeout(() => {
timeoutId = null;
Piloted.refresh();
}, 1000);
}
};
2017-07-17 17:45:14 +03:00
Piloted.on('refresh', () => {
loadConfig();
});
const startServer = function ({ docker, rethink }) {
const port = process.env.PORT ? Number.parseInt(process.env.PORT, 10) : 3000;
const server = new Hapi.Server();
server.connection({ port });
const portalOptions = {
data: {
db: {
host: rethink.address
},
docker: {
protocol: 'https',
host: docker.address,
port: docker.port,
ca: process.env.DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'ca.pem'))
: undefined,
cert: process.env.DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'cert.pem'))
: undefined,
key: process.env.DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'key.pem'))
: undefined
},
triton: {
url: process.env.SDC_URL,
account: process.env.SDC_ACCOUNT,
keyId: process.env.SDC_KEY_ID
}
2017-06-23 19:24:25 +03:00
},
watch: {
url: process.env.SDC_URL,
account: process.env.SDC_ACCOUNT,
keyId: process.env.SDC_KEY_ID
}
};
const goodOptions = {
ops: {
interval: 1000
2017-07-17 17:45:14 +03:00
},
reporters: {
consoleReporter: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ response: '*', error: '*' }]
},
{
module: 'good-console'
},
'stdout'
]
2017-06-23 19:24:25 +03:00
}
};
2017-06-23 19:24:25 +03:00
server.register(
[
Brule,
2017-07-17 17:45:14 +03:00
{
register: Good,
options: goodOptions
2017-07-17 17:45:14 +03:00
},
{
register: Portal,
options: portalOptions
2017-07-17 17:45:14 +03:00
},
{
register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' }
}
],
(err) => {
2017-06-23 19:24:25 +03:00
handlerError(err);
server.start((err) => {
handlerError(err);
console.log(`server started at http://localhost:${server.info.port}`);
});
}
);
};
2017-06-23 19:24:25 +03:00
const handlerError = function (err) {
if (err) {
console.error(err);
2017-06-23 19:24:25 +03:00
process.exit(1);
}
};
2017-08-21 22:09:15 +03:00
process.on('unhandledRejection', (err) => {
console.error(err);
});
loadConfig();