From 363fe9ea19694a7121b7ee54d4d290301c85f650 Mon Sep 17 00:00:00 2001 From: Jona Heitzer Date: Wed, 1 Jan 2025 14:55:23 +0100 Subject: [PATCH] Parameterize route and index.html template to allow serving specific poll --- server.go | 22 ++++++-- static/{index.html => index.html.tmpl} | 1 + static/index.js | 76 -------------------------- 3 files changed, 19 insertions(+), 80 deletions(-) rename static/{index.html => index.html.tmpl} (99%) delete mode 100644 static/index.js diff --git a/server.go b/server.go index 31b652a..408a60c 100644 --- a/server.go +++ b/server.go @@ -64,14 +64,29 @@ func initDatabase() { func prepareStaticContent() { var err error - indexHtmlTemplate, err = template.ParseFiles("./static/index.html") + indexHtmlTemplate, err = template.ParseFiles("./static/index.html.tmpl") if err != nil { log.Fatalf("[!] Could not parse index.html template, error: %s\n", err) } } func handleGetRoot(w http.ResponseWriter, r *http.Request) { - err := indexHtmlTemplate.Execute(w, nil) + var pollId = r.PathValue("pollId") + var pollName string + const query = ` + SELECT name + FROM polls + WHERE id = ?; + ` + + err := db.QueryRow(query, pollId).Scan(&pollName) + if err != nil { + log.Printf("[!] No poll with id %s was found, error: %s\n", pollId, err) + http.Error(w, "Poll not found", http.StatusBadRequest) + return + } + + err = indexHtmlTemplate.Execute(w, struct{ PollName string }{PollName: pollName}) if err != nil { log.Printf("[!] Could not execute index.html template, error: %s\n", err) http.Error(w, "Error rendering template", http.StatusInternalServerError) @@ -123,7 +138,6 @@ func handlePostSubmit(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) return } - tx.Commit() log.Printf("[*] Persisted choices for poll %d, username %s to database\n", @@ -151,7 +165,7 @@ func main() { ) http.HandleFunc( - "GET /", + "GET /{pollId}", handleGetRoot, ) diff --git a/static/index.html b/static/index.html.tmpl similarity index 99% rename from static/index.html rename to static/index.html.tmpl index 97b299e..bc73e53 100644 --- a/static/index.html +++ b/static/index.html.tmpl @@ -49,6 +49,7 @@

Datefinder

+

{{ .PollName }}

diff --git a/static/index.js b/static/index.js deleted file mode 100644 index aae1aaf..0000000 --- a/static/index.js +++ /dev/null @@ -1,76 +0,0 @@ -// Days of the week -const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; - -// Select the calendar container -const calendar = document.getElementById("calendar"); - -// Create the weekly calendar -daysOfWeek.forEach((day) => { - // Create a column for each day - const dayColumn = document.createElement("div"); - dayColumn.classList.add("day-column"); - - // Add a header for the day - const dayHeader = document.createElement("div"); - dayHeader.classList.add("day-header"); - dayHeader.textContent = day; - dayColumn.appendChild(dayHeader); - - // Add hourly blocks for each day - for (let hour = 0; hour < 24; hour++) { - const hourBlock = document.createElement("div"); - hourBlock.classList.add("hour-block"); - hourBlock.textContent = `${hour}:00`; - - // Add a click event listener to toggle color - hourBlock.addEventListener("click", () => { - hourBlock.classList.toggle("clicked"); - }); - - dayColumn.appendChild(hourBlock); - } - - // Append the day column to the calendar - calendar.appendChild(dayColumn); -}); - -const submitName = document.getElementById("submit-name") -const submitBtn = document.getElementById("submit-btn") - -submitBtn.onclick = function() { - if (submitName.value == "") { - console.log("Submitter name is missing.") - return - } - - const days = calendar.children - const choices = Array.from(days).map( - day => Array.from(day.children).slice(1, 25).map( - hour => hour.classList.contains("clicked"))) - console.log(`Submitting dates for ${submitName.value}`) - console.log(JSON.stringify(choices)) - - const url = "http://localhost:8080/api/submit"; - - fetch(url, { - method: "POST", // Specify the HTTP method - headers: { - "Content-Type": "application/json", // Inform the server about the data format - }, - body: JSON.stringify({ pollId: 1, username: submitName.value, choices: choices }), // Convert the data to JSON string - }) - /* - .then(response => { - if (!response.ok) { - throw new Error('Network response was not ok ' + response.statusText); - } - return response.json(); // Parse the JSON response - }) - .then(data => { - console.log('Success:', data); // Handle the response data - }) - .catch(error => { - console.error('Error:', error); // Handle errors - }); - */ -}