support multiple repos

This commit is contained in:
Jaana Burcu Dogan 2017-06-25 16:49:28 -07:00
parent 27b4ae8290
commit 3e5407ffe2
4 changed files with 49 additions and 31 deletions

View File

@ -19,12 +19,12 @@ go get -u -d github.com/GoogleCloudPlatform/govanityurls
cd $(go env GOPATH)/github.com/GoogleCloudPlatform/govanityurls
```
Edit `app.yaml` with your custom domain and git repo information.
Edit `vanity.yaml` too any number of git repos. E.g., `CUSTOM_DOMAIN/portmidi` will
serve the package in the [https://github.com/rakyll/portmidi] repo.
```
env_variables:
DOMAIN: go.grpcutil.org
REPO: https://github.com/rakyll/grpcutil
/portmidi:
repo: https://github.com/rakyll/portmidi
```
Deploy the app:

View File

@ -4,7 +4,3 @@ api_version: go1
handlers:
- url: /.*
script: _go_app
env_variables:
DOMAIN: portmidigo.org
REPO: https://github.com/rakyll/portmidi

63
main.go
View File

@ -22,40 +22,57 @@ import (
"net/http"
"os"
"strings"
"google.golang.org/appengine"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type pkg struct {
Import string
Repo string
Display string
CurrentPkg string
type ypkg struct {
Repo string `yaml:"repo,omitempty"`
Display string `yaml:"display,omitempty"`
}
var (
domain string
repo string
display string
)
var m map[string]ypkg
func init() {
mustLoad("DOMAIN", &domain)
mustLoad("REPO", &repo)
if strings.Contains(repo, "github.com") {
display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", repo, repo, repo)
vanity, err := ioutil.ReadFile("./vanity.yaml")
if err != nil {
log.Fatal(err)
}
if display == "" {
mustLoad("DISPLAY", &display)
if err := yaml.Unmarshal(vanity, &m); err != nil {
log.Fatal(err)
}
for _, e := range m {
if e.Display != "" {
continue
}
if strings.Contains(e.Repo, "github.com") {
e.Display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", e.Repo, e.Repo, e.Repo)
}
}
http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
current := r.URL.Path
if err := vanityTmpl.Execute(w, &pkg{
Import: domain,
Repo: repo,
Display: display,
CurrentPkg: current,
p, ok := m[current]
if !ok {
http.NotFound(w, r)
return
}
host := appengine.DefaultVersionHostname(appengine.NewContext(r))
if err := vanityTmpl.Execute(w, struct {
Import string
Repo string
Display string
}{
Import: host + current,
Repo: p.Repo,
Display: p.Display,
}); err != nil {
http.Error(w, "cannot render the page", http.StatusInternalServerError)
}
@ -67,10 +84,10 @@ var vanityTmpl, _ = template.New("vanity").Parse(`<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.Import}} git {{.Repo}}">
<meta name="go-source" content="{{.Import}} {{.Display}}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}{{.CurrentPkg}}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}">
</head>
<body>
Nothing to see here; <a href="https://godoc.org/{{.Import}}{{.CurrentPkg}}">move along</a>.
Nothing to see here; <a href="https://godoc.org/{{.Import}}">move along</a>.
</body>
</html>`)

5
vanity.yaml Normal file
View File

@ -0,0 +1,5 @@
/portmidi:
repo: https://github.com/rakyll/portmidi
/launchpad:
repo: https://github.com/rakyll/launchpad