Switch to "html/template" and improve the package page
This improves the look of each "package" page, especially with more useful information and links. 👍 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.
This commit is contained in:
parent
2fd844bd80
commit
4f39be8f0c
3 changed files with 41 additions and 10 deletions
26
main.go
26
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(`<!DOCTYPE html><html>
|
||||
<head><meta charset="utf-8"><title>%s</title><meta name="go-import" content="%s %s %s"></head>
|
||||
<body>%s</body>
|
||||
</html>`, "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)
|
||||
|
|
|
@ -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"}
|
||||
]
|
||||
|
|
15
template.html
Normal file
15
template.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html><html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{ .Path }}</title>
|
||||
<meta name="go-import" content="{{ .Path }} git {{ .Repo }}">
|
||||
</head>
|
||||
<body>
|
||||
<div>package: <code>{{ .Path }}</code></div>
|
||||
<div>source: <a href="{{ .Url }}">{{ .Url }}</a></div>
|
||||
<div>godocs:<ul>
|
||||
<li><a href="https://godoc.org/{{ .Path }}">{{ .Path }}</a></li>{{range .Packages}}
|
||||
<li><a href="https://godoc.org/{{ $.Path }}/{{ . }}">{{ $.Path }}/{{ . }}</a></li>{{end}}
|
||||
</ul></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue