diff options
| author | Shulhan <ms@kilabit.info> | 2022-05-16 13:16:30 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-05-16 13:16:30 +0700 |
| commit | 3568245be794cb73790331f85ee2b6d1a20754ef (patch) | |
| tree | ac45cb3a7504ac546cdba18adfb161b35f29a903 /httpd.go | |
| parent | 11b83e218c699f581507959f40b2ef0f27131a74 (diff) | |
| download | rescached-3568245be794cb73790331f85ee2b6d1a20754ef.tar.xz | |
all: implement HTTP API to fetch records in zone
Sending the following request to HTTP server:
GET /api/zone.d/rr?zone=<string>
where zone parameter is the zone name, it will return list of records
in that zone.
Diffstat (limited to 'httpd.go')
| -rw-r--r-- | httpd.go | 60 |
1 files changed, 59 insertions, 1 deletions
@@ -254,6 +254,16 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { } 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, RequestType: libhttp.RequestTypeJSON, @@ -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=<string> +// +// 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": { +// "<domain>": [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 |
