summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-20 01:17:09 +0700
committerShulhan <ms@kilabit.info>2022-04-20 01:17:09 +0700
commit289bb8e6bd9e6d8dcad93f92a1602779170c7fe6 (patch)
tree3589a9e052c064000710404b58464c3ed2fd965b
parentbf2c970904462b35595cf74c40691120e12ac15d (diff)
downloadrescached-289bb8e6bd9e6d8dcad93f92a1602779170c7fe6.tar.xz
all: implement HTTP API to update hosts.d
The API receive the block.d name and if it valid, the server will fetch latest hosts file from the block provider based on the registered URL.
-rw-r--r--httpd.go77
1 files changed, 75 insertions, 2 deletions
diff --git a/httpd.go b/httpd.go
index 4e2f226..c2378e6 100644
--- a/httpd.go
+++ b/httpd.go
@@ -25,10 +25,11 @@ const (
paramNameQuery = "query"
paramNameType = "type"
paramNameValue = "value"
+ apiBlockd = "/api/block.d"
+ apiBlockdUpdate = "/api/block.d/update"
apiCaches = "/api/caches"
apiCachesSearch = "/api/caches/search"
apiEnvironment = "/api/environment"
- apiHostsBlock = "/api/block.d"
apiHostsDir = "/api/hosts.d/:name"
apiHostsDirRR = "/api/hosts.d/:name/rr"
apiZone = "/api/zone.d/:name"
@@ -111,7 +112,7 @@ func (srv *Server) httpdRegisterEndpoints() (err error) {
err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPost,
- Path: apiHostsBlock,
+ Path: apiBlockd,
RequestType: libhttp.RequestTypeJSON,
ResponseType: libhttp.ResponseTypeJSON,
Call: srv.apiHostsBlockUpdate,
@@ -120,6 +121,17 @@ func (srv *Server) httpdRegisterEndpoints() (err error) {
return err
}
+ err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
+ Method: libhttp.RequestMethodPost,
+ Path: apiBlockdUpdate,
+ RequestType: libhttp.RequestTypeJSON,
+ ResponseType: libhttp.ResponseTypeJSON,
+ Call: srv.httpApiBlockdUpdate,
+ })
+ if err != nil {
+ return err
+ }
+
// Register API to create new hosts file.
err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPut,
@@ -238,6 +250,67 @@ func (srv *Server) httpdRun() {
}
}
+// httpApiBlockdUpdate fetch the latest hosts file from the hosts block
+// provider based on registered URL.
+//
+// # Request
+//
+// POST /api/block.d/update
+// Content-Type: application/json
+//
+// {
+// "Name": <block.d name>
+// }
+//
+// # Response
+//
+// On success, the hosts file will be updated and the server will be
+// restarted.
+func (srv *Server) httpApiBlockdUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
+ var (
+ logp = "httpApiBlockdUpdate"
+ res = libhttp.EndpointResponse{}
+
+ hb *hostsBlock
+ hbName string
+ )
+
+ err = json.Unmarshal(epr.RequestBody, &hb)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %w", logp, err)
+ }
+
+ hbName = strings.ToLower(hb.Name)
+
+ hb = srv.env.HostsBlocks[hbName]
+ if hb == nil {
+ res.Code = http.StatusBadRequest
+ res.Message = fmt.Sprintf("%s: unknown hosts block.d name: %s", logp, hbName)
+ return nil, &res
+ }
+
+ err = hb.update()
+ if err != nil {
+ res.Code = http.StatusInternalServerError
+ res.Message = fmt.Sprintf("%s: %s", logp, err)
+ return nil, &res
+ }
+
+ srv.Stop()
+ err = srv.Start()
+ if err != nil {
+ res.Code = http.StatusInternalServerError
+ res.Message = err.Error()
+ return nil, &res
+ }
+
+ res.Code = http.StatusOK
+ res.Message = fmt.Sprintf("%s: block.d %s has succesfully updated", logp, hbName)
+ res.Data = hb
+
+ return json.Marshal(&res)
+}
+
func (srv *Server) apiCaches(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
var (
res = libhttp.EndpointResponse{}