From 29d8e3f3835cc7543c390a8a8f5bdfd67bcd3c2d Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sat, 29 Apr 2023 11:48:34 -0700 Subject: 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. --- handler.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'handler.go') diff --git a/handler.go b/handler.go index 5144caf..918405e 100644 --- a/handler.go +++ b/handler.go @@ -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) } } -- cgit v1.2.3