Refactor sub package redirect

Redirect github.com/user/pkg/sub -> github.com/user/pkg

git-svn-id: file:///srv/svn/repo/toyohime/trunk@69 922d331f-388e-da47-97a9-ad700dc0b8b9
This commit is contained in:
kare.nuorteva 2018-01-11 13:50:49 +00:00
parent 55652d606f
commit c5a903a3fb
2 changed files with 17 additions and 19 deletions

View File

@ -37,34 +37,31 @@ func Redirect(vcs, importPath, repoRoot string) http.Handler {
return
}
path := strings.TrimSuffix(r.URL.Path, "/")
if !strings.HasPrefix(strings.TrimSuffix(r.Host+r.URL.Path, "/"), importPath+"/") {
http.NotFound(w, r)
return
}
vcsroot := repoRoot
if strings.HasPrefix(r.URL.Path, "/cmd/") {
path = r.URL.Path[4:]
} else {
path = r.URL.Path
}
shortPath := strings.Split(path, "/")
f := func(c rune) bool {
return c == '/'
}
shortPath = strings.FieldsFunc(path, f)
if (len(shortPath) > 0) {
vcsroot = repoRoot + "/" + shortPath[0]
}
if r.FormValue("go-get") != "1" {
url := "https://godoc.org/" + r.Host + r.URL.Path
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return
}
var path string
if strings.HasPrefix(r.URL.Path, "/cmd/") {
path = r.URL.Path[4:]
} else {
path = r.URL.Path
}
// redirect github.com/kare/pkg/sub -> github.com/kare/pkg
vcsroot := repoRoot
f := func(c rune) bool { return c == '/' }
shortPath := strings.FieldsFunc(path, f)
if len(shortPath) > 0 {
vcsroot = repoRoot + "/" + shortPath[0]
}
d := &data{
ImportRoot: r.Host + r.URL.Path,
VCS: vcs,

View File

@ -116,6 +116,7 @@ func TestBrowserGoDoc(t *testing.T) {
{"/set", "https://godoc.org/kkn.fi/set"},
{"/cmd/vanity", "https://godoc.org/kkn.fi/cmd/vanity"},
{"/cmd/tcpproxy", "https://godoc.org/kkn.fi/cmd/tcpproxy"},
{"/pkgabc/sub/foo", "https://godoc.org/kkn.fi/pkgabc/sub"},
}
for _, test := range tests {
res := httptest.NewRecorder()