aboutsummaryrefslogtreecommitdiff
path: root/httpd.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-21 01:39:56 +0700
committerShulhan <ms@kilabit.info>2022-04-21 01:39:56 +0700
commit26bbadb4ca1c38f2d34a722b0c7d9509135a7eb2 (patch)
treeb0c0a3d3f7d6314f3a9a2798557ce6a2fc683d22 /httpd.go
parentd99356259a60ac0e2c345e60b3a6c92c3fb9d764 (diff)
downloadrescached-26bbadb4ca1c38f2d34a722b0c7d9509135a7eb2.tar.xz
all: implement HTTP API to enable or disable hosts on block.d
The URL /api/block.d/enable activate the hosts in block.d, while The URL /api/block.d/disable deactivate the hosts in block.d. Both of this API accept single parameter "name" in the body as application/x-www-form-urlencoded.
Diffstat (limited to 'httpd.go')
-rw-r--r--httpd.go111
1 files changed, 104 insertions, 7 deletions
diff --git a/httpd.go b/httpd.go
index c2378e6..c1ced0c 100644
--- a/httpd.go
+++ b/httpd.go
@@ -25,15 +25,22 @@ const (
paramNameQuery = "query"
paramNameType = "type"
paramNameValue = "value"
- apiBlockd = "/api/block.d"
- apiBlockdUpdate = "/api/block.d/update"
+
+ apiBlockd = "/api/block.d"
+ apiBlockdDisable = "/api/block.d/disable"
+ apiBlockdEnable = "/api/block.d/enable"
+ apiBlockdUpdate = "/api/block.d/update"
+
apiCaches = "/api/caches"
apiCachesSearch = "/api/caches/search"
- apiEnvironment = "/api/environment"
- apiHostsDir = "/api/hosts.d/:name"
- apiHostsDirRR = "/api/hosts.d/:name/rr"
- apiZone = "/api/zone.d/:name"
- apiZoneRRType = "/api/zone.d/:name/rr/:type"
+
+ apiEnvironment = "/api/environment"
+
+ apiHostsDir = "/api/hosts.d/:name"
+ apiHostsDirRR = "/api/hosts.d/:name/rr"
+
+ apiZone = "/api/zone.d/:name"
+ apiZoneRRType = "/api/zone.d/:name/rr/:type"
)
func (srv *Server) httpdInit() (err error) {
@@ -123,6 +130,28 @@ func (srv *Server) httpdRegisterEndpoints() (err error) {
err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPost,
+ Path: apiBlockdDisable,
+ RequestType: libhttp.RequestTypeForm,
+ ResponseType: libhttp.ResponseTypeJSON,
+ Call: srv.httpApiBlockdDisable,
+ })
+ if err != nil {
+ return err
+ }
+
+ err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
+ Method: libhttp.RequestMethodPost,
+ Path: apiBlockdEnable,
+ RequestType: libhttp.RequestTypeForm,
+ ResponseType: libhttp.ResponseTypeJSON,
+ Call: srv.httpApiBlockdEnable,
+ })
+ if err != nil {
+ return err
+ }
+
+ err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
+ Method: libhttp.RequestMethodPost,
Path: apiBlockdUpdate,
RequestType: libhttp.RequestTypeJSON,
ResponseType: libhttp.ResponseTypeJSON,
@@ -250,6 +279,74 @@ func (srv *Server) httpdRun() {
}
}
+// httpApiBlockdDisable disable the hosts block.d.
+func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
+ var (
+ res = libhttp.EndpointResponse{}
+
+ hb *hostsBlock
+ hbName string
+ )
+
+ hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName))
+
+ hb = srv.env.HostsBlocks[hbName]
+ if hb == nil {
+ res.Code = http.StatusBadRequest
+ res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName)
+ return nil, &res
+ }
+
+ if hb.IsEnabled {
+ err = hb.disable()
+ if err != nil {
+ res.Code = http.StatusInternalServerError
+ res.Message = err.Error()
+ return nil, &res
+ }
+ }
+
+ res.Code = http.StatusOK
+ res.Message = fmt.Sprintf("hosts block.d %s has succesfully disabled", hbName)
+ res.Data = hb
+
+ return json.Marshal(&res)
+}
+
+// httpApiBlockdEnable enable the hosts block.d.
+func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
+ var (
+ res = libhttp.EndpointResponse{}
+
+ hb *hostsBlock
+ hbName string
+ )
+
+ hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName))
+
+ hb = srv.env.HostsBlocks[hbName]
+ if hb == nil {
+ res.Code = http.StatusBadRequest
+ res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName)
+ return nil, &res
+ }
+
+ if !hb.IsEnabled {
+ err = hb.enable()
+ if err != nil {
+ res.Code = http.StatusInternalServerError
+ res.Message = err.Error()
+ return nil, &res
+ }
+ }
+
+ res.Code = http.StatusOK
+ res.Message = fmt.Sprintf("hosts block.d %s has succesfully enabled", hbName)
+ res.Data = hb
+
+ return json.Marshal(&res)
+}
+
// httpApiBlockdUpdate fetch the latest hosts file from the hosts block
// provider based on registered URL.
//