feat: Remove VueJS and rewrite in vanilla JS

pull/16/head
Karan Sharma 2021-03-04 18:23:04 +05:30
parent 022c1267b7
commit d985ecd1b4
3 changed files with 185 additions and 154 deletions

View File

@ -1,95 +1,167 @@
var app = new Vue({ const $ = document.querySelector.bind(document);
el: '#app', const $new = document.createElement.bind(document);
data: { const apiURL = "/api/lookup/";
apiURL: "/api/lookup/", const isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;
results: [], const nsAddrMap = {
noRecordsFound: false, "google": "8.8.8.8",
emptyNameError: false, "cloudflare": "1.1.1.1",
apiErrorMessage: "", "cloudflare-doh": "https://cloudflare-dns.com/dns-query",
queryName: "", "quad9": "9.9.9.9",
queryType: "A", }
nameserverName: "cloudflare",
customNSAddr: "",
nsAddrMap: { function handleNSChange() {
"google": "8.8.8.8", if ($('select[name=ns]').value == "custom") {
"cloudflare": "1.1.1.1", $('div[id=custom_ns]').classList.remove("hidden");
"cloudflare-doh": "https://cloudflare-dns.com/dns-query", $('div[id=ns]').classList.add("hidden");
"quad9": "9.9.9.9", } else {
} $('div[id=custom_ns]').classList.add("hidden");
}, $('div[id=ns]').classList.remove("hidden");
created: function () { $('input[name=ns]').placeholder = nsAddrMap[$('select[name=ns]').value];
}, }
computed: { }
getNSAddrValue() {
return this.nsAddrMap[this.nameserverName]
}, // Source: https://stackoverflow.com/a/1026087.
isCustomNS() { function capitalizeFirstLetter(string) {
if (this.nameserverName == "custom") { return string.charAt(0).toUpperCase() + string.slice(1);
return true }
window.addEventListener('DOMContentLoaded', (event) => {
handleNSChange();
});
(function () {
const fields = ['name', 'address', 'type', 'ttl', 'rtt', 'nameserver'];
// createRow creates a table row with the given cell values.
function createRow(item) {
const tr = $new('tr');
fields.forEach((f) => {
const td = $new('td');
td.classList.add("px-6", "py-4", "whitespace-nowrap", "text-sm");
if (f == "ttl" || f == "rtt" || f == "nameserver") {
td.classList.add("text-gray-500");
} else {
td.classList.add("text-gray-900");
} }
return false if (f == "name") {
} td.classList.add("font-semibold");
},
methods: {
prepareNS() {
switch (this.nameserverName) {
case "google":
return "tcp://8.8.8.8:53"
case "cloudflare":
return "tcp://1.1.1.1:53"
case "cloudflare-doh":
return "https://cloudflare-dns.com/dns-query"
case "quad9":
return "tcp://9.9.9.9:53"
case "custom":
return this.customNSAddr
default:
return ""
} }
}, if (f == "type") {
lookupRecords() { td.innerHTML = '<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">' + item[f] + '</span>';
// reset variables. } else {
this.results = [] td.innerText = item[f];
this.noRecordsFound = false
this.emptyNameError = false
this.apiErrorMessage = ""
if (this.queryName == "") {
this.emptyNameError = true
return
} }
tr.appendChild(td);
});
return tr;
}
// GET request using fetch with error handling
fetch(this.apiURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: [this.queryName,],
type: [this.queryType,],
nameservers: [this.prepareNS(),],
}),
}).then(async response => {
const res = await response.json();
// check for error response // createList creates a table row with the given cell values.
if (!response.ok) { function createList(item) {
// get error message from body or default to response statusText const ul = $new('ul');
const error = (res && res.message) || response.statusText; ul.classList.add("m-4", "block", "bg-indigo-100");
return Promise.reject(error); fields.forEach((f) => {
} const li = $new('li');
const span = $new('span');
span.classList.add("p-2", "text-gray-500", "font-semibold");
span.innerText = capitalizeFirstLetter(f) + ': ' + item[f]
li.appendChild(span);
ul.appendChild(li);
});
return ul;
}
if (res.data[0].answers == null) { function prepareNSAddr(ns) {
this.noRecordsFound = true switch (ns) {
} else { case "google":
// Set the answers in the results list. return "tcp://8.8.8.8:53"
this.results = res.data[0].answers case "cloudflare":
} return "tcp://1.1.1.1:53"
case "cloudflare-doh":
}).catch(error => { return "https://cloudflare-dns.com/dns-query"
this.apiErrorMessage = error case "quad9":
}); return "tcp://9.9.9.9:53"
case "custom":
return $('input[name=custom_ns]').value.trim()
default:
return ""
} }
} }
})
const postForm = body => {
return fetch(apiURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body
});
};
const handleSubmit = async (e) => {
e.preventDefault();
const tbl = $('table tbody');
const list = $('div[id=mobile-answers-sec]');
tbl.innerHTML = '';
list.innerHTML = '';
$('p[id=empty-name-sec]').classList.add("hidden");
const errSec = $('div[id=api-error-sec]');
errSec.classList.add("hidden");
const q = $('input[name=q]').value.trim(), typ = $('select[name=type]').value;
const ns = $('select[name=ns]').value;
if (!q) {
$('p[id=empty-name-sec]').classList.remove("hidden");
throw ('Invalid query name.');
}
if (!q || !typ || !ns) {
throw ('Invalid or empty query params.');
}
const nsAddr = prepareNSAddr(ns);
const body = JSON.stringify({ query: [q,], type: [typ,], nameservers: [nsAddr,] });
const response = await postForm(body);
const res = await response.json();
if (res.status != "success") {
// get error message from body or default to response statusText
const error = (res && res.message) || response.statusText;
errSec.classList.remove("hidden");
errSec.innerHTML = '<p class="text-xl text-red-500">' + error + '</p>'
throw (error);
}
if (res.data[0].answers == null) {
const errSec = $('div[id=api-error-sec]');
errSec.classList.remove("hidden");
errSec.innerHTML = '<p class="text-xl text-red-500">' + 'No records found!' + '</p>'
return null;
}
$('div[id=answer_sec]').classList.remove("hidden");
if (isMobile === true) {
list.classList.remove("hidden");
res.data[0].answers.forEach((item) => {
console.log("appending", item)
list.appendChild(createList(item));
});
} else {
res.data[0].answers.forEach((item) => {
tbl.appendChild(createRow(item));
});
}
};
document.querySelector('form').addEventListener('submit', handleSubmit);
})();

View File

@ -70,7 +70,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
err = app.LoadNameservers() err = app.LoadNameservers()
if err != nil { if err != nil {
app.Logger.WithError(err).Error("error loading nameservers") app.Logger.WithError(err).Error("error loading nameservers")
sendErrorResponse(w, fmt.Sprintf("Error lookuping up for records."), http.StatusInternalServerError, nil) sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
return return
} }
@ -86,7 +86,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
}) })
if err != nil { if err != nil {
app.Logger.WithError(err).Error("error loading resolver") app.Logger.WithError(err).Error("error loading resolver")
sendErrorResponse(w, fmt.Sprintf("Error lookuping up for records."), http.StatusInternalServerError, nil) sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
return return
} }
app.Resolvers = rslvrs app.Resolvers = rslvrs
@ -97,7 +97,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
resp, err := rslv.Lookup(q) resp, err := rslv.Lookup(q)
if err != nil { if err != nil {
app.Logger.WithError(err).Error("error looking up DNS records") app.Logger.WithError(err).Error("error looking up DNS records")
sendErrorResponse(w, fmt.Sprintf("Error lookuping up for records."), http.StatusInternalServerError, nil) sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
return return
} }
responses = append(responses, resp) responses = append(responses, resp)

View File

@ -10,17 +10,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com"> <link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
<style> <style>
[v-cloak]>* {
display: none;
}
/* [v-cloak]::before {
content: "loading...";
} */
body { body {
font-family: 'Poppins', sans-serif; font-family: 'Poppins', sans-serif;
} }
@ -39,18 +30,18 @@
<header class="mt-10"> <header class="mt-10">
<h1 class="text-5xl font-black text-center"><span class="text-indigo-700">Doggo</span> DNS</h1> <h1 class="text-5xl font-black text-center"><span class="text-indigo-700">Doggo</span> DNS</h1>
</header> </header>
<main id="app" v-cloak class="main flex flex-col flex-grow"> <main id="app" class="main flex flex-col flex-grow">
<form @submit.prevent="lookupRecords"> <form>
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 flex flex-col my-2"> <div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 flex flex-col my-2">
<div class="-mx-3 md:flex mb-6"> <div class="-mx-3 md:flex mb-6">
<div class="md:w-1/2 px-3 mb-6 md:mb-0"> <div class="md:w-1/2 px-3 mb-6 md:mb-0">
<label class="block tracking-wide text-grey-darker text-xs font-bold mb-2"> <label class="block tracking-wide text-grey-darker text-xs font-bold mb-2">
Domain Name Domain Name
</label> </label>
<input v-model="queryName" <input name="q"
class="appearance-none block w-full bg-grey-lighter text-grey-darker border border-red rounded py-3 px-4 mb-3" class="appearance-none block w-full bg-grey-lighter text-grey-darker border border-red rounded py-3 px-4 mb-3"
type="text" placeholder="domain.tld"> type="text" placeholder="domain.tld">
<p v-if="emptyNameError" class="text-red-500 text-xs">Please enter a domain name to <p id="empty-name-sec" class="hidden text-red-500 text-xs">Please enter a domain name to
query. query.
</p> </p>
</div> </div>
@ -60,8 +51,8 @@
</label> </label>
<select <select
class="block appearance-none w-full bg-grey-lighter border border-grey-lighter text-grey-darker py-3 px-4 pr-8 rounded" class="block appearance-none w-full bg-grey-lighter border border-grey-lighter text-grey-darker py-3 px-4 pr-8 rounded"
v-model="queryType"> name="type">
<option>A</option> <option default>A</option>
<option>AAAA</option> <option>AAAA</option>
<option>CAA</option> <option>CAA</option>
<option>CNAME</option> <option>CNAME</option>
@ -77,13 +68,13 @@
</div> </div>
<div class="-mx-3 md:flex"> <div class="-mx-3 md:flex">
<div class="md:w-1/2 px-3 mb-6 md:mb-0"> <div class="md:w-1/2 px-3 mb-6 md:mb-0">
<label class="block tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-state"> <label class="block tracking-wide text-grey-darker text-xs font-bold mb-2">
Nameserver Nameserver
</label> </label>
<select v-model="nameserverName" <select name="ns"
class="block appearance-none w-full bg-grey-lighter border border-grey-lighter text-grey-darker py-3 px-4 pr-8 rounded" class="block appearance-none w-full bg-grey-lighter border border-grey-lighter text-grey-darker py-3 px-4 pr-8 rounded"
id="grid-state"> onchange="handleNSChange()">>
<option value="cloudflare">Cloudflare</option> <option default value="cloudflare">Cloudflare</option>
<option value="cloudflare-doh">Cloudflare (DOH)</option> <option value="cloudflare-doh">Cloudflare (DOH)</option>
<option value="google">Google</option> <option value="google">Google</option>
<option value="quad9">Quad9</option> <option value="quad9">Quad9</option>
@ -94,30 +85,29 @@
<label class="block tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-zip"> <label class="block tracking-wide text-grey-darker text-xs font-bold mb-2" for="grid-zip">
Address Address
</label> </label>
<div v-if="isCustomNS"> <div id="custom_ns" class="hidden">
<input v-model="customNSAddr" class="appearance-none block w-full bg-grey-lighter text-grey-darker border <input name="custom_ns" class="appearance-none block w-full bg-grey-lighter text-grey-darker border
border-grey-lighter rounded py-3 px-4" id="grid-zip" type="text" border-grey-lighter rounded py-3 px-4" type="text" placeholder="Enter Nameserver address">
placeholder="Enter Nameserver address">
<p class="mt-2 text-grey-darker text-xs">To use different protocols like DOH, DOT etc. refer <p class="mt-2 text-grey-darker text-xs">To use different protocols like DOH, DOT etc. refer
to the to the
instructions <a class="font-semibold" instructions <a class="font-semibold"
href="https://github.com/mr-karan/doggo#transport-options">here</a>.</p> href="https://github.com/mr-karan/doggo#transport-options">here</a>.</p>
</div> </div>
<div v-else> <div id="ns">
<input readonly <input readonly name="ns"
class="appearance-none block w-full bg-grey-lighter text-grey-darker border border-grey-lighter rounded py-3 px-4" class="appearance-none block w-full bg-grey-lighter text-grey-darker border border-grey-lighter rounded py-3 px-4"
id="grid-zip" type="text" :placeholder="getNSAddrValue"> type="text">
</div> </div>
</div> </div>
</div> </div>
<div class="mt-8 text-right"> <div class="mt-8 text-right">
<button type="submit" class="py-2 px-10 text-white rounded-xl bg-indigo-800">Submit</button> <button class="py-2 px-10 text-white rounded-xl bg-indigo-800">Submit</button>
</div> </div>
</div> </div>
</form> </form>
<!--Responses--> <!--Responses-->
<div v-if="Object.keys(results).length > 0" class="mt-12 flex flex-col"> <div id="answer_sec" class="hidden mt-12 flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8 sm:block hidden"> <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8 sm:block hidden">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8"> <div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg"> <div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
@ -151,55 +141,24 @@
</tr> </tr>
</thead> </thead>
<tbody class="bg-white divide-y divide-gray-200"> <tbody class="bg-white divide-y divide-gray-200">
<tr v-for="answer in results"> <!--tr is added by JS-->
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 font-medium">
{{answer.name}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
{{answer.address}}
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span
class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
{{answer.type}}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{answer.ttl}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{answer.rtt}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{answer.nameserver}}
</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>
</div> </div>
<!--On Mobile--> <!--On Mobile-->
<ul v-for="answer in results" class="m-4 sm:hidden block bg-indigo-100"> <div id="mobile-answers-sec" class="hidden">
<li><span class="p-2 text-gray-500 font-semibold">Name:</span> {{answer.name}}</li> </div>
<li><span class="p-2 text-gray-500 font-semibold">Address:</span> {{answer.address}}</li>
<li><span class="p-2 text-gray-500 font-semibold">Type:</span> {{answer.type}}</li>
<li><span class="p-2 text-gray-500 font-semibold">Nameserver:</span> {{answer.nameserver}}</li>
<li><span class="p-2 text-gray-500 font-semibold">TTL:</span> {{answer.ttl}}</li>
<li><span class="p-2 text-gray-500 font-semibold">RTT:</span> {{answer.rtt}}</li>
</ul>
</div> </div>
<!--No Answers--> <!--No Answers-->
<div v-if="noRecordsFound" class="mt-12 text-center"> <div id="no-answers-sec" class="hidden mt-12 text-center">
<p class="text-xl text-gray-900">Oops! Found no records for this query.</p> <p class="text-xl text-gray-900">Oops! Found no records for this query.</p>
</div> </div>
<!--Show Error--> <!--Show Error-->
<div v-if="apiErrorMessage" class="mt-12 text-center"> <div id="api-error-sec" class="hidden mt-12 text-center">
<p class="text-xl text-red-500">{{apiErrorMessage}}</p>
</div> </div>
</main> </main>