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"
"log"
"net/http"
"strconv"
_ "github.com/mattn/go-sqlite3"
)
@@ -71,22 +72,34 @@ func prepareStaticContent() {
}
func handleGetRoot(w http.ResponseWriter, r *http.Request) {
var pollId = r.PathValue("pollId")
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 = `
SELECT name
FROM polls
WHERE id = ?;
`
err := db.QueryRow(query, pollId).Scan(&pollName)
err = db.QueryRow(query, pollId).Scan(&pollName)
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)
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 {
log.Printf("[!] Could not execute index.html template, error: %s\n", err)
http.Error(w, "Error rendering template", http.StatusInternalServerError)
@@ -112,6 +125,7 @@ func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
w.WriteHeader(http.StatusBadRequest)
return
}
choicesJson, err := json.Marshal(choicesPost.Choices)
@@ -164,6 +178,13 @@ func main() {
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(
"GET /{pollId}",
handleGetRoot,