From c072558b90f1bbedc2022b0f30c8b1ac4712538e Mon Sep 17 00:00:00 2001
From: Ondřej Surý
-func viewHandler(w http.ResponseWriter, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) + if err != nil { + return + } p, err := loadPage(title) if err != nil { http.Redirect(w, r, "/edit/"+title, http.StatusFound) @@ -658,10 +662,14 @@ Now let's fix upsaveHandler
:-func saveHandler(w http.ResponseWriter, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) + if err != nil { + return + } body := r.FormValue("body") p := &Page{Title: title, Body: []byte(body)} - err := p.save() + err = p.save() if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return @@ -702,7 +710,7 @@ Then we create aninit
function, which will be called beforeParseFile
that does not return an error code; instead, it panics if an error is encountered. A panic is appropriate here; if the templates can't be loaded the only sensible thing to do is exit the program. -func init() { @@ -726,7 +734,7 @@ theExecute
method on the appropriateTemplate
fromfunc renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - err := templates[tmpl].Execute(p, w) + err := templates[tmpl].Execute(w, p) if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) } @@ -747,6 +755,7 @@ Then we can create a global variable to store our validation regexp:+var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")-- cgit v1.2.3