WIP add http rest interface
This commit is contained in:
parent
abaed056e5
commit
94cf2aa4bc
15
bin/digd.js
15
bin/digd.js
|
@ -34,6 +34,7 @@ cli.parse({
|
|||
//, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ]
|
||||
//, 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ]
|
||||
//, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ]
|
||||
, 'http': [ false, 'enable http on the specified port', 'int' ]
|
||||
});
|
||||
|
||||
cli.main(function (args, cli) {
|
||||
|
@ -95,6 +96,8 @@ cli.main(function (args, cli) {
|
|||
}
|
||||
}
|
||||
|
||||
var engine;
|
||||
var engineOpts = { filepath: path.resolve(cli.input) };
|
||||
var dnsd = {};
|
||||
dnsd.onMessage = function (nb, cb) {
|
||||
var byteOffset = nb._dnsByteOffset || nb.byteOffset;
|
||||
|
@ -383,9 +386,8 @@ cli.main(function (args, cli) {
|
|||
sendResponse(resp);
|
||||
}
|
||||
|
||||
var engine;
|
||||
try {
|
||||
engine = require('../lib/store.json.js').create({ filepath: path.resolve(cli.input) });
|
||||
engine = engine || require('../lib/store.json.js').create(engineOpts);
|
||||
} catch(e) {
|
||||
respondWithResults(e);
|
||||
return;
|
||||
|
@ -409,6 +411,15 @@ cli.main(function (args, cli) {
|
|||
if (cli.tcp /* TODO v1.3 !cli.notcp */) {
|
||||
require('../lib/tcpd.js').create(cli, dnsd);
|
||||
}
|
||||
if (cli.http) {
|
||||
try {
|
||||
engine = engine || require('../lib/store.json.js').create(engineOpts);
|
||||
} catch(e) {
|
||||
respondWithResults(e);
|
||||
return;
|
||||
}
|
||||
require('../lib/httpd.js').create(cli, engine, dnsd);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
if (!cli.nocmd) {
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
'use strict';
|
||||
|
||||
module.exports.create = function (cli, engine, dnsd) {
|
||||
|
||||
function runHttp() {
|
||||
var path = require('path');
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var httpServer = require('http').createServer(app);
|
||||
|
||||
app.use('/', express.static(path.join(__dirname, 'public')));
|
||||
app.get('/api/peers', function (req, res) {
|
||||
res.send({ nameservers: [] });
|
||||
});
|
||||
app.get('/api/zones', function (req, res) {
|
||||
engine.zones.all(function (zones) {
|
||||
res.send({ zones: zones });
|
||||
});
|
||||
});
|
||||
app.get('/api/records/:zone', function (req, res) {
|
||||
engine.records.all(function (records) {
|
||||
res.send({ records: records });
|
||||
});
|
||||
});
|
||||
|
||||
httpServer.listen(cli.http);
|
||||
}
|
||||
|
||||
runHttp();
|
||||
|
||||
};
|
|
@ -7,15 +7,28 @@ module.exports.create = function (opts) {
|
|||
var db = require(opts.filepath);
|
||||
|
||||
engine.primaryNameservers = db.primaryNameservers;
|
||||
engine.getSoas = function (query, cb) {
|
||||
engine.zones = {
|
||||
all: function (query, cb) {
|
||||
process.nextTick(function () {
|
||||
cb(null, db.domains.slice(0));
|
||||
});
|
||||
}
|
||||
, get: function (query, cb) {
|
||||
var myDomains = db.domains.filter(function (d) {
|
||||
return -1 !== query.names.indexOf(d.id.toLowerCase());
|
||||
});
|
||||
process.nextTick(function () {
|
||||
cb(null, myDomains);
|
||||
});
|
||||
}
|
||||
};
|
||||
engine.getRecords = function (query, cb) {
|
||||
engine.records = {
|
||||
all: function (query, cb) {
|
||||
process.nextTick(function () {
|
||||
cb(null, db.records.slice(0));
|
||||
});
|
||||
}
|
||||
, get: function (query, cb) {
|
||||
var myRecords = db.records.slice(0).filter(function (r) {
|
||||
|
||||
if ('string' !== typeof r.name) {
|
||||
|
@ -31,7 +44,10 @@ module.exports.create = function (opts) {
|
|||
process.nextTick(function () {
|
||||
cb(null, myRecords);
|
||||
});
|
||||
}
|
||||
};
|
||||
engine.getSoas = engine.zones.get;
|
||||
engine.getRecords = engine.records.get;
|
||||
|
||||
return engine;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue