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