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")