diff options
| author | Shulhan <ms@kilabit.info> | 2022-05-23 02:27:30 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-06-09 23:39:01 +0700 |
| commit | 03ae9f6bfde5262b44d2318cc1162c20d867a3f6 (patch) | |
| tree | 92e805bcd533bc1e2fa21c151b78a00a0b1526b5 /_www/rescached.js | |
| parent | fdb3c4462417dd1caeb28d263c1f3be6e453f6bb (diff) | |
| download | rescached-03ae9f6bfde5262b44d2318cc1162c20d867a3f6.tar.xz | |
all: change the HTTP API to update hosts in block.d
We have HTTP API to enable or disable block.d by batch by passing all
block.d records.
The name of HTTP API is /v2/block.d/update and this is duplicate with
API to update the content of hosts in block.d.
This changes fix this by creating new HTTP API /v2/block.d/fetch
that fetch the latest hosts file from specific block.d by its name.
While at it,
- reformat html and JavaScript files using js-beautify
- rename constants and methods for HTTP APIs
- add and update comments for some HTTP APIs
Diffstat (limited to '_www/rescached.js')
| -rw-r--r-- | _www/rescached.js | 155 |
1 files changed, 76 insertions, 79 deletions
diff --git a/_www/rescached.js b/_www/rescached.js index d0bc20c..5694635 100644 --- a/_www/rescached.js +++ b/_www/rescached.js @@ -2,25 +2,25 @@ // SPDX-License-Identifier: GPL-3.0-or-later const RRTypes = { - 1: "A", - 2: "NS", - 3: "MD", - 4: "MF", - 5: "CNAME", - 6: "SOA", - 7: "MB", - 8: "MG", - 9: "MR", - 10: "NULL", - 11: "WKS", - 12: "PTR", - 13: "HINFO", - 14: "MINFO", - 15: "MX", - 16: "TXT", - 28: "AAAA", - 33: "SRV", - 41: "OPT", + 1 : "A", + 2 : "NS", + 3 : "MD", + 4 : "MF", + 5 : "CNAME", + 6 : "SOA", + 7 : "MB", + 8 : "MG", + 9 : "MR", + 10 : "NULL", + 11 : "WKS", + 12 : "PTR", + 13 : "HINFO", + 14 : "MINFO", + 15 : "MX", + 16 : "TXT", + 28 : "AAAA", + 33 : "SRV", + 41 : "OPT", }; const contentTypeForm = "application/x-www-form-urlencoded"; @@ -41,7 +41,7 @@ function getRRTypeName(k) { class Rescached { static nanoSeconds = 1000000000; static apiBlockd = "/api/block.d"; - static apiBlockdUpdate = "/api/block.d/update"; + static apiBlockdFetch = "/api/block.d/fetch"; static apiCaches = "/api/caches"; static apiCachesSearch = "/api/caches/search"; static apiHostsd = "/api/hosts.d"; @@ -54,44 +54,52 @@ class Rescached { this.env = {}; } - async BlockdUpdate(name) { + async BlockdFetch(name) { let params = new URLSearchParams(); params.set("name", name); - const httpRes = await fetch(Rescached.apiBlockdUpdate, { - method: "POST", - headers: { - [headerContentType]: contentTypeForm, + const httpRes = await fetch(Rescached.apiBlockdFetch, { + method : "POST", + headers : { + [headerContentType] : contentTypeForm, }, - body: params.toString(), + body : params.toString(), + }); + return await httpRes.json(); + } + + async BlockdUpdate(hostsBlocks) { + const httpRes = await fetch(Rescached.apiBlockd, { + method : "PUT", + headers : { + [headerContentType] : contentTypeJson, + }, + body : JSON.stringify(hostsBlocks), }); return await httpRes.json(); } async Caches() { const res = await fetch(this.server + Rescached.apiCaches, { - headers: { - Connection: "keep-alive", + headers : { + Connection : "keep-alive", }, }); return await res.json(); } async CacheRemove(qname) { - const res = await fetch( - this.server + Rescached.apiCaches + "?name=" + qname, - { - method: "DELETE", - } - ); + const res = + await fetch(this.server + Rescached.apiCaches + "?name=" + qname, { + method : "DELETE", + }); return await res.json(); } async Search(query) { console.log("Search: ", query); - const res = await fetch( - this.server + Rescached.apiCachesSearch + "?query=" + query - ); + const res = await fetch(this.server + Rescached.apiCachesSearch + + "?query=" + query); return await res.json(); } @@ -130,17 +138,17 @@ class Rescached { params.set(paramNameName, name); const httpRes = await fetch(Rescached.apiHostsd, { - method: "POST", - headers: { - [headerContentType]: contentTypeForm, + method : "POST", + headers : { + [headerContentType] : contentTypeForm, }, - body: params.toString(), + body : params.toString(), }); let res = await httpRes.json(); if (res.code === 200) { this.env.HostsFiles[name] = { - Name: name, - Records: [], + Name : name, + Records : [], }; } return res; @@ -152,7 +160,7 @@ class Rescached { var url = Rescached.apiHostsd + "?" + params.toString(); const httpRes = await fetch(url, { - method: "DELETE", + method : "DELETE", }); const res = await httpRes.json(); if (httpRes.status === 200) { @@ -171,8 +179,8 @@ class Rescached { let res = await httpRes.json(); if (httpRes.Status === 200) { this.env.HostsFiles[name] = { - Name: name, - Records: res.data, + Name : name, + Records : res.data, }; } return res; @@ -185,11 +193,11 @@ class Rescached { params.set("value", value); const httpRes = await fetch(Rescached.apiHostsdRR, { - method: "POST", - headers: { - [headerContentType]: contentTypeForm, + method : "POST", + headers : { + [headerContentType] : contentTypeForm, }, - body: params.toString(), + body : params.toString(), }); const res = await httpRes.json(); if (httpRes.Status === 200) { @@ -207,7 +215,7 @@ class Rescached { const api = Rescached.apiHostsdRR + "?" + params.toString(); const httpRes = await fetch(api, { - method: "DELETE", + method : "DELETE", }); const res = await httpRes.json(); if (httpRes.Status === 200) { @@ -230,37 +238,26 @@ class Rescached { got.PruneThreshold = got.PruneThreshold * this.nanoSeconds; const httpRes = await fetch(this.server + "/api/environment", { - method: "POST", - headers: { - [headerContentType]: contentTypeJson, + method : "POST", + headers : { + [headerContentType] : contentTypeJson, }, - body: JSON.stringify(got), + body : JSON.stringify(got), }); return await httpRes.json(); } - async updateHostsBlocks(hostsBlocks) { - const httpRes = await fetch(Rescached.apiBlockd, { - method: "POST", - headers: { - [headerContentType]: contentTypeJson, - }, - body: JSON.stringify(hostsBlocks), - }); - return await httpRes.json(); - } - async ZoneFileCreate(name) { let params = new URLSearchParams(); params.set(paramNameName, name); const httpRes = await fetch(Rescached.apiZoned, { - method: "POST", - headers: { - [headerContentType]: contentTypeForm, + method : "POST", + headers : { + [headerContentType] : contentTypeForm, }, - body: params.toString(), + body : params.toString(), }); let res = await httpRes.json(); if (res.code == 200) { @@ -275,7 +272,7 @@ class Rescached { let url = Rescached.apiZoned + "?" + params.toString(); const httpRes = await fetch(url, { - method: "DELETE", + method : "DELETE", }); let res = await httpRes.json(); if (res.code == 200) { @@ -297,17 +294,17 @@ class Rescached { async ZonedRecordAdd(name, rr) { let req = { - name: name, - type: getRRTypeName(rr.Type), - record: btoa(JSON.stringify(rr)), + name : name, + type : getRRTypeName(rr.Type), + record : btoa(JSON.stringify(rr)), }; const httpRes = await fetch(Rescached.apiZonedRR, { - method: "POST", - headers: { - [headerContentType]: contentTypeJson, + method : "POST", + headers : { + [headerContentType] : contentTypeJson, }, - body: JSON.stringify(req), + body : JSON.stringify(req), }); let res = await httpRes.json(); @@ -335,7 +332,7 @@ class Rescached { let api = Rescached.apiZonedRR + "?" + params.toString(); const httpRes = await fetch(api, { - method: "DELETE", + method : "DELETE", }); let res = await httpRes.json(); |
