aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-06-11 01:48:50 +0700
committerShulhan <ms@kilabit.info>2022-06-12 13:02:21 +0700
commitb76e182e94f55ce542ba336ea35bcb108e924c3f (patch)
tree9b682de074402632a5c96f12df4f9ba7ad1d8da1
parent8c8619d939e57c42dcb28c3f680ec9a3c6d075fa (diff)
downloadrescached-b76e182e94f55ce542ba336ea35bcb108e924c3f.tar.xz
all: make GET /api/hosts.d to return list of hosts.d if name is empty
This is to provide an HTTP API to fetch list of hosts.d files on the server.
-rw-r--r--httpd.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/httpd.go b/httpd.go
index 2e50b45..09ac8c8 100644
--- a/httpd.go
+++ b/httpd.go
@@ -925,8 +925,16 @@ func (srv *Server) apiHostsdDelete(epr *libhttp.EndpointRequest) (resbody []byte
//
// # Request
//
+// Format,
+//
// GET /api/hosts.d?name=<name>
//
+// Parameters,
+//
+// - name: string, optional, the name of hosts file where content to be
+// fetch.
+// If its empty, it will return all hosts files.
+//
// # Response
//
// On success, it will return list of resource record in JSON format.
@@ -939,6 +947,13 @@ func (srv *Server) apiHostsdGet(epr *libhttp.EndpointRequest) (resbody []byte, e
found bool
)
+ name = strings.TrimSpace(name)
+ if len(name) == 0 {
+ res.Code = http.StatusOK
+ res.Data = srv.env.HostsFiles
+ return json.Marshal(&res)
+ }
+
hf, found = srv.env.HostsFiles[name]
if !found {
res.Code = http.StatusNotFound