From a2332a32b8c6eb3a71113ad86edffda709a59f4a Mon Sep 17 00:00:00 2001
From: Stephen Ma
The function
-An
The function then loads the page data, formats the page with a string of simple
-HTML, and writes it to
@@ -406,13 +406,13 @@ and displays an HTML form.
handler
is of the type http.HandlerFunc
.
-It takes an http.Conn
and http.Request
as its
-arguments.
+It takes an http.ResponseWriter
and an http.Request
as
+its arguments.
http.Conn
is the server end of an HTTP connection; by writing
+An http.ResponseWriter
value assembles the HTTP server's response; by writing
to it, we send data to the HTTP client.
const lenPath = len("/view/")
-func viewHandler(c *http.Conn, r *http.Request) {
+func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, _ := loadPage(title)
- fmt.Fprintf(c, "<h1>%s</h1><div>%s</div>", p.title, p.body)
+ fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.title, p.body)
}
@@ -333,7 +333,7 @@ begin with "/view/"
, which is not part of the page title.
c
, the http.Conn
.
+HTML, and writes it to w
, the http.ResponseWriter
.
-func editHandler(c *http.Conn, r *http.Request) {
+func editHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
p = &page{title: title}
}
- fmt.Fprintf(c, "<h1>Editing %s</h1>"+
+ fmt.Fprintf(w, "<h1>Editing %s</h1>"+
"<form action=\"/save/%s\" method=\"POST\">"+
"<textarea name=\"body\">%s</textarea><br>"+
"<input type=\"submit\" value=\"Save\">"+
@@ -468,14 +468,14 @@ HTML:
-func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } t, _ := template.ParseFile("edit.html", nil) - t.Execute(p, c) + t.Execute(p, w) }@@ -488,7 +488,7 @@ The function
template.ParseFile
will read the contents of
The method t.Execute
replaces all occurrences of
{title}
and {body}
with the values of
p.title
and p.body
, and writes the resultant
-HTML to the http.Conn
.
+HTML to the http.ResponseWriter
.
@@ -523,11 +523,11 @@ Modify viewHandler
accordingly:
-func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) t, _ := template.ParseFile("view.html", nil) - t.Execute(p, c) + t.Execute(p, w) }@@ -538,24 +538,24 @@ to its own function:
-func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func renderTemplate(c *http.Conn, tmpl string, p *page) { +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { t, _ := template.ParseFile(tmpl+".html", nil) - t.Execute(p, c) + t.Execute(p, w) }@@ -573,13 +573,13 @@ redirect the client to the edit page so the content may be created:
-func viewHandler(c *http.Conn, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { - http.Redirect(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) }@@ -596,12 +596,12 @@ The function
saveHandler
will handle the form submission.
-func saveHandler(c *http.Conn, r *http.Request) { +func saveHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] body := r.FormValue("body") p := &page{title: title, body: []byte(body)} p.save() - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) }@@ -634,15 +634,15 @@ First, let's handle the errors in
renderTemplate
:
-func renderTemplate(c *http.Conn, tmpl string, p *page) { +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { t, err := template.ParseFile(tmpl+".html", nil) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - err = t.Execute(p, c) + err = t.Execute(p, w) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) } }@@ -658,15 +658,15 @@ Now let's fix up
saveHandler
:
-func saveHandler(c *http.Conn, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request, title string) { body := r.FormValue("body") p := &page{title: title, body: []byte(body)} err := p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) }@@ -725,10 +725,10 @@ the
Execute
method on the appropriate Template
from
templates
:
-func renderTemplate(c *http.Conn, tmpl string, p *page) { - err := templates[tmpl].Execute(p, c) +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { + err := templates[tmpl].Execute(p, w) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) } }@@ -765,10 +765,10 @@ URL, and tests it against our
titleValidator
expression:
-func getTitle(c *http.Conn, r *http.Request) (title string, err os.Error) { +func getTitle(w http.ResponseWriter, r *http.Request) (title string, err os.Error) { title = r.URL.Path[lenPath:] if !titleValidator.MatchString(title) { - http.NotFound(c, r) + http.NotFound(w, r) err = os.NewError("Invalid Page Title") } return @@ -787,21 +787,21 @@ Let's put a call togetTitle
in each of the handlers:-func viewHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +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(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +func editHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) if err != nil { return } @@ -809,11 +809,11 @@ func editHandler(c *http.Conn, r *http.Request) { if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func saveHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +func saveHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) if err != nil { return } @@ -821,10 +821,10 @@ func saveHandler(c *http.Conn, r *http.Request) { p := &page{title: title, body: []byte(body)} err = p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) }@@ -845,9 +845,9 @@ a title string:-func viewHandler(c *http.Conn, r *http.Request, title string) -func editHandler(c *http.Conn, r *http.Request, title string) -func saveHandler(c *http.Conn, r *http.Request, title string) +func viewHandler(w http.ResponseWriter, r *http.Request, title string) +func editHandler(w http.ResponseWriter, r *http.Request, title string) +func saveHandler(w http.ResponseWriter, r *http.Request, title string)@@ -857,8 +857,8 @@ type, and returns a function of type
http.HandlerFunc
-func makeHandler(fn func (*http.Conn, *http.Request, string)) http.HandlerFunc { - return func(c *http.Conn, r *http.Request) { +func makeHandler(fn func (http.ResponseWriter, *http.Request, string)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { // Here we will extract the page title from the Request, // and call the provided handler 'fn' } @@ -878,28 +878,28 @@ Now we can take the code fromgetTitle
and use it here-func makeHandler(fn func(*http.Conn, *http.Request, string)) http.HandlerFunc { - return func(c *http.Conn, r *http.Request) { +func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] if !titleValidator.MatchString(title) { - http.NotFound(c, r) + http.NotFound(w, r) return } - fn(c, r, title) + fn(w, r, title) } }The closure returned by
@@ -924,32 +924,32 @@ making them much simpler:makeHandler
is a function that takes -anhttp.Conn
andhttp.Request
(in other words, -anhttp.HandlerFunc
). +anhttp.ResponseWriter
andhttp.Request
(in other +words, anhttp.HandlerFunc
). The closure extracts thetitle
from the request path, and validates it with thetitleValidator
regexp. If thetitle
is invalid, an error will be written to the -Conn
using thehttp.NotFound
function. +ResponseWriter
using thehttp.NotFound
function. If thetitle
is valid, the enclosed handler function -fn
will be called with theConn
, +fn
will be called with theResponseWriter
,Request
, andtitle
as arguments.-func viewHandler(c *http.Conn, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { - http.Redirect(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request, title string) { +func editHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func saveHandler(c *http.Conn, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request, title string) { body := r.FormValue("body") p := &page{title: title, body: []byte(body)} err := p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) }diff --git a/doc/codelab/wiki/notemplate.go b/doc/codelab/wiki/notemplate.go index a61d905e39..c1f952c838 100644 --- a/doc/codelab/wiki/notemplate.go +++ b/doc/codelab/wiki/notemplate.go @@ -28,19 +28,19 @@ func loadPage(title string) (*page, os.Error) { const lenPath = len("/view/") -func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - fmt.Fprintf(c, "%s
%s", p.title, p.body) + fmt.Fprintf(w, "%s
%s", p.title, p.body) } -func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } - fmt.Fprintf(c, "Editing %s
"+ + fmt.Fprintf(w, "Editing %s
"+ "