all: index.html
-CLEANFILES:=srcextract.bin htmlify.bin get.bin
-
-index.html: wiki.html srcextract.bin htmlify.bin
- PATH=.:$$PATH awk '/^!/{system(substr($$0,2)); next} {print}' < wiki.html | tr -d '\r' > index.html
-
-test: get.bin
- bash ./test.sh
- rm -f get.6 get.bin
-
-%.bin: %.go
- go build -o $@ $^
+CLEANFILES:=get.bin final-test.bin a.out
clean:
rm -f $(CLEANFILES)
}
}
-const lenPath = len("/view/")
+var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
-var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
-
-func getTitle(w http.ResponseWriter, r *http.Request) (title string, err error) {
- title = r.URL.Path[lenPath:]
- if !titleValidator.MatchString(title) {
+func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
+ m := validPath.FindStringSubmatch(r.URL.Path)
+ if m == nil {
http.NotFound(w, r)
- err = errors.New("Invalid Page Title")
+ return "", errors.New("Invalid Page Title")
}
- return
+ return m[2], nil // The title is the second subexpression.
}
func main() {
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func editHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
t, _ := template.ParseFiles("view.html")
t.Execute(w, p)
}
}
-const lenPath = len("/view/")
-
-var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
+var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
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) {
+ m := validPath.FindStringSubmatch(r.URL.Path)
+ if m == nil {
http.NotFound(w, r)
return
}
- fn(w, r, title)
+ fn(w, r, m[2])
}
}
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func editHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
renderTemplate(w, "view", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/save/"):]
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
p.save()
}
}
-const lenPath = len("/view/")
-
-var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
+var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
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) {
+ m := validPath.FindStringSubmatch(r.URL.Path)
+ if m == nil {
http.NotFound(w, r)
return
}
- fn(w, r, title)
+ fn(w, r, m[2])
}
}
+++ /dev/null
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package main
-
-import (
- "io/ioutil"
- "os"
- "text/template"
-)
-
-func main() {
- b, _ := ioutil.ReadAll(os.Stdin)
- template.HTMLEscape(os.Stdout, b)
-}
view a wiki page. It will handle URLs prefixed with "/view/".
</p>
-{{code "doc/articles/wiki/part2.go" `/^const lenPath/`}}
-
{{code "doc/articles/wiki/part2.go" `/^func viewHandler/` `/^}/`}}
<p>
First, this function extracts the page title from <code>r.URL.Path</code>,
-the path component of the request URL. The global constant
-<code>lenPath</code> is the length of the leading <code>"/view/"</code>
-component of the request path.
-The <code>Path</code> is re-sliced with <code>[lenPath:]</code> to drop the
-first 6 characters of the string. This is because the path will invariably
-begin with <code>"/view/"</code>, which is not part of the page's title.
+the path component of the request URL.
+The <code>Path</code> is re-sliced with <code>[len("/view/"):]</code> to drop
+the leading <code>"/view/"</code> component of the request path.
+This is because the path will invariably begin with <code>"/view/"</code>,
+which is not part of the page's title.
</p>
<p>
</p>
{{code "doc/articles/wiki/final-template.go" `/^func renderTemplate/` `/^}/`}}
+
+<p>
+And modify the handlers to use that function:
+</p>
+
{{code "doc/articles/wiki/final-template.go" `/^func viewHandler/` `/^}/`}}
{{code "doc/articles/wiki/final-template.go" `/^func editHandler/` `/^}/`}}
<p>
First, add <code>"regexp"</code> to the <code>import</code> list.
-Then we can create a global variable to store our validation regexp:
+Then we can create a global variable to store our validation
+expression:
</p>
-{{code "doc/articles/wiki/final-noclosure.go" `/^var titleValidator/`}}
+{{code "doc/articles/wiki/final-noclosure.go" `/^var validPath/`}}
<p>
The function <code>regexp.MustCompile</code> will parse and compile the
</p>
<p>
-Now, let's write a function, <code>getTitle</code>, that extracts the title
-string from the request URL, and tests it against our
-<code>TitleValidator</code> expression:
+Now, let's write a function that uses the <code>validPath</code>
+expression to validate path and extract the page title:
</p>
{{code "doc/articles/wiki/final-noclosure.go" `/func getTitle/` `/^}/`}}
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, _ := template.ParseFiles(tmpl + ".html")
t.Execute(w, p)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/save/"):]
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err := p.save()
return &Page{Title: title, Body: body}, nil
}
-const lenPath = len("/view/")
-
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, _ := template.ParseFiles(tmpl + ".html")
t.Execute(w, p)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
- title := r.URL.Path[lenPath:]
+ title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
+++ /dev/null
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package main
-
-import (
- "bytes"
- "flag"
- "go/ast"
- "go/parser"
- "go/printer"
- "go/token"
- "log"
- "os"
- "text/template"
-)
-
-var (
- srcFn = flag.String("src", "", "source filename")
- getName = flag.String("name", "", "func/type name to output")
- html = flag.Bool("html", true, "output HTML")
- showPkg = flag.Bool("pkg", false, "show package in output")
-)
-
-func main() {
- // handle input
- flag.Parse()
- if *srcFn == "" || *getName == "" {
- flag.Usage()
- os.Exit(2)
- }
- // load file
- fs := token.NewFileSet()
- file, err := parser.ParseFile(fs, *srcFn, nil, 0)
- if err != nil {
- log.Fatal(err)
- }
- // create filter
- filter := func(name string) bool {
- return name == *getName
- }
- // filter
- if !ast.FilterFile(file, filter) {
- os.Exit(1)
- }
- // print the AST
- var b bytes.Buffer
- printer.Fprint(&b, fs, file)
- // drop package declaration
- if !*showPkg {
- for {
- c, err := b.ReadByte()
- if c == '\n' || err != nil {
- break
- }
- }
- }
- // drop leading newlines
- for {
- b, err := b.ReadByte()
- if err != nil {
- break
- }
- if b != '\n' {
- os.Stdout.Write([]byte{b})
- break
- }
- }
- // output
- if *html {
- template.HTMLEscape(os.Stdout, b.Bytes())
- } else {
- b.WriteTo(os.Stdout)
- }
-}
wiki_pid=
cleanup() {
kill $wiki_pid
- rm -f test_*.out Test.txt final-test.bin final-test.go
+ rm -f test_*.out Test.txt final-test.bin final-test.go a.out get.bin
}
trap cleanup 0 INT
+# If called with -all, check that all code snippets compile.
+if [ "$1" == "-all" ]; then
+ for fn in *.go; do
+ go build -o a.out $fn
+ done
+fi
+
go build -o get.bin get.go
addr=$(./get.bin -addr)
sed s/:8080/$addr/ < final.go > final-test.go