Add executeTemplate function

This commit is contained in:
Lucas Schumacher 2024-07-31 21:16:06 -04:00
parent 4e2ad80fdf
commit aac5e45b3f

24
main.go
View File

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