backend: frontend: cleaned up the codebase and added more logging for debuging
This commit is contained in:
parent
faf7c2f782
commit
3a77eb1593
11 changed files with 152 additions and 38 deletions
|
|
@ -8,25 +8,48 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
// FormHandler returns an http.HandlerFunc that handles form submissions.
|
||||
// It takes the content embed.FS, a pointer to a slice of model.Monster,
|
||||
// and a filename string as parameters.
|
||||
// The function parses the template files from the content FS,
|
||||
// executes the template with the provided data, and renders it as a response.
|
||||
func FormHandler(content embed.FS, monsters *[]model.Monster, filename string) http.HandlerFunc {
|
||||
log.Print("FormHandler called")
|
||||
|
||||
// Lock the mutex to ensure exclusive access to the monsters slice.
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterForm.html", "templates/monster.html", "templates/monsterTable.html")
|
||||
log.Print("FormHandler handler called")
|
||||
|
||||
// Parse the template files.
|
||||
templateFiles := []string{
|
||||
"templates/base.html",
|
||||
"templates/header.html",
|
||||
"templates/main.html",
|
||||
"templates/footer.html",
|
||||
"templates/monsterForm.html",
|
||||
"templates/monster.html",
|
||||
"templates/monsterTable.html",
|
||||
}
|
||||
tmpl, err := template.ParseFS(content, templateFiles...)
|
||||
if err != nil {
|
||||
log.Printf("Template parsing error: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = tmpl.ExecuteTemplate(w, "base", map[string]interface{}{
|
||||
// Execute the template and render the response.
|
||||
data := map[string]interface{}{
|
||||
"Title": "Dungeons & Dragons Monster Generator",
|
||||
"Monsters": *monsters,
|
||||
})
|
||||
}
|
||||
err = tmpl.ExecuteTemplate(w, "base", data)
|
||||
if err != nil {
|
||||
log.Printf("Template execution error: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
log.Printf("Template mit %d Monstern gerendert\n", len(*monsters))
|
||||
log.Printf("Template rendered with %d Monsters\n", len(*monsters))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue