joyent-portal/packages/portal-api/lib/index.js

75 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-04-28 00:59:03 +03:00
'use strict';
2017-05-27 19:35:38 +03:00
const Schema = require('joyent-cp-gql-schema');
2017-05-23 03:13:15 +03:00
const Graphi = require('graphi');
const Piloted = require('piloted');
2017-05-11 23:28:32 +03:00
const PortalData = require('portal-data');
const PortalWatch = require('portal-watch');
2017-04-28 00:59:03 +03:00
const Pack = require('../package.json');
2017-05-27 19:35:38 +03:00
const Resolvers = require('./resolvers');
2017-04-28 00:59:03 +03:00
const internals = {};
2017-04-28 00:59:03 +03:00
module.exports = function (server, options, next) {
const docker = Piloted.service('docker-compose-api');
if (docker) {
2017-06-21 22:12:57 +03:00
options.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
}
2017-05-11 23:28:32 +03:00
const data = new PortalData(options.data);
const watch = new PortalWatch(Object.assign(options.watch, {
data
}));
data.on('error', (err) => {
server.log(['error'], err);
});
2017-05-02 01:28:03 +03:00
data.connect((err) => {
if (err) {
return next(err);
}
2017-04-28 00:59:03 +03:00
server.bind(Object.assign(data, {
watch
}));
2017-05-02 01:28:03 +03:00
Piloted.on('refresh', internals.refresh(data));
watch.poll();
2017-05-02 01:28:03 +03:00
server.register([
{
2017-05-23 03:13:15 +03:00
register: Graphi,
2017-05-27 19:35:38 +03:00
options: {
schema: Schema,
resolvers: Resolvers(data)
}
2017-04-28 00:59:03 +03:00
}
2017-05-02 01:28:03 +03:00
]);
next();
});
2017-04-28 00:59:03 +03:00
};
module.exports.attributes = {
name: Pack.name,
version: Pack.version,
once: true,
multiple: false
};
internals.refresh = function (data) {
return () => {
const docker = Piloted.service('docker-compose-api');
if (!docker) {
return;
}
data.reconnectCompose(`tcp://${docker.address}:${docker.port}`);
};
2017-06-21 22:12:57 +03:00
};