mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-20 17:24:16 +00:00
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"html"
|
|
"net/http"
|
|
)
|
|
|
|
// GoImport serves the `<meta name="go-import">` tag required by `go install` /
|
|
// `go get` to resolve the vanity path `atcr.io/...` to the source repository.
|
|
//
|
|
// Go tooling requests `https://atcr.io/<subpath>?go-get=1` and expects an HTML
|
|
// document with a meta tag of the form:
|
|
//
|
|
// <meta name="go-import" content="<root> <vcs> <repo-url>">
|
|
//
|
|
// The meta tag must be present on every subpath under the module root, so this
|
|
// runs as middleware at the top of the chain and short-circuits any request
|
|
// carrying `?go-get=1`.
|
|
func GoImport(modulePath, repoURL string) func(http.Handler) http.Handler {
|
|
body := fmt.Sprintf(
|
|
`<!DOCTYPE html><html><head><meta name="go-import" content="%s git %s"><meta name="go-source" content="%s %s %s/tree/main{/dir} %s/tree/main{/dir}/{file}#L{line}"></head><body>go get %s</body></html>`,
|
|
html.EscapeString(modulePath),
|
|
html.EscapeString(repoURL),
|
|
html.EscapeString(modulePath),
|
|
html.EscapeString(repoURL),
|
|
html.EscapeString(repoURL),
|
|
html.EscapeString(repoURL),
|
|
html.EscapeString(modulePath),
|
|
)
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Query().Get("go-get") != "1" {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "public, max-age=300")
|
|
_, _ = w.Write([]byte(body))
|
|
})
|
|
}
|
|
}
|