Check for invalid templates at server startup
This commit is contained in:
parent
54313fe21b
commit
5adfeefa33
42
main.go
42
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)
|
||||
func addStaticTemplate(r chi.Router, pattern string, filepath string) {
|
||||
tpl := views.Must(views.FromFile(filepath))
|
||||
|
||||
var testWriter strings.Builder
|
||||
err := tpl.ExecuteWriter(&testWriter, nil)
|
||||
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
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
43
views/template.go
Normal file
43
views/template.go
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user