Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
Complete package badge implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
garyburd committed Nov 12, 2013
1 parent ac3a490 commit 22e5783
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 64 deletions.
2 changes: 1 addition & 1 deletion gddo-server/assets/robots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Disallow: /*?importers
Disallow: /*?import-graph*
Disallow: /*?gosrc*
Disallow: /*?file*
Disallow: /*?status
Disallow: /*?tools

user-agent: AhrefsBot
disallow: /
Binary file modified gddo-server/assets/status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions gddo-server/assets/templates/common.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@
<form name="x-refresh" method="POST" action="/-/refresh"><input type="hidden" name="path" value="{{.ImportPath}}"></form>
<p>{{if or .Imports $.importerCount}}Package {{.Name}} {{if .Imports}}imports <a href="?imports">{{.Imports|len}} packages</a> (<a href="?import-graph">graph</a>){{end}}{{if and .Imports $.importerCount}} and {{end}}{{if $.importerCount}}is imported by <a href="?importers">{{$.importerCount}} packages</a>{{end}}.{{end}}
{{if not .Updated.IsZero}}Updated <span class="timeago" title="{{.Updated.Format "2006-01-02T15:04:05Z"}}">{{.Updated.Format "2006-01-02"}}</span>{{if or (equal .GOOS "windows") (equal .GOOS "darwin")}} with GOOS={{.GOOS}}{{end}}.{{end}}
<a href="javascript:document.getElementsByName('x-refresh')[0].submit();" title="Refresh this page from the source.">Refresh</a>.
{{/* {{if and .Name (equal templateName "pkg.html")}}
<a href="?status" class="pull-right"><img src="/-/status.png" width="56" height="18" alt="Status Badge"></a>
{{end}} */}}
<a href="javascript:document.getElementsByName('x-refresh')[0].submit();" title="Refresh this page from the source.">Refresh now</a>.
{{if equal templateName "pkg.html"}}<a href="?tools">Tools</a> for package owners.{{end}}
{{end}}
{{with $.pdoc.Errors}}
<p>The <a href="http://golang.org/cmd/go/#Download_and_install_packages_and_dependencies">go get</a>
Expand Down
21 changes: 0 additions & 21 deletions gddo-server/assets/templates/status.html

This file was deleted.

26 changes: 26 additions & 0 deletions gddo-server/assets/templates/tools.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{define "Head"}}<title>{{.pdoc.PageName}} tools - GoDoc</title><meta name="robots" content="NOINDEX, NOFOLLOW">{{end}}

{{define "Body"}}
{{template "ProjectNav" $}}
<h2>Tools for {{$.pdoc.Name}}</h2>

<h3>Badge</h3>

<p><a href="http://{{.host}}/{{.pdoc.ImportPath}}"><img src="{{.uri}}" alt="GoDoc"></a>

<p>Use one of the snippets below to add a link to GoDoc from your project
website or README file:</a>

<h5>HTML</h5>
<input type="text" value='<a href="http://{{.host}}/{{.pdoc.ImportPath}}"><img src="{{.uri}}" alt="GoDoc"></a>' class="click-select form-control">

<h5>Markdown</h5>
<input type="text" value="[![GoDoc]({{.uri}})](http://{{.host}}/{{.pdoc.ImportPath}})" class="click-select form-control">

<h3>Lint</h3>

<form name="x-lint" method="POST" action="http://go-lint.appspot.com/-/refresh"><input name="importPath" type="hidden" value="{{.pdoc.ImportPath}}"></form>
Run lint on <a href="javascript:document.getElementsByName('x-lint')[0].submit();">{{.pdoc.Name}}</a>.

<p>&nbsp;
{{end}}
45 changes: 25 additions & 20 deletions gddo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func servePackage(resp http.ResponseWriter, req *http.Request) error {
}

if isView(req, "status.png") {
statusHandler.ServeHTTP(resp, req)
statusImageHandler.ServeHTTP(resp, req)
return nil
}

Expand Down Expand Up @@ -292,13 +292,20 @@ func servePackage(resp http.ResponseWriter, req *http.Request) error {
"pkgs": pkgs,
"pdoc": newTDoc(pdoc),
})
case isView(req, "status"):
case isView(req, "tools"):
if pdoc.Name == "" {
break
}
return executeTemplate(resp, "status.html", http.StatusOK, nil, map[string]interface{}{
"Host": req.Host,
proto := "http"
if req.Host == "godoc.org" {
// In case show different badges in the future, use https to
// bust through the caching proxy on Github and other services.
proto = "https"
}
return executeTemplate(resp, "tools.html", http.StatusOK, nil, map[string]interface{}{
"uri": fmt.Sprintf("%s://%s/%s?status.png", proto, req.Host, importPath),
"pdoc": newTDoc(pdoc),
"host": req.Host,
})
case isView(req, "redir"):
if srcFiles == nil {
Expand Down Expand Up @@ -745,16 +752,19 @@ func defaultBase(path string) string {
}

var (
db *database.Database
db *database.Database
statusImageHandler http.Handler
srcFiles = make(map[string]*zip.File)
)

var (
robot = flag.Float64("robot", 100, "Request counter threshold for robots")
assetsDir = flag.String("assets", filepath.Join(defaultBase("github.com/garyburd/gddo/gddo-server"), "assets"), "Base directory for templates and static files.")
getTimeout = flag.Duration("get_timeout", 8*time.Second, "Time to wait for package update from the VCS.")
firstGetTimeout = flag.Duration("first_get_timeout", 5*time.Second, "Time to wait for first fetch of package from the VCS.")
maxAge = flag.Duration("max_age", 24*time.Hour, "Update package documents older than this age.")
httpAddr = flag.String("http", ":8080", "Listen for HTTP connections on this address")
srcZip = flag.String("srcZip", "", "")
srcFiles = make(map[string]*zip.File)
statusHandler http.Handler
)

func main() {
Expand Down Expand Up @@ -787,7 +797,7 @@ func main() {
{"notfound.html", "common.html", "layout.html"},
{"pkg.html", "common.html", "layout.html"},
{"results.html", "common.html", "layout.html"},
{"status.html", "common.html", "layout.html"},
{"tools.html", "common.html", "layout.html"},
{"std.html", "common.html", "layout.html"},
{"subrepo.html", "common.html", "layout.html"},
{"graph.html", "common.html"},
Expand Down Expand Up @@ -822,28 +832,22 @@ func main() {
".js": "text/javascript; charset=utf-8",
},
}
apiStaticServer := tango.StaticServer{
Dir: *assetsDir,
MaxAge: time.Hour,
}

statusHandler = staticServer.FileHandler("status.png")
statusImageHandler = staticServer.FileHandler("status.png")

hr := tango.NewHostRouter()
r := tango.NewRouter()
r.Error(handleAPIError)
r.Add("/favicon.ico").Get(apiStaticServer.FileHandler("favicon.ico"))
r.Add("/google3d2f3cd4cc2bb44b.html").Get(apiStaticServer.FileHandler("google3d2f3cd4cc2bb44b.html"))
r.Add("/humans.txt").Get(apiStaticServer.FileHandler("humans.txt"))
r.Add("/robots.txt").Get(apiStaticServer.FileHandler("apiRobots.txt"))
r.Add("/favicon.ico").Get(staticServer.FileHandler("favicon.ico"))
r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticServer.FileHandler("google3d2f3cd4cc2bb44b.html"))
r.Add("/humans.txt").Get(staticServer.FileHandler("humans.txt"))
r.Add("/robots.txt").Get(staticServer.FileHandler("apiRobots.txt"))
r.Add("/search").Get(apiHandler(serveAPISearch))
r.Add("/packages").Get(apiHandler(serveAPIPackages))
r.Add("/importers/<path:.+>").Get(apiHandler(serveAPIImporters))
r.Add("/imports/<path:.+>").Get(apiHandler(serveAPIImports))
hr.Add("api.<:.*>", r)

r = tango.NewRouter()
cacheBusters.Handler = r
r.Add("/-/site.js").Get(staticServer.FilesHandler(
"third_party/jquery.timeago.js",
"third_party/typeahead.min.js",
Expand All @@ -859,7 +863,6 @@ func main() {
r.Add("/-/subrepo").Get(handler(serveGoSubrepoIndex))
r.Add("/-/index").Get(handler(serveIndex))
r.Add("/-/refresh").Post(handler(serveRefresh))
r.Add("/-/status.png").Get(statusHandler)
r.Add("/a/index").Get(http.RedirectHandler("/-/index", 301))
r.Add("/about").Get(http.RedirectHandler("/-/about", 301))
r.Add("/favicon.ico").Get(staticServer.FileHandler("favicon.ico"))
Expand All @@ -871,6 +874,8 @@ func main() {
r.Add("/<path:.+>").Get(handler(servePackage))
hr.Add("<:.*>", r)

cacheBusters.Handler = r

if err := http.ListenAndServe(*httpAddr, hr); err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 1 addition & 18 deletions gddo-server/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package main

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
godoc "go/doc"
Expand Down Expand Up @@ -56,13 +54,6 @@ func newTDoc(pdoc *doc.Package) *tdoc {
return &tdoc{Package: pdoc}
}

func (pdoc *tdoc) Token() string {
h := sha1.New()
io.WriteString(h, pdoc.ImportPath)
io.WriteString(h, " shared")
return hex.EncodeToString(h.Sum(nil))
}

func (pdoc *tdoc) SourceLink(pos doc.Pos, text, anchor string) htemp.HTML {
text = htemp.HTMLEscapeString(text)
if pos.Line == 0 || pdoc.LineFmt == "" || pdoc.Files[pos.File].URL == "" {
Expand Down Expand Up @@ -208,14 +199,6 @@ func hostFn(urlStr string) string {
return u.Host
}

func staticPathFn(p string) string {
token := cacheBusters.Get(p)
if token == "" {
return p
}
return p + "?v=" + token
}

func mapFn(kvs ...interface{}) (map[string]interface{}, error) {
if len(kvs)%2 != 0 {
return nil, errors.New("map requires even number of arguments.")
Expand Down Expand Up @@ -440,7 +423,7 @@ func parseHTMLTemplates(sets [][]string) error {
"map": mapFn,
"noteTitle": noteTitleFn,
"relativePath": relativePathFn,
"staticPath": staticPathFn,
"staticPath": func(p string) string { return cacheBusters.AppendQueryParam(p, "v") },
"templateName": func() string { return templateName },
})
if _, err := t.ParseFiles(joinTemplateDir(*assetsDir, set)...); err != nil {
Expand Down

0 comments on commit 22e5783

Please sign in to comment.