Within template.go, we can add the following “Must” fuction:
func Must(t Template, err error) Template {
if err != nil {
panic(err)
}
return t
}
That will allow us to call it in main.go, like this:
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/rabocse/lenslocked/controllers"
"github.com/rabocse/lenslocked/views"
)
func main() {
r := chi.NewRouter()
r.Get("/", controllers.StaticHandler(views.Must(views.Parse(filepath.Join("templates", "home.gohtml")))))
r.Get("/contact", controllers.StaticHandler(views.Must(views.Parse(filepath.Join("templates", "contact.gohtml")))))
r.Get("/faq", controllers.StaticHandler(views.Must(views.Parse(filepath.Join("templates", "faq.gohtml")))))
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Page Not Found", http.StatusNotFound)
})
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)
}