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 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: /portmidi:
DOMAIN: go.grpcutil.org repo: https://github.com/rakyll/portmidi
REPO: https://github.com/rakyll/grpcutil
``` ```
Deploy the app: Deploy the app:

View File

@ -4,7 +4,3 @@ api_version: go1
handlers: handlers:
- url: /.* - url: /.*
script: _go_app 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" "net/http"
"os" "os"
"strings" "strings"
"google.golang.org/appengine"
"io/ioutil"
"gopkg.in/yaml.v2"
) )
type pkg struct { type ypkg struct {
Import string Repo string `yaml:"repo,omitempty"`
Repo string Display string `yaml:"display,omitempty"`
Display string
CurrentPkg string
} }
var ( var m map[string]ypkg
domain string
repo string
display string
)
func init() { func init() {
mustLoad("DOMAIN", &domain) vanity, err := ioutil.ReadFile("./vanity.yaml")
mustLoad("REPO", &repo) if err != nil {
if strings.Contains(repo, "github.com") { log.Fatal(err)
display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", repo, repo, repo) }
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)
} }
if display == "" {
mustLoad("DISPLAY", &display)
} }
http.HandleFunc("/", handle) http.HandleFunc("/", handle)
} }
func handle(w http.ResponseWriter, r *http.Request) { func handle(w http.ResponseWriter, r *http.Request) {
current := r.URL.Path current := r.URL.Path
if err := vanityTmpl.Execute(w, &pkg{ p, ok := m[current]
Import: domain, if !ok {
Repo: repo, http.NotFound(w, r)
Display: display, return
CurrentPkg: current, }
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 { }); err != nil {
http.Error(w, "cannot render the page", http.StatusInternalServerError) 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 http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.Import}} git {{.Repo}}"> <meta name="go-import" content="{{.Import}} git {{.Repo}}">
<meta name="go-source" content="{{.Import}} {{.Display}}"> <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> </head>
<body> <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> </body>
</html>`) </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