From ded1372023f408303eb5d0d9bb6f4339c88e8429 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 16 May 2022 02:09:16 +0700 Subject: all: add new HTTP API to get all zones The HTTP API has the following format, GET /api/zone.d On success, it will return HTTP status code 200 with all zone formatted as JSON in the body. --- httpd.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/httpd.go b/httpd.go index 0f05c43..bd491ff 100644 --- a/httpd.go +++ b/httpd.go @@ -221,6 +221,17 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { return err } + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: apiZoned, + RequestType: libhttp.RequestTypeNone, + ResponseType: libhttp.ResponseTypeJSON, + Call: srv.apiZoned, + }) + if err != nil { + return err + } + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: apiZoned, @@ -960,6 +971,37 @@ func (srv *Server) apiHostsdRecordDelete(epr *libhttp.EndpointRequest) (resbody return json.Marshal(&res) } +// apiZoned return all zone files in JSON format. +// +// # Request +// +// GET /api/zone.d +// +// # Response +// +// On success, it will return HTTP status code 200 with all zone formatted as +// JSON in the body, +// +// Content-Type: application/json +// +// { +// "code": 200, +// "data": { +// ": , +// ... +// } +// } +func (srv *Server) apiZoned(epr *libhttp.EndpointRequest) (resb []byte, err error) { + var ( + res = libhttp.EndpointResponse{} + ) + + res.Code = http.StatusOK + res.Data = srv.env.Zones + resb, err = json.Marshal(&res) + return resb, err +} + func (srv *Server) apiZonedCreate(epr *libhttp.EndpointRequest) (resb []byte, err error) { var ( res = libhttp.EndpointResponse{} -- cgit v1.3