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

124 lines
2.4 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');
2017-07-17 17:45:14 +03:00
const HapiSwagger = require('hapi-swagger');
const Inert = require('inert');
2017-06-23 19:24:25 +03:00
const Toppsy = require('toppsy');
2017-07-17 17:45:14 +03:00
const Vision = require('vision');
2017-06-23 19:24:25 +03:00
const Pack = require('./package');
2017-07-17 17:45:14 +03:00
const Portal = require('./lib');
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 });
const swaggerOptions = {
info: {
title: 'Portal API Documentation',
version: Pack.version
}
};
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',
args: [{ log: '*', response: '*', error: '*' }]
},
{
module: 'good-console'
},
'stdout'
]
2017-06-23 19:24:25 +03:00
}
};
2017-07-17 17:45:14 +03:00
server.register(
[
Brule,
Inert,
Vision,
{
register: Good,
options: goodOptions
},
{
register: Portal,
options: portalOptions
},
{
register: HapiSwagger,
options: swaggerOptions
},
{
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);
}
}