redbird
Version:
A reverse proxy with support for dynamic tables
80 lines • 3.13 kB
JavaScript
/*eslint-env node */
;
export class ETCDModule {
constructor(redbird, options) {
this.redbird = redbird;
const Etcd = require('node-etcd');
// Create Redbird Instance and Log
this.redbird = redbird;
const log = redbird.logger;
const _this = this;
// Create node-etcd Instance
this.etcd = new Etcd(options.hosts, options.ssloptions);
this.etcd_dir = typeof options.path !== 'undefined' ? options.path : 'redbird';
// Create directory if not created
this.etcd.get(this.etcd_dir, function (err, body, header) {
if (err && err.errorCode == 100) {
_this.etcd.mkdir(_this.etcd_dir, function (err) {
if (err) {
log.error(err, 'etcd backend error');
}
else {
createWatcher();
}
});
}
else if (!err && body.node.dir) {
createWatcher();
}
else {
log.error(err, 'etcd backend error');
}
});
// Helper function to check if values contain settings
function IsJsonString(str) {
try {
JSON.parse(str);
}
catch (e) {
return false;
}
return true;
}
// Helper function to pretify etcd directory strings
function removeEtcDir(str) {
return str.replace(_this.etcd_dir, '').replace(/^\/+|\/+$/g, '');
}
function createWatcher() {
// Watch etcd directory
_this.watcher = _this.etcd.watcher(_this.etcd_dir, null, { recursive: true });
// On Add/Update
_this.watcher.on('change', function (body, headers) {
if (body.node.key && body.node.value && !IsJsonString(body.node.value)) {
_this.redbird.register(removeEtcDir(body.node.key), body.node.value);
}
else if (body.node.key && body.node.value && IsJsonString(body.node.value)) {
var config = JSON.parse(body.node.value);
if (typeof config.docker !== 'undefined') {
require('./index.js')
.docker(_this.redbird)
.register(body.node.key, body.node.value.docker, body.node.value);
}
else {
_this.redbird.register(removeEtcDir(body.node.key), config.hosts, config);
}
}
});
// On Delete
_this.watcher.on('delete', function (body, headers) {
if (body.node.key) {
_this.redbird.unregister(removeEtcDir(body.node.key));
}
});
// Handle Errors
_this.watcher.on('error', function (err) {
log.error(err, 'etcd backend error');
});
}
}
}
//# sourceMappingURL=etcd-backend.js.map