Check for invalid templates at server startup

This commit is contained in:
Lucas Schumacher 2024-08-01 00:10:44 -04:00
parent 54313fe21b
commit 5adfeefa33
2 changed files with 58 additions and 31 deletions

46
main.go
View File

@ -2,42 +2,26 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
"git.kealoha.me/lks/lenslocked/views"
) )
func executeTemplate(w http.ResponseWriter, filepath string) { func addStaticTemplate(r chi.Router, pattern string, filepath string) {
w.Header().Set("Content-Type", "text/html; charset=utf8") tpl := views.Must(views.FromFile(filepath))
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 homeHandler(w http.ResponseWriter, r *http.Request) { var testWriter strings.Builder
path := filepath.Join("templates", "home.gohtml") err := tpl.ExecuteWriter(&testWriter, nil)
executeTemplate(w, path) if err != nil {
} panic(err)
func contactHandler(w http.ResponseWriter, r *http.Request) { }
path := filepath.Join("templates", "contact.gohtml")
executeTemplate(w, path) r.Get(pattern, func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) })
}
func faqHandler(w http.ResponseWriter, r *http.Request) {
path := filepath.Join("templates", "faq.gohtml")
executeTemplate(w, path)
} }
func notFoundHandler(w http.ResponseWriter, r *http.Request) { func notFoundHandler(w http.ResponseWriter, r *http.Request) {
@ -49,9 +33,9 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(middleware.Logger) r.Use(middleware.Logger)
r.Get("/", homeHandler) addStaticTemplate(r, "/", filepath.Join("templates", "home.gohtml"))
r.Get("/contact", contactHandler) addStaticTemplate(r, "/contact", filepath.Join("templates", "contact.gohtml"))
r.Get("/faq", faqHandler) addStaticTemplate(r, "/faq", filepath.Join("templates", "faq.gohtml"))
r.NotFound(notFoundHandler) r.NotFound(notFoundHandler)
fmt.Println("Starting the server on :3000...") fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r) http.ListenAndServe(":3000", r)

43
views/template.go Normal file
View File

@ -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
}