From 3e5407ffe2b9ac1a8e02f39ba585cc7f69f4d0ec Mon Sep 17 00:00:00 2001 From: Jaana Burcu Dogan Date: Sun, 25 Jun 2017 16:49:28 -0700 Subject: [PATCH] support multiple repos --- README.md | 8 +++---- app.yaml | 4 ---- main.go | 63 ++++++++++++++++++++++++++++++++++------------------- vanity.yaml | 5 +++++ 4 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 vanity.yaml diff --git a/README.md b/README.md index 1b23247..1f2f4df 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/app.yaml b/app.yaml index 233d5f1..51946a9 100644 --- a/app.yaml +++ b/app.yaml @@ -4,7 +4,3 @@ api_version: go1 handlers: - url: /.* script: _go_app - -env_variables: - DOMAIN: portmidigo.org - REPO: https://github.com/rakyll/portmidi diff --git a/main.go b/main.go index d571c33..9f6c1e2 100644 --- a/main.go +++ b/main.go @@ -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(` - + -Nothing to see here; move along. +Nothing to see here; move along. `) diff --git a/vanity.yaml b/vanity.yaml new file mode 100644 index 0000000..fa1ad59 --- /dev/null +++ b/vanity.yaml @@ -0,0 +1,5 @@ +/portmidi: + repo: https://github.com/rakyll/portmidi + +/launchpad: + repo: https://github.com/rakyll/launchpad