From aac5e45b3f90f3828ef5b7cf226d010c113bd0f0 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Wed, 31 Jul 2024 21:16:06 -0400 Subject: [PATCH] Add executeTemplate function --- main.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 12cb4e6..557c67e 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,9 @@ import ( "github.com/go-chi/chi/v5" ) -func homeHandler(w http.ResponseWriter, r *http.Request) { +func executeTemplate(w http.ResponseWriter, filepath string) { w.Header().Set("Content-Type", "text/html; charset=utf8") - tpl, err := template.ParseFiles("templates/home.gohtml") + tpl, err := template.ParseFiles(filepath) if err != nil { log.Printf("Error parsing template: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) @@ -24,20 +24,14 @@ func homeHandler(w http.ResponseWriter, r *http.Request) { return } } + +func homeHandler(w http.ResponseWriter, r *http.Request) { + path := "templates/home.gohtml" + executeTemplate(w, path) +} func contactHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf8") - tpl, err := template.ParseFiles("templates/contact.gohtml") - if err != nil { - log.Printf("Error parsing template: %v", err) - http.Error(w, "Internal Server Error", http.StatusInternalServerError) - return - } - err = tpl.Execute(w, nil) - if err != nil { - log.Printf("Error executing template: %v", err) - http.Error(w, "Internal Server Error", http.StatusInternalServerError) - return - } + path := "templates/contact.gohtml" + executeTemplate(w, path) } func faqHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf8")