From 200cf0ee2c9de24e08df8c76345bedef85cbb461 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 5 May 2020 19:41:26 +1200 Subject: [PATCH] automatic support for go get on deep subpackages --- main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/main.go b/main.go index e12884a..ec69fab 100644 --- a/main.go +++ b/main.go @@ -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) }