From 4f39be8f0ce821c7f04c5fe52e95d47b534cbada Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Sat, 8 Aug 2015 09:01:44 -0700 Subject: [PATCH] Switch to "html/template" and improve the package page This improves the look of each "package" page, especially with more useful information and links. :+1: Also, this switches the Git clone URLs to "https" (see https://help.github.com/articles/which-remote-url-should-i-use/), and adds an explicit "URL" for each package so we can link to browsable source code. --- main.go | 26 ++++++++++++++++++++------ packages.json | 10 ++++++---- template.html | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 template.html diff --git a/main.go b/main.go index 4195831..8970912 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "bytes" "encoding/json" "fmt" + "html/template" "io/ioutil" "net/http" ) @@ -12,6 +14,7 @@ type Package struct { Repo string Path string Packages []string + Url string } func (p Package) GetRoutes() (ret []string) { @@ -38,10 +41,17 @@ func writePageError(w http.ResponseWriter, message string, code int) error { } func writePageGo(w http.ResponseWriter, path string, pkg Package, code int) error { - return writePage(w, fmt.Sprintf(` - %s - %s -`, "Package!", path, "git", pkg.Repo, "Package!"), code) + t, err := template.ParseFiles("template.html") + if err != nil { + return err + } + pkg.Path = path + var page bytes.Buffer + err = t.Execute(&page, pkg) + if err != nil { + return err + } + return writePage(w, page.String(), code) } // }}} @@ -81,10 +91,14 @@ func main() { mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { route := req.URL.Path + var err error if val, ok := routes[route]; ok { - writePageGo(w, "pault.ag"+val.Path, val, 200) + err = writePageGo(w, "pault.ag"+val.Path, val, 200) } else { - writePageError(w, ":(", 404) + err = writePageError(w, ":(", 404) + } + if err != nil { + writePageError(w, fmt.Sprintf("error: %v", err), 400) } }) http.ListenAndServe(":8000", mux) diff --git a/packages.json b/packages.json index 6114c80..a37e798 100644 --- a/packages.json +++ b/packages.json @@ -1,9 +1,11 @@ [ - {"Repo": "git://github.com/paultag/go-debian", + {"Repo": "https://github.com/paultag/go-debian.git", "Path": "/go/debian", - "Packages": ["control", "dependency", "version"]}, + "Packages": ["control", "dependency", "version"], + "Url": "https://github.com/paultag/go-debian"}, - {"Repo": "git://github.com/paultag/go-topsort", + {"Repo": "https://github.com/paultag/go-topsort.git", "Path": "/go/topsort", - "Packages": []} + "Packages": [], + "Url": "https://github.com/paultag/go-topsort"} ] diff --git a/template.html b/template.html new file mode 100644 index 0000000..2f7de81 --- /dev/null +++ b/template.html @@ -0,0 +1,15 @@ + + + + {{ .Path }} + + + +
package: {{ .Path }}
+
source: {{ .Url }}
+
godocs:
+ +