<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Poll</title>
</head>
<body>
<h2>Should we use Rust?</h2>
<div id="results"></div>
<button id="yes">Yes</button>
<button id="no">No</button>
<button id="maybe">Maybe</button>
</body>
<script>
const BASE_URL = "http://localhost:8086";
async function getVotes() {
const res = await fetch(BASE_URL + "/votes");
const votes = await res.json();
document.getElementById("results").innerText =
`Yes: ${votes.yes} No: ${votes.no} Maybe: ${votes.maybe}`;
}
async function vote(option) {
await fetch(BASE_URL + "/vote/" + option, { method: "POST" });
await getVotes();
}
document
.getElementById("yes")
.addEventListener("click", () => vote("yes"));
document
.getElementById("no")
.addEventListener("click", () => vote("no"));
document
.getElementById("maybe")
.addEventListener("click", () => vote("maybe"));
getVotes();
</script>
</html>