From 3a38245a6a418b2ddbe4dfcd916ab83708b6f500 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 22 May 2022 21:47:09 +0700 Subject: all: implement HTTP API to fetch list of block.d Given the following request, GET /api/block.d It will return list of hosts in block.d as JSON format: { "data": { "": ... } } --- httpd.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'httpd.go') diff --git a/httpd.go b/httpd.go index 3f8964d..389bc1f 100644 --- a/httpd.go +++ b/httpd.go @@ -118,6 +118,17 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { return err } + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: apiBlockd, + RequestType: libhttp.RequestTypeNone, + ResponseType: libhttp.ResponseTypeJSON, + Call: srv.httpApiBlockdList, + }) + if err != nil { + return err + } + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: apiBlockd, @@ -301,6 +312,34 @@ func (srv *Server) httpdRun() { } } +// httpApiBlockdList fetch the list of block.d files. +// +// # Request +// +// GET /api/block.d +// +// # Response +// +// On success it will return list of hosts in block.d, +// +// { +// "data": { +// "": +// ... +// } +// } +func (srv *Server) httpApiBlockdList(epr *libhttp.EndpointRequest) (resBody []byte, err error) { + var ( + res = libhttp.EndpointResponse{} + ) + + res.Code = http.StatusOK + res.Data = srv.env.HostBlockd + + resBody, err = json.Marshal(&res) + return resBody, err +} + // httpApiBlockdDisable disable the hosts block.d. func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( -- cgit v1.3