diff --git a/main.go b/main.go index ec9c806..c760a5e 100644 --- a/main.go +++ b/main.go @@ -2,42 +2,26 @@ package main import ( "fmt" - "html/template" - "log" "net/http" "path/filepath" + "strings" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + + "git.kealoha.me/lks/lenslocked/views" ) -func executeTemplate(w http.ResponseWriter, filepath string) { - w.Header().Set("Content-Type", "text/html; charset=utf8") - tpl, err := template.ParseFiles(filepath) - 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 - } -} +func addStaticTemplate(r chi.Router, pattern string, filepath string) { + tpl := views.Must(views.FromFile(filepath)) -func homeHandler(w http.ResponseWriter, r *http.Request) { - path := filepath.Join("templates", "home.gohtml") - executeTemplate(w, path) -} -func contactHandler(w http.ResponseWriter, r *http.Request) { - path := filepath.Join("templates", "contact.gohtml") - executeTemplate(w, path) -} -func faqHandler(w http.ResponseWriter, r *http.Request) { - path := filepath.Join("templates", "faq.gohtml") - executeTemplate(w, path) + var testWriter strings.Builder + err := tpl.ExecuteWriter(&testWriter, nil) + if err != nil { + panic(err) + } + + r.Get(pattern, func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }) } func notFoundHandler(w http.ResponseWriter, r *http.Request) { @@ -49,9 +33,9 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) { func main() { r := chi.NewRouter() r.Use(middleware.Logger) - r.Get("/", homeHandler) - r.Get("/contact", contactHandler) - r.Get("/faq", faqHandler) + addStaticTemplate(r, "/", filepath.Join("templates", "home.gohtml")) + addStaticTemplate(r, "/contact", filepath.Join("templates", "contact.gohtml")) + addStaticTemplate(r, "/faq", filepath.Join("templates", "faq.gohtml")) r.NotFound(notFoundHandler) fmt.Println("Starting the server on :3000...") http.ListenAndServe(":3000", r) diff --git a/views/template.go b/views/template.go new file mode 100644 index 0000000..4688e3d --- /dev/null +++ b/views/template.go @@ -0,0 +1,43 @@ +package views + +import ( + "fmt" + "html/template" + "io" + "log" + "net/http" +) + +type Template struct { + htmlTpl *template.Template +} + +func (t Template) Execute(w http.ResponseWriter, data interface{}) { + w.Header().Set("Content-Type", "text/html; charset=utf8") + err := t.htmlTpl.Execute(w, data) + if err != nil { + log.Printf("Error executing template: %v", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } +} +func (t Template) ExecuteWriter(w io.Writer, data interface{}) error { + return t.htmlTpl.Execute(w, data) +} + +func FromFile(filepath string) (Template, error) { + tpl, err := template.ParseFiles(filepath) + if err != nil { + return Template{}, fmt.Errorf("Error parsing template: %v", err) + } + return Template{ + htmlTpl: tpl, + }, nil +} + +func Must(t Template, err error) Template { + if err != nil { + panic(err) + } + return t +}