Refactor static template helper func

This commit is contained in:
Lucas Schumacher 2024-08-01 09:37:59 -04:00
parent 9a59acfd2d
commit 140230d89b

10
main.go
View File

@ -12,7 +12,7 @@ import (
"git.kealoha.me/lks/lenslocked/views"
)
func addStaticTemplate(r chi.Router, pattern string, templatePath string) {
func parseStaticTemplate(templatePath string) http.HandlerFunc {
tpl := views.Must(views.FromFS(templates.FS, templatePath))
var testWriter strings.Builder
@ -21,7 +21,7 @@ func addStaticTemplate(r chi.Router, pattern string, templatePath string) {
panic(err)
}
r.Get(pattern, func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) })
return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
@ -33,9 +33,9 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
addStaticTemplate(r, "/", "home.gohtml")
addStaticTemplate(r, "/contact", "contact.gohtml")
addStaticTemplate(r, "/faq", "faq.gohtml")
r.Get("/", parseStaticTemplate("home.gohtml"))
r.Get("/contact", parseStaticTemplate("contact.gohtml"))
r.Get("/faq", parseStaticTemplate("faq.gohtml"))
r.NotFound(notFoundHandler)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)