support host directive in config (#7)

This allows for the service to be proxied as a service in a larger site.
This commit is contained in:
Ross Light 2017-07-10 09:50:21 -07:00 committed by Jaana B. Dogan
parent 148d52de30
commit c5fd58403f
6 changed files with 57 additions and 39 deletions

View File

@ -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.

View File

@ -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))
}

View File

@ -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

View File

@ -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))

View File

@ -41,6 +41,6 @@ func main() {
}
}
func requestHost(r *http.Request) string {
func defaultHost(r *http.Request) string {
return r.Host
}

View File

@ -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