From a3d5f547d3d9fd965e36fcf21081eb8a8a6c9052 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Fri, 24 Jun 2016 11:16:58 +1000 Subject: [PATCH] Cache templates --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index c265f83..d2e567f 100644 --- a/main.go +++ b/main.go @@ -226,6 +226,9 @@ func getPaste(paste string, lang string) string { } +var templates = template.Must(template.ParseFiles("assets/paste.html")) +var syntax, _ = ioutil.ReadFile("assets/syntax.html") + func pasteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) paste := vars["pasteId"] @@ -239,16 +242,13 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { Raw: link, Home: ADDRESS, } - t, err := template.ParseFiles("assets/paste.html") + err := templates.ExecuteTemplate(w, "assets/paste.html", p) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } - t.Execute(w, p) } else { - dat, err := ioutil.ReadFile("assets/syntax.html") - check(err) - fmt.Fprintf(w, string(dat), paste, paste, s, ADDRESS, link) + fmt.Fprintf(w, string(syntax), paste, paste, s, ADDRESS, link) } }