summaryrefslogtreecommitdiff
path: root/httpd.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-24 17:56:11 +0700
committerShulhan <ms@kilabit.info>2022-04-25 01:50:39 +0700
commita619e95df4d64ca7a8ddf41d996b62dc23334f9f (patch)
treee070ae42070e8dad06c9f8e069b1cc47c7d6cde2 /httpd.go
parentf30942daafd60a629a17cb08beeca6e4bb86aa93 (diff)
downloadrescached-a619e95df4d64ca7a8ddf41d996b62dc23334f9f.tar.xz
all: refactor the APIs to manage hosts.d resource records (RR)
There are two APIs to manage hosts.d RR: create and delete. Previously, the create API pass the hosts name inside and values inside the path, POST /hosts.d/<name>/rr?domain=&value= This commit changes the request type to form, so all parameters move to body, POST /hosts.d/rr Content-Type: application/x-www-form-urlencoded name=&domain=&value= On delete API, we changes the name parameter to be send on query parameter along with domain to be deleted. Previously, the delete API was DELETE /hosts.d/<name>/rr?domain= now it become DELETE /hosts.d/rr?name=&domain=
Diffstat (limited to 'httpd.go')
-rw-r--r--httpd.go53
1 files changed, 43 insertions, 10 deletions
diff --git a/httpd.go b/httpd.go
index 44c5cfe..e336307 100644
--- a/httpd.go
+++ b/httpd.go
@@ -36,8 +36,8 @@ const (
apiEnvironment = "/api/environment"
- apiHostsd = "/api/hosts.d"
- apiHostsDirRR = "/api/hosts.d/:name/rr"
+ apiHostsd = "/api/hosts.d"
+ apiHostsdRR = "/api/hosts.d/rr"
apiZone = "/api/zone.d/:name"
apiZoneRRType = "/api/zone.d/:name/rr/:type"
@@ -198,10 +198,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) {
// Register API to create one record in hosts file.
err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPost,
- Path: apiHostsDirRR,
- RequestType: libhttp.RequestTypeQuery,
+ Path: apiHostsdRR,
+ RequestType: libhttp.RequestTypeForm,
ResponseType: libhttp.ResponseTypeJSON,
- Call: srv.apiHostsFileRRCreate,
+ Call: srv.apiHostsdRecordAdd,
})
if err != nil {
return err
@@ -210,10 +210,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) {
// Register API to delete a record from hosts file.
err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodDelete,
- Path: apiHostsDirRR,
+ Path: apiHostsdRR,
RequestType: libhttp.RequestTypeQuery,
ResponseType: libhttp.ResponseTypeJSON,
- Call: srv.apiHostsFileRRDelete,
+ Call: srv.apiHostsdRecordDelete,
})
if err != nil {
return err
@@ -809,8 +809,31 @@ func (srv *Server) apiHostsdGet(epr *libhttp.EndpointRequest) (resbody []byte, e
return json.Marshal(&res)
}
-// apiHostsFileRRCreate create new record and save it to the hosts file.
-func (srv *Server) apiHostsFileRRCreate(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+// apiHostsdRecordAdd add new record and save it to the hosts file.
+//
+// # Request
+//
+// Request format,
+//
+// POST /hosts.d/rr
+// content-type: application/x-www-form-urlencoded
+//
+// name=&domain=&value=
+//
+// Parameters,
+//
+// - name: the hosts file name where record to be added.
+// - domain: the domain name.
+// - value: the IPv4 or IPv6 address of domain name.
+//
+// If the domain name already exist, the new record will be appended to the
+// end of file.
+//
+// # Response
+//
+// On success, a single line "<domain> <value>" will be appended to the hosts
+// file as new record and return it to the caller.
+func (srv *Server) apiHostsdRecordAdd(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
var (
res = libhttp.EndpointResponse{}
hostsFileName = epr.HttpRequest.Form.Get(paramNameName)
@@ -875,7 +898,17 @@ func (srv *Server) apiHostsFileRRCreate(epr *libhttp.EndpointRequest) (resbody [
return json.Marshal(&res)
}
-func (srv *Server) apiHostsFileRRDelete(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+// apiHostsdRecordDelete delete a record from hosts file by domain name.
+//
+// # Request
+//
+// DELETE /hosts.d/record?name=&domain=
+//
+// # Response
+//
+// If the hosts file "name" exist and domain name exist, it will return HTTP
+// status code 200.
+func (srv *Server) apiHostsdRecordDelete(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
var (
res = libhttp.EndpointResponse{}
hostsFileName = epr.HttpRequest.Form.Get(paramNameName)