400 favico request and parameterize pollId

This commit is contained in:
2025-01-01 15:26:49 +01:00
parent 363fe9ea19
commit ebea1c07fd
2 changed files with 26 additions and 5 deletions
+25 -4
View File
@@ -7,6 +7,7 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"strconv"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@@ -71,22 +72,34 @@ func prepareStaticContent() {
} }
func handleGetRoot(w http.ResponseWriter, r *http.Request) { func handleGetRoot(w http.ResponseWriter, r *http.Request) {
var pollId = r.PathValue("pollId")
var pollName string var pollName string
pollId, err := strconv.Atoi(r.PathValue("pollId"))
if err != nil {
log.Printf("[!] Invallid poll id url segment, error: %s\n", err)
http.Error(w, "Poll not found", http.StatusBadRequest)
return
}
const query = ` const query = `
SELECT name SELECT name
FROM polls FROM polls
WHERE id = ?; WHERE id = ?;
` `
err := db.QueryRow(query, pollId).Scan(&pollName) err = db.QueryRow(query, pollId).Scan(&pollName)
if err != nil { if err != nil {
log.Printf("[!] No poll with id %s was found, error: %s\n", pollId, err) log.Printf("[!] No poll with id %d was found, error: %s\n", pollId, err)
http.Error(w, "Poll not found", http.StatusBadRequest) http.Error(w, "Poll not found", http.StatusBadRequest)
return return
} }
err = indexHtmlTemplate.Execute(w, struct{ PollName string }{PollName: pollName}) err = indexHtmlTemplate.Execute(w, struct {
PollId int
PollName string
}{
PollId: pollId,
PollName: pollName,
})
if err != nil { if err != nil {
log.Printf("[!] Could not execute index.html template, error: %s\n", err) log.Printf("[!] Could not execute index.html template, error: %s\n", err)
http.Error(w, "Error rendering template", http.StatusInternalServerError) http.Error(w, "Error rendering template", http.StatusInternalServerError)
@@ -112,6 +125,7 @@ func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err) log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return
} }
choicesJson, err := json.Marshal(choicesPost.Choices) choicesJson, err := json.Marshal(choicesPost.Choices)
@@ -164,6 +178,13 @@ func main() {
handleGetPing, handleGetPing,
) )
http.HandleFunc(
"GET /favicon.ico",
func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "There is no favicon.ico!", http.StatusBadRequest)
},
)
http.HandleFunc( http.HandleFunc(
"GET /{pollId}", "GET /{pollId}",
handleGetRoot, handleGetRoot,
+1 -1
View File
@@ -115,7 +115,7 @@
headers: { headers: {
"Content-Type": "application/json", // Inform the server about the data format "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 body: JSON.stringify({pollId: {{ .PollId }}, username: submitName.value, choices: choices}), // Convert the data to JSON string
}) })
/* /*
.then(response => { .then(response => {