camweb: set "go-import" meta for both camlistore.org and perkeep.org

It is not enough for go get to work, as our packages won't have the
right import path comment, but i believe it is the first necessary step.

Issue #981

Change-Id: I9418f944fc9bed603fd6acc0761e003cafdcfa8a
This commit is contained in:
mpl 2017-12-12 17:32:58 +01:00
parent d3339954df
commit 9a492bd05f
4 changed files with 27 additions and 9 deletions

View File

@ -177,13 +177,22 @@ func applyTemplate(t *template.Template, name string, data interface{}) []byte {
return buf.Bytes()
}
// goGetDomain returns one of the two domains that we serve for the "go-import"
// meta header
func goGetDomain(host string) string {
if host == "camlistore.org" {
return host
}
return "perkeep.org"
}
type pageParams struct {
title string // required
subtitle string // used by pkg doc
content []byte // required
}
func servePage(w http.ResponseWriter, params pageParams) {
func servePage(w http.ResponseWriter, r *http.Request, params pageParams) {
title, subtitle, content := params.title, params.subtitle, params.content
// insert an "install command" if it applies
if strings.Contains(title, cmdPattern) && subtitle != cmdPattern {
@ -197,12 +206,15 @@ func servePage(w http.ResponseWriter, params pageParams) {
Title string
Subtitle string
Content template.HTML
Domain string // for the "go-import" meta header
}{
title,
subtitle,
template.HTML(content),
// the redirects happening before should ensure that r.Host is only ever one of
// camlistore.org or perkeep.org
goGetDomain(r.Host),
}
if err := pageHTML.ExecuteTemplate(w, "page", &d); err != nil {
log.Printf("godocHTML.Execute: %s", err)
}
@ -232,7 +244,7 @@ func readTemplates() {
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
w.WriteHeader(http.StatusNotFound)
servePage(w, pageParams{
servePage(w, r, pageParams{
title: "File " + relpath,
content: contents,
})
@ -440,7 +452,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, relPath, absPath string)
title = string(m[1])
}
servePage(w, pageParams{
servePage(w, r, pageParams{
title: title,
content: data,
})
@ -467,6 +479,12 @@ func (h *redirectRootHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
return
}
if goget := r.FormValue("go-get"); goget == "1" {
// do not redirect on a go get request, because we want to be able to serve the
// "go-import" meta for camlistore.org, and not just for perkeep.org
h.Handler.ServeHTTP(rw, r)
return
}
host := strings.ToLower(r.Host)
if host == "www.camlistore.org" || host == "camlistore.org" ||
host == "www."+prodDomain || (inProd && r.TLS == nil) {
@ -1177,7 +1195,7 @@ func errHandler(w http.ResponseWriter, r *http.Request) {
}
contents := applyTemplate(camliErrorHTML, "camliErrorHTML", data)
w.WriteHeader(http.StatusFound)
servePage(w, pageParams{
servePage(w, r, pageParams{
title: errString,
content: contents,
})

View File

@ -187,7 +187,7 @@ func contribHandler() http.HandlerFunc {
title = string(m[1])
}
return func(w http.ResponseWriter, r *http.Request) {
servePage(w, pageParams{
servePage(w, r, pageParams{
title: title,
content: c,
})

View File

@ -415,7 +415,7 @@ func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, tit
buf.WriteString("<pre>")
FormatText(&buf, src, 1, pathpkg.Ext(abspath) == ".go", r.FormValue("h"), rangeSelection(r.FormValue("s")))
buf.WriteString("</pre>")
servePage(w, pageParams{
servePage(w, r, pageParams{
title: title,
content: buf.Bytes(),
})
@ -466,7 +466,7 @@ func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
subtitle := pathpkg.Base(diskPath)
title := subtitle + " (" + pathpkg.Join(domainName, suffix) + ")"
servePage(w, pageParams{
servePage(w, r, pageParams{
title: title,
subtitle: subtitle,
content: applyTextTemplate(packageHTML, "packageHTML", pi),

View File

@ -16,7 +16,7 @@
{{define "header"}}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="camlistore.org git https://camlistore.googlesource.com/camlistore">
<meta name="go-import" content="{{.Domain}} git https://camlistore.googlesource.com/camlistore">
{{with $x := .Title}}
<title>{{$x}} - Camlistore</title>
{{else}}