aboutsummaryrefslogtreecommitdiff
path: root/_www
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-05-13 22:49:12 +0700
committerShulhan <ms@kilabit.info>2022-05-14 01:22:48 +0700
commit4e5ae6b15752c0a2a98b3414c5b88e0df43af4e4 (patch)
tree4b53f10cc999d8512e353abc8fc46eb6b632b357 /_www
parent20c3f80e7dfd9e453d757199beb2137c09a9f536 (diff)
downloadrescached-4e5ae6b15752c0a2a98b3414c5b88e0df43af4e4.tar.xz
all: refactor the HTTP API for zone.d
Previously, the the HTTP API for creating and deleting zone file is by passing the zone file name in path of URL. This changes move the parameter name to POST body when creating new zone file and in the DELETE query when deleting zone file.
Diffstat (limited to '_www')
-rw-r--r--_www/rescached.js85
-rw-r--r--_www/zone.d/index.html20
2 files changed, 70 insertions, 35 deletions
diff --git a/_www/rescached.js b/_www/rescached.js
index 15d7673..cda8b43 100644
--- a/_www/rescached.js
+++ b/_www/rescached.js
@@ -23,8 +23,13 @@ const RRTypes = {
41: "OPT",
}
+const contentTypeForm = "application/x-www-form-urlencoded"
+const contentTypeJson = "application/json"
+
const paramNameName = "name"
+const headerContentType = "Content-Type"
+
function getRRTypeName(k) {
let v = RRTypes[k]
if (v === "") {
@@ -41,7 +46,8 @@ class Rescached {
static apiCachesSearch = "/api/caches/search"
static apiHostsd = "/api/hosts.d"
static apiHostsdRR = "/api/hosts.d/rr"
- static apiZoned = "/api/zone.d/"
+ static apiZoned = "/api/zone.d"
+ static apiZonedRR = "/api/zone.d/rr"
constructor(server) {
this.server = server
@@ -55,7 +61,7 @@ class Rescached {
const httpRes = await fetch(Rescached.apiBlockdUpdate, {
method: "POST",
headers: {
- "Content-Type": "application/x-www-form-urlencoded",
+ [headerContentType]: contentTypeForm,
},
body: params.toString(),
})
@@ -84,7 +90,10 @@ class Rescached {
async Search(query) {
console.log("Search: ", query)
const res = await fetch(
- this.server + Rescached.apiCachesSearch + "?query=" + query,
+ this.server +
+ Rescached.apiCachesSearch +
+ "?query=" +
+ query,
)
return await res.json()
}
@@ -94,9 +103,11 @@ class Rescached {
const res = await httpRes.json()
if (httpRes.status === 200) {
- res.data.PruneDelay = res.data.PruneDelay / Rescached.nanoSeconds
+ res.data.PruneDelay =
+ res.data.PruneDelay / Rescached.nanoSeconds
res.data.PruneThreshold =
- res.data.PruneThreshold / Rescached.nanoSeconds
+ res.data.PruneThreshold /
+ Rescached.nanoSeconds
for (let k in res.data.HostsFiles) {
if (!res.data.HostsFiles.hasOwnProperty(k)) {
@@ -127,7 +138,7 @@ class Rescached {
const httpRes = await fetch(Rescached.apiHostsd, {
method: "POST",
headers: {
- "Content-Type": "application/x-www-form-urlencoded",
+ [headerContentType]: contentTypeForm,
},
body: params.toString(),
})
@@ -182,7 +193,7 @@ class Rescached {
const httpRes = await fetch(Rescached.apiHostsdRR, {
method: "POST",
headers: {
- "Content-Type": "application/x-www-form-urlencoded",
+ [headerContentType]: contentTypeForm,
},
body: params.toString(),
})
@@ -224,13 +235,16 @@ class Rescached {
got.PruneDelay = got.PruneDelay * this.nanoSeconds
got.PruneThreshold = got.PruneThreshold * this.nanoSeconds
- const httpRes = await fetch(this.server + "/api/environment", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
+ const httpRes = await fetch(
+ this.server + "/api/environment",
+ {
+ method: "POST",
+ headers: {
+ [headerContentType]: contentTypeJson,
+ },
+ body: JSON.stringify(got),
},
- body: JSON.stringify(got),
- })
+ )
return await httpRes.json()
}
@@ -239,7 +253,7 @@ class Rescached {
const httpRes = await fetch(Rescached.apiBlockd, {
method: "POST",
headers: {
- "Content-Type": "application/json",
+ [headerContentType]: contentTypeJson,
},
body: JSON.stringify(hostsBlocks),
})
@@ -247,39 +261,55 @@ class Rescached {
}
async ZoneFileCreate(name) {
- const httpRes = await fetch(this.server + Rescached.apiZoned + name, {
- method: "PUT",
+ let params = new URLSearchParams()
+ params.set(paramNameName, name)
+
+ const httpRes = await fetch(Rescached.apiZoned, {
+ method: "POST",
+ headers: {
+ [headerContentType]: contentTypeForm,
+ },
+ body: params.toString(),
})
let res = await httpRes.json()
if (res.code == 200) {
- this.env.ZoneFiles[name] = res.data
+ this.env.Zones[name] = res.data
}
return res
}
async ZoneFileDelete(name) {
- const httpRes = await fetch(this.server + Rescached.apiZoned + name, {
+ let params = new URLSearchParams()
+ params.set(paramNameName, name)
+
+ let url = Rescached.apiZoned + "?" + params.toString()
+ const httpRes = await fetch(url, {
method: "DELETE",
})
let res = await httpRes.json()
if (res.code == 200) {
- delete this.env.ZoneFiles[name]
+ delete this.env.Zones[name]
}
return res
}
async ZoneFileRecordCreate(name, rr) {
- let api = this.server + Rescached.apiZoned + name + "/rr/" + rr.Type
+ let api =
+ this.server +
+ Rescached.apiZoned +
+ name +
+ "/rr/" +
+ rr.Type
const httpRes = await fetch(api, {
method: "POST",
headers: {
- "Content-Type": "application/json",
+ [headerContentType]: contentTypeJson,
},
body: JSON.stringify(rr),
})
let res = await httpRes.json()
if (httpRes.status === 200) {
- let zf = this.env.ZoneFiles[name]
+ let zf = this.env.Zones[name]
if (rr.Type == 6) {
// SOA.
zf.SOA = res.data
@@ -291,17 +321,22 @@ class Rescached {
}
async ZoneFileRecordDelete(name, rr) {
- let api = this.server + Rescached.apiZoned + name + "/rr/" + rr.Type
+ let api =
+ this.server +
+ Rescached.apiZoned +
+ name +
+ "/rr/" +
+ rr.Type
const httpRes = await fetch(api, {
method: "DELETE",
headers: {
- "Content-Type": "application/json",
+ [headerContentType]: contentTypeJson,
},
body: JSON.stringify(rr),
})
let res = await httpRes.json()
if (httpRes.status === 200) {
- this.env.ZoneFiles[name].Records = res.data
+ this.env.Zones[name].Records = res.data
}
return res
}
diff --git a/_www/zone.d/index.html b/_www/zone.d/index.html
index 286c20e..db56d4c 100644
--- a/_www/zone.d/index.html
+++ b/_www/zone.d/index.html
@@ -94,7 +94,7 @@
<div class="nav-left">
<h3>Zone files</h3>
- <div id="ZoneFiles"></div>
+ <div id="Zones"></div>
<label for="newZoneFile"> New zone file: </label>
<input id="newZoneFile" />
@@ -303,7 +303,7 @@
notifError(res.message)
return
}
- renderZoneFiles(resc.env.ZoneFiles)
+ renderZones(resc.env.Zones)
resetActiveZone()
}
@@ -318,7 +318,7 @@
notifError(res.message)
return
}
- renderZoneFiles(resc.env.ZoneFiles)
+ renderZones(resc.env.Zones)
}
async function deleteZoneFile() {
@@ -327,7 +327,7 @@
notifError(res.message)
return
}
- renderZoneFiles(resc.env.ZoneFiles)
+ renderZones(resc.env.Zones)
resetActiveZone()
notifInfo(res.message)
}
@@ -408,14 +408,14 @@
}
}
- function renderZoneFiles(zoneFiles) {
- let wrapper = document.getElementById("ZoneFiles")
+ function renderZones(zones) {
+ let wrapper = document.getElementById("Zones")
out = ""
- for (let name in zoneFiles) {
- if (!zoneFiles.hasOwnProperty(name)) {
+ for (let name in zones) {
+ if (!zones.hasOwnProperty(name)) {
continue
}
- let zoneFile = zoneFiles[name]
+ let zoneFile = zones[name]
out += `
<div class="item">
<span onclick="setActiveZone('${zoneFile.Name}')">
@@ -528,7 +528,7 @@
}
function setActiveZone(name) {
- activeZone = resc.env.ZoneFiles[name]
+ activeZone = resc.env.Zones[name]
console.log("setActiveZone: ", activeZone)
renderActiveZone()
renderActiveZoneSOA()