From 3568245be794cb73790331f85ee2b6d1a20754ef Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 16 May 2022 13:16:30 +0700 Subject: all: implement HTTP API to fetch records in zone Sending the following request to HTTP server: GET /api/zone.d/rr?zone= where zone parameter is the zone name, it will return list of records in that zone. --- httpd.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/httpd.go b/httpd.go index e91b038..d63ce30 100644 --- a/httpd.go +++ b/httpd.go @@ -253,6 +253,16 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { return err } + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: apiZonedRR, + RequestType: libhttp.RequestTypeQuery, + ResponseType: libhttp.ResponseTypeJSON, + Call: srv.apiZonedRR, + }) + if err != nil { + return err + } err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: apiZonedRR, @@ -273,7 +283,6 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { if err != nil { return err } - return nil } @@ -1093,6 +1102,55 @@ func (srv *Server) apiZonedDelete(epr *libhttp.EndpointRequest) (resb []byte, er return json.Marshal(&res) } +// apiZonedRR fetch all records on specific zone by its name. +// +// # Request +// +// GET /api/zone.d/rr?zone= +// +// Parameters, +// - zone: the zone file name where records to be fetched. +// +// # Response +// +// If the zone file exist, it will return HTTP status code 200 with response +// body contains mapping of domain name and records. +// +// { +// "code": 200, +// "data": { +// "": [dns.ResourceRecord], +// ... +// } +// } +func (srv *Server) apiZonedRR(epr *libhttp.EndpointRequest) (resb []byte, err error) { + var ( + zoneName = epr.HttpRequest.Form.Get(paramNameZone) + res = libhttp.EndpointResponse{} + + zone *dns.Zone + ) + + res.Code = http.StatusBadRequest + + if len(zoneName) == 0 { + res.Message = fmt.Sprintf("unknown or empty zone parameter: %q", zoneName) + return nil, &res + } + + zone = srv.env.Zones[zoneName] + if zone == nil { + res.Message = fmt.Sprintf("unknown or empty zone parameter: %q", zoneName) + return nil, &res + } + + res.Code = http.StatusOK + res.Data = zone.Records + + resb, err = json.Marshal(&res) + return resb, err +} + // apiZonedRRAdd create new RR for the zone file. // // # Request -- cgit v1.3