go.tools/cmd/blog: handle "/blog/" redirect properly

The redirect.PrefixHandler redirects "/foo/" to "/foo", which is what
we want in most cases ("/cl/", "/change/", etc) but not here.
So wrap it with a handler that handles "/blog/" explictly.

R=dsymonds
CC=golang-dev
https://golang.org/cl/14326043
This commit is contained in:
Andrew Gerrand 2013-10-03 18:10:57 +10:00
parent 228e3cbbaa
commit dcad628c68
1 changed files with 16 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"code.google.com/p/go.tools/blog"
@ -21,6 +22,7 @@ import (
const (
blogRepo = "code.google.com/p/go.blog"
blogURL = "http://blog.golang.org/"
blogPath = "/blog/"
)
var (
@ -31,7 +33,7 @@ var (
func init() {
// Initialize blog only when first accessed.
http.HandleFunc("/blog/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc(blogPath, func(w http.ResponseWriter, r *http.Request) {
blogInitOnce.Do(blogInit)
blogServer.ServeHTTP(w, r)
})
@ -50,13 +52,13 @@ func blogInit() {
if fi, err := os.Stat(root); err != nil || !fi.IsDir() {
fmt.Fprintf(os.Stderr, "Blog content not available locally. "+
"To install, run \n\tgo get %v\n", blogRepo)
blogServer = redirect.PrefixHandler("/blog/", blogURL)
blogServer = http.HandlerFunc(blogRedirectHandler)
return
}
s, err := blog.NewServer(blog.Config{
BaseURL: "/blog/",
BasePath: "/blog",
BaseURL: blogPath,
BasePath: strings.TrimSuffix(blogPath, "/"),
ContentPath: filepath.Join(root, "content"),
TemplatePath: filepath.Join(root, "template"),
HomeArticles: 5,
@ -67,3 +69,13 @@ func blogInit() {
}
blogServer = s
}
func blogRedirectHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == blogPath {
http.Redirect(w, r, blogURL, http.StatusFound)
return
}
blogPrefixHandler.ServeHTTP(w, r)
}
var blogPrefixHandler = redirect.PrefixHandler(blogPath, blogURL)