From 2cea7a877d2078041f66518f824cdedde4698791 Mon Sep 17 00:00:00 2001 From: Bruno Santos Date: Tue, 29 Jun 2021 18:24:05 +0100 Subject: [PATCH 1/2] Add optional home property to be used as a redirect in template --- handler.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/handler.go b/handler.go index c37c5a0..df09844 100644 --- a/handler.go +++ b/handler.go @@ -36,6 +36,7 @@ type pathConfig struct { path string repo string display string + home string vcs string } @@ -46,6 +47,7 @@ func newHandler(config []byte) (*handler, error) { Paths map[string]struct { Repo string `yaml:"repo,omitempty"` Display string `yaml:"display,omitempty"` + Home string `yaml:"home,omitempty"` VCS string `yaml:"vcs,omitempty"` } `yaml:"paths,omitempty"` } @@ -66,6 +68,7 @@ func newHandler(config []byte) (*handler, error) { path: strings.TrimSuffix(path, "/"), repo: e.Repo, display: e.Display, + home: e.Home, vcs: e.VCS, } switch { @@ -106,18 +109,26 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Cache-Control", h.cacheControl) + + importStr := h.Host(r) + pc.path + redirectURL := fmt.Sprintf("https://pkg.go.dev/%s/%s", importStr, subpath) + if pc.home != "" { + redirectURL = pc.home + } if err := vanityTmpl.Execute(w, struct { - Import string - Subpath string - Repo string - Display string - VCS string + Import string + Subpath string + Repo string + Display string + VCS string + RedirectURL string }{ - Import: h.Host(r) + pc.path, - Subpath: subpath, - Repo: pc.repo, - Display: pc.display, - VCS: pc.vcs, + Import: importStr, + Subpath: subpath, + Repo: pc.repo, + Display: pc.display, + VCS: pc.vcs, + RedirectURL: redirectURL, }); err != nil { http.Error(w, "cannot render the page", http.StatusInternalServerError) } @@ -163,10 +174,10 @@ var vanityTmpl = template.Must(template.New("vanity").Parse(` - + -Nothing to see here; see the package on pkg.go.dev. +Nothing to see here; see the package on {{.RedirectURL}}. `)) From c6d787e12093a75d6fbe91c40df9f88953bc3223 Mon Sep 17 00:00:00 2001 From: Bruno Santos Date: Tue, 29 Jun 2021 18:35:19 +0100 Subject: [PATCH 2/2] Use home property in index template --- handler.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/handler.go b/handler.go index df09844..1dac38b 100644 --- a/handler.go +++ b/handler.go @@ -134,15 +134,24 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +type indexDTO struct { + Import string + URL string +} + func (h *handler) serveIndex(w http.ResponseWriter, r *http.Request) { host := h.Host(r) - handlers := make([]string, len(h.paths)) + handlers := make([]indexDTO, len(h.paths)) for i, h := range h.paths { - handlers[i] = host + h.path + url := fmt.Sprintf("https://pkg.go.dev/%s", host+h.path) + if h.home != "" { + url = h.home + } + handlers[i] = indexDTO{Import: host + h.path, URL: url} } if err := indexTmpl.Execute(w, struct { Host string - Handlers []string + Handlers []indexDTO }{ Host: host, Handlers: handlers, @@ -163,7 +172,7 @@ var indexTmpl = template.Must(template.New("index").Parse(`

{{.Host}}

`))