digd.js/lib/httpd.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-01-10 08:14:05 +00:00
'use strict';
2018-01-10 21:54:08 +00:00
module.exports.create = function (cli, engine/*, dnsd*/) {
2018-01-10 08:14:05 +00:00
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) {
2018-01-10 21:54:08 +00:00
engine.peers.all(function (err, peers) {
res.send({ peers: peers });
});
2018-01-10 08:14:05 +00:00
});
app.get('/api/zones', function (req, res) {
2018-01-10 08:25:56 +00:00
engine.zones.all(function (err, zones) {
2018-01-10 08:14:05 +00:00
res.send({ zones: zones });
});
});
2018-01-10 21:54:08 +00:00
function mapRecord(r) {
return {
id: r.id
, zone: r.zone
, name: r.name
, tld: r.tld
, type: r.type
, class: r.class
, ttl: r.ttl
, data: r.data
, address: r.address
, exchange: r.exchange
, priority: r.priority
, value: r.value
, aname: r.aname
, flag: r.flag
, tag: r.tag
, weight: r.weight
, port: r.port
, target: r.target
};
}
app.get('/api/zones/:zone/records', function (req, res) {
engine.records.all(function (err, records) {
res.send({ records: records.filter(function (r) {
return r.zone === req.params.zone;
}).map(mapRecord) });
});
});
app.get('/api/records', function (req, res) {
2018-01-10 08:25:56 +00:00
engine.records.all(function (err, records) {
2018-01-10 21:54:08 +00:00
res.send({ records: records.map(mapRecord) });
});
});
app.get('/api/records/:name', function (req, res) {
engine.records.all(function (err, records) {
res.send({ records: records.filter(function (r) {
if (r.name === req.params.name) {
return true;
}
var parts = req.params.name.split('.');
parts.shift();
if ('*.' + parts.join('.') === r.name) {
return true;
}
}).map(mapRecord) });
2018-01-10 08:14:05 +00:00
});
});
2018-01-10 08:25:56 +00:00
httpServer.listen(cli.http, function () {
console.log(httpServer.address().address + '#' + httpServer.address().port + ' (http)');
});
2018-01-10 08:14:05 +00:00
}
runHttp();
};