automatic support for go get on deep subpackages

This commit is contained in:
mappu 2020-05-05 19:41:26 +12:00
parent 28e7e769b1
commit 200cf0ee2c

13
main.go
View File

@ -342,6 +342,19 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = r.URL.Path[8:]
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
} else if r.URL.Query().Get("go-get") == "1" {
// This wasn't one of our standard `/repo` paths, but there is the ?go-get=1 parameter
// It must be a subpackage request
// We can't serve the proper go-import meta tag immediately because
// we haven't looked up the go.mod yet. Just redirect to the root
// package - `go get` will follow redirects and the resulting meta tag is correct
slashParts := strings.SplitN(r.URL.Path, `/`, 3) // len === 3 is guaranteed from earlier if cases
w.Header().Set(`Location`, `/`+slashParts[1])
w.WriteHeader(301)
return
} else {
http.Error(w, "not found", 404)
}