'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) { engine.peers.all(function (err, peers) { res.send({ peers: peers }); }); }); app.get('/api/zones', function (req, res) { engine.zones.all(function (err, zones) { res.send({ zones: zones }); }); }); 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) { engine.records.all(function (err, records) { 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) }); }); }); httpServer.listen(cli.http, function () { console.log(httpServer.address().address + '#' + httpServer.address().port + ' (http)'); }); } runHttp(); };