diff --git a/README.md b/README.md index 693be18..5896924 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,9 @@ Edit `vanity.yaml` to add any number of git repos. E.g., `customdomain.com/portm serve the [https://github.com/rakyll/portmidi](https://github.com/rakyll/portmidi) repo. ``` -/portmidi: - repo: https://github.com/rakyll/portmidi +paths: + /portmidi: + repo: https://github.com/rakyll/portmidi ``` You can add as many rules as you wish. diff --git a/appengine.go b/appengine.go index 2d2c159..6cb4098 100644 --- a/appengine.go +++ b/appengine.go @@ -37,6 +37,6 @@ func main() { appengine.Main() } -func requestHost(r *http.Request) string { +func defaultHost(r *http.Request) string { return appengine.DefaultVersionHostname(appengine.NewContext(r)) } diff --git a/handler.go b/handler.go index b1cdfcc..ee131c9 100644 --- a/handler.go +++ b/handler.go @@ -38,16 +38,19 @@ type pathConfig struct { } func newHandler(config []byte) (*handler, error) { - var m map[string]struct { - Repo string `yaml:"repo,omitempty"` - Display string `yaml:"display,omitempty"` - VCS string `yaml:"vcs,omitempty"` + var parsed struct { + Host string `yaml:"host,omitempty"` + Paths map[string]struct { + Repo string `yaml:"repo,omitempty"` + Display string `yaml:"display,omitempty"` + VCS string `yaml:"vcs,omitempty"` + } `yaml:"paths,omitempty"` } - if err := yaml.Unmarshal(config, &m); err != nil { + if err := yaml.Unmarshal(config, &parsed); err != nil { return nil, err } - h := new(handler) - for path, e := range m { + h := &handler{host: parsed.Host} + for path, e := range parsed.Paths { pc := pathConfig{ path: strings.TrimSuffix(path, "/"), repo: e.Repo, @@ -89,7 +92,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { host := h.host if host == "" { - host = requestHost(r) + host = defaultHost(r) } if err := vanityTmpl.Execute(w, struct { Import string diff --git a/handler_test.go b/handler_test.go index d36d122..9b41c8a 100644 --- a/handler_test.go +++ b/handler_test.go @@ -34,53 +34,65 @@ func TestHandler(t *testing.T) { }{ { name: "explicit display", - config: "/portmidi:\n" + - " repo: https://github.com/rakyll/portmidi\n" + - " display: https://github.com/rakyll/portmidi _ _\n", + config: "host: example.com\n" + + "paths:\n" + + " /portmidi:\n" + + " repo: https://github.com/rakyll/portmidi\n" + + " display: https://github.com/rakyll/portmidi _ _\n", path: "/portmidi", goImport: "example.com/portmidi git https://github.com/rakyll/portmidi", goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _", }, { name: "display GitHub inference", - config: "/portmidi:\n" + - " repo: https://github.com/rakyll/portmidi\n", + config: "host: example.com\n" + + "paths:\n" + + " /portmidi:\n" + + " repo: https://github.com/rakyll/portmidi\n", path: "/portmidi", goImport: "example.com/portmidi git https://github.com/rakyll/portmidi", goSource: "example.com/portmidi https://github.com/rakyll/portmidi https://github.com/rakyll/portmidi/tree/master{/dir} https://github.com/rakyll/portmidi/blob/master{/dir}/{file}#L{line}", }, { name: "Bitbucket Mercurial", - config: "/gopdf:\n" + - " repo: https://bitbucket.org/zombiezen/gopdf\n" + - " vcs: hg\n", + config: "host: example.com\n" + + "paths:\n" + + " /gopdf:\n" + + " repo: https://bitbucket.org/zombiezen/gopdf\n" + + " vcs: hg\n", path: "/gopdf", goImport: "example.com/gopdf hg https://bitbucket.org/zombiezen/gopdf", goSource: "example.com/gopdf https://bitbucket.org/zombiezen/gopdf https://bitbucket.org/zombiezen/gopdf/src/default{/dir} https://bitbucket.org/zombiezen/gopdf/src/default{/dir}/{file}#{file}-{line}", }, { name: "Bitbucket Git", - config: "/mygit:\n" + - " repo: https://bitbucket.org/zombiezen/mygit\n" + - " vcs: git\n", + config: "host: example.com\n" + + "paths:\n" + + " /mygit:\n" + + " repo: https://bitbucket.org/zombiezen/mygit\n" + + " vcs: git\n", path: "/mygit", goImport: "example.com/mygit git https://bitbucket.org/zombiezen/mygit", goSource: "example.com/mygit https://bitbucket.org/zombiezen/mygit https://bitbucket.org/zombiezen/mygit/src/default{/dir} https://bitbucket.org/zombiezen/mygit/src/default{/dir}/{file}#{file}-{line}", }, { name: "subpath", - config: "/portmidi:\n" + - " repo: https://github.com/rakyll/portmidi\n" + - " display: https://github.com/rakyll/portmidi _ _\n", + config: "host: example.com\n" + + "paths:\n" + + " /portmidi:\n" + + " repo: https://github.com/rakyll/portmidi\n" + + " display: https://github.com/rakyll/portmidi _ _\n", path: "/portmidi/foo", goImport: "example.com/portmidi git https://github.com/rakyll/portmidi", goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _", }, { name: "subpath with trailing config slash", - config: "/portmidi/:\n" + - " repo: https://github.com/rakyll/portmidi\n" + - " display: https://github.com/rakyll/portmidi _ _\n", + config: "host: example.com\n" + + "paths:\n" + + " /portmidi/:\n" + + " repo: https://github.com/rakyll/portmidi\n" + + " display: https://github.com/rakyll/portmidi _ _\n", path: "/portmidi/foo", goImport: "example.com/portmidi git https://github.com/rakyll/portmidi", goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _", @@ -92,7 +104,6 @@ func TestHandler(t *testing.T) { t.Errorf("%s: newHandler: %v", test.name, err) continue } - h.host = "example.com" s := httptest.NewServer(h) resp, err := http.Get(s.URL + test.path) if err != nil { @@ -121,11 +132,13 @@ func TestHandler(t *testing.T) { func TestBadConfigs(t *testing.T) { badConfigs := []string{ - "/missingvcs:\n" + - " repo: https://bitbucket.org/zombiezen/gopdf\n", - "/unknownvcs:\n" + - " repo: https://bitbucket.org/zombiezen/gopdf\n" + - " vcs: xyzzy\n", + "paths:\n" + + " /missingvcs:\n" + + " repo: https://bitbucket.org/zombiezen/gopdf\n", + "paths:\n" + + " /unknownvcs:\n" + + " repo: https://bitbucket.org/zombiezen/gopdf\n" + + " vcs: xyzzy\n", } for _, config := range badConfigs { _, err := newHandler([]byte(config)) diff --git a/main.go b/main.go index b7c6cd0..ce9b452 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,6 @@ func main() { } } -func requestHost(r *http.Request) string { +func defaultHost(r *http.Request) string { return r.Host } diff --git a/vanity.yaml b/vanity.yaml index fa1ad59..de7fa14 100644 --- a/vanity.yaml +++ b/vanity.yaml @@ -1,5 +1,6 @@ -/portmidi: - repo: https://github.com/rakyll/portmidi +paths: + /portmidi: + repo: https://github.com/rakyll/portmidi -/launchpad: - repo: https://github.com/rakyll/launchpad + /launchpad: + repo: https://github.com/rakyll/launchpad