mirror of
https://github.com/golang/go
synced 2024-11-18 14:44:41 -07:00
go.tools/godoc: provide an explicit mux to register redirects
R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/14199043
This commit is contained in:
parent
a32c2633d0
commit
401293d22c
@ -18,7 +18,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"code.google.com/p/go.tools/godoc"
|
"code.google.com/p/go.tools/godoc"
|
||||||
_ "code.google.com/p/go.tools/godoc/redirect"
|
"code.google.com/p/go.tools/godoc/redirect"
|
||||||
"code.google.com/p/go.tools/godoc/vfs"
|
"code.google.com/p/go.tools/godoc/vfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,6 +35,7 @@ func registerHandlers(pres *godoc.Presentation) {
|
|||||||
http.Handle("/doc/play/", pres.FileServer())
|
http.Handle("/doc/play/", pres.FileServer())
|
||||||
http.Handle("/robots.txt", pres.FileServer())
|
http.Handle("/robots.txt", pres.FileServer())
|
||||||
http.Handle("/", pres)
|
http.Handle("/", pres)
|
||||||
|
redirect.Register(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readTemplate(name string) *template.Template {
|
func readTemplate(name string) *template.Template {
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package redirect registers HTTP handlers that redirect old godoc paths to
|
// Package redirect provides hooks to register HTTP handlers that redirect old
|
||||||
// their new equivalents and assist in accessing the issue tracker, wiki, code
|
// godoc paths to their new equivalents and assist in accessing the issue
|
||||||
// review system, etc.
|
// tracker, wiki, code review system, etc.
|
||||||
package redirect
|
package redirect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -12,17 +12,30 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
// Register registers HTTP handlers that redirect old godoc paths to their new
|
||||||
handlePathRedirects(pkgRedirects, "/pkg/")
|
// equivalents and assist in accessing the issue tracker, wiki, code review
|
||||||
handlePathRedirects(cmdRedirects, "/cmd/")
|
// system, etc. If mux is nil it uses http.DefaultServeMux.
|
||||||
|
func Register(mux *http.ServeMux) {
|
||||||
|
if mux == nil {
|
||||||
|
mux = http.DefaultServeMux
|
||||||
|
}
|
||||||
|
handlePathRedirects(mux, pkgRedirects, "/pkg/")
|
||||||
|
handlePathRedirects(mux, cmdRedirects, "/cmd/")
|
||||||
for prefix, redirect := range prefixHelpers {
|
for prefix, redirect := range prefixHelpers {
|
||||||
p := "/" + prefix + "/"
|
p := "/" + prefix + "/"
|
||||||
h := PrefixHandler(p, redirect)
|
mux.Handle(p, PrefixHandler(p, redirect))
|
||||||
http.HandleFunc(p, h)
|
|
||||||
}
|
}
|
||||||
for path, redirect := range redirects {
|
for path, redirect := range redirects {
|
||||||
h := Handler(redirect)
|
mux.Handle(path, Handler(redirect))
|
||||||
http.HandleFunc(path, h)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePathRedirects(mux *http.ServeMux, redirects map[string]string, prefix string) {
|
||||||
|
for source, target := range redirects {
|
||||||
|
h := Handler(prefix + target + "/")
|
||||||
|
p := prefix + source
|
||||||
|
mux.Handle(p, h)
|
||||||
|
mux.Handle(p+"/", h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,25 +125,16 @@ var prefixHelpers = map[string]string{
|
|||||||
"wiki": "https://code.google.com/p/go-wiki/wiki/",
|
"wiki": "https://code.google.com/p/go-wiki/wiki/",
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePathRedirects(redirects map[string]string, prefix string) {
|
func Handler(target string) http.Handler {
|
||||||
for source, target := range pkgRedirects {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
h := Handler(prefix + target + "/")
|
|
||||||
p := prefix + source
|
|
||||||
http.HandleFunc(p, h)
|
|
||||||
http.HandleFunc(p+"/", h)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Handler(target string) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
http.Redirect(w, r, target, http.StatusMovedPermanently)
|
http.Redirect(w, r, target, http.StatusMovedPermanently)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var validId = regexp.MustCompile(`^[A-Za-z0-9-]*$`)
|
var validId = regexp.MustCompile(`^[A-Za-z0-9-]*$`)
|
||||||
|
|
||||||
func PrefixHandler(prefix, baseURL string) http.HandlerFunc {
|
func PrefixHandler(prefix, baseURL string) http.Handler {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if p := r.URL.Path; p == prefix {
|
if p := r.URL.Path; p == prefix {
|
||||||
// redirect /prefix/ to /prefix
|
// redirect /prefix/ to /prefix
|
||||||
http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
|
http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
|
||||||
@ -143,5 +147,5 @@ func PrefixHandler(prefix, baseURL string) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
target := baseURL + id
|
target := baseURL + id
|
||||||
http.Redirect(w, r, target, http.StatusFound)
|
http.Redirect(w, r, target, http.StatusFound)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user