diff options
author | 2023-04-29 11:48:34 -0700 | |
---|---|---|
committer | 2023-04-29 11:48:34 -0700 | |
commit | 29d8e3f3835cc7543c390a8a8f5bdfd67bcd3c2d (patch) | |
tree | 5a151adbee7a1d94d3b1b91542f0bafbf83024cf /handler.go | |
parent | 8d9e76b1f11fd164ef83614bdc485ffd71088d9c (diff) | |
download | sally-29d8e3f3835cc7543c390a8a8f5bdfd67bcd3c2d.tar.gz sally-29d8e3f3835cc7543c390a8a8f5bdfd67bcd3c2d.tar.zst sally-29d8e3f3835cc7543c390a8a8f5bdfd67bcd3c2d.zip |
refactor(indexHandler): Don't retain configuration (#86)
Similar to the previous patch,
this patch removes knowledge of the configuration from the index handler.
Instead, the index handler is now passed a list of packageInfo structs
each containing just the information needed to render the index page.
This also obviates the need to duplicate the logic to compute
the package import path in the index.html template.
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 28 |
1 files changed, 25 insertions, 3 deletions
@@ -4,6 +4,7 @@ import ( "fmt" "html/template" "net/http" + "sort" "strings" "go.uber.org/sally/templates" @@ -20,20 +21,38 @@ var ( func CreateHandler(config *Config) http.Handler { mux := http.NewServeMux() - mux.Handle("/", &indexHandler{config: config}) + pkgs := make([]packageInfo, 0, len(config.Packages)) for name, pkg := range config.Packages { handler := newPackageHandler(config, name, pkg) // Double-register so that "/foo" // does not redirect to "/foo/" with a 300. mux.Handle("/"+name, handler) mux.Handle("/"+name+"/", handler) + + pkgs = append(pkgs, packageInfo{ + Desc: pkg.Desc, + ImportPath: handler.canonicalURL, + GitURL: handler.gitURL, + GodocHome: handler.godocHost + "/" + handler.canonicalURL, + }) } + sort.Slice(pkgs, func(i, j int) bool { + return pkgs[i].ImportPath < pkgs[j].ImportPath + }) + mux.Handle("/", &indexHandler{pkgs: pkgs}) return mux } type indexHandler struct { - config *Config + pkgs []packageInfo +} + +type packageInfo struct { + Desc string // package description + ImportPath string // canonical improt path + GitURL string // URL of the Git repository + GodocHome string // documentation home URL } func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -44,7 +63,10 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - if err := indexTemplate.Execute(w, h.config); err != nil { + data := struct{ Packages []packageInfo }{ + Packages: h.pkgs, + } + if err := indexTemplate.Execute(w, data); err != nil { http.Error(w, err.Error(), 500) } } |