From: Christian Hofstaedtler Date: Tue, 7 Jan 2014 10:46:00 +0000 (+0100) Subject: webserver: implement server discovery endpoints X-Git-Tag: rec-3.6.0-rc1~217^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=765afe1a144c0e0626107e88381dd6ff15f854e9;p=pdns webserver: implement server discovery endpoints --- diff --git a/pdns/ws.cc b/pdns/ws.cc index 533f66337..5e76bfc31 100644 --- a/pdns/ws.cc +++ b/pdns/ws.cc @@ -370,6 +370,40 @@ static string createOrUpdateZone(const string& zonename, bool onlyCreate, varmap return getZone(zonename); } +static void fillServerDetail(Value& out, Value::AllocatorType& allocator) { + out.SetObject(); + out.AddMember("type", "Server", allocator); + out.AddMember("id", "localhost", allocator); + out.AddMember("url", "/servers/localhost", allocator); + out.AddMember("daemon_type", "authoritative", allocator); + out.AddMember("version", VERSION, allocator); + out.AddMember("config_url", "/servers/localhost/config{/config_setting}", allocator); + out.AddMember("zones_url", "/servers/localhost/zones{/zone}", allocator); +} + +static void apiServer(HttpRequest* req, HttpResponse* resp) { + if(req->method != "GET") + throw HttpMethodNotAllowedException(); + + vector items = ::arg().list(); + Document doc; + doc.SetArray(); + Value server; + fillServerDetail(server, doc.GetAllocator()); + doc.PushBack(server, doc.GetAllocator()); + resp->body = makeStringFromDocument(doc); +} + +static void apiServerDetail(HttpRequest* req, HttpResponse* resp) { + if(req->method != "GET") + throw HttpMethodNotAllowedException(); + + vector items = ::arg().list(); + Document doc; + fillServerDetail(doc, doc.GetAllocator()); + resp->body = makeStringFromDocument(doc); +} + static void apiServerConfig(HttpRequest* req, HttpResponse* resp) { if(req->method != "GET") throw HttpMethodNotAllowedException(); @@ -735,6 +769,8 @@ void StatWebServer::launch() d_ws->registerHandler("/", boost::bind(&StatWebServer::indexfunction, this, _1, _2)); d_ws->registerHandler("/style.css", boost::bind(&StatWebServer::cssfunction, this, _1, _2)); if(::arg().mustDo("experimental-json-interface")) { + registerApiHandler("/servers", &apiServer); + registerApiHandler("/servers/localhost", &apiServerDetail); registerApiHandler("/servers/localhost/config", &apiServerConfig); registerApiHandler("/servers/localhost/search-log", &apiServerSearchLog); registerApiHandler("/servers/localhost/zones", &apiServerZones);