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:
parent
148d52de30
commit
c5fd58403f
@ -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.
|
serve the [https://github.com/rakyll/portmidi](https://github.com/rakyll/portmidi) repo.
|
||||||
|
|
||||||
```
|
```
|
||||||
/portmidi:
|
paths:
|
||||||
repo: https://github.com/rakyll/portmidi
|
/portmidi:
|
||||||
|
repo: https://github.com/rakyll/portmidi
|
||||||
```
|
```
|
||||||
|
|
||||||
You can add as many rules as you wish.
|
You can add as many rules as you wish.
|
||||||
|
@ -37,6 +37,6 @@ func main() {
|
|||||||
appengine.Main()
|
appengine.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestHost(r *http.Request) string {
|
func defaultHost(r *http.Request) string {
|
||||||
return appengine.DefaultVersionHostname(appengine.NewContext(r))
|
return appengine.DefaultVersionHostname(appengine.NewContext(r))
|
||||||
}
|
}
|
||||||
|
19
handler.go
19
handler.go
@ -38,16 +38,19 @@ type pathConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newHandler(config []byte) (*handler, error) {
|
func newHandler(config []byte) (*handler, error) {
|
||||||
var m map[string]struct {
|
var parsed struct {
|
||||||
Repo string `yaml:"repo,omitempty"`
|
Host string `yaml:"host,omitempty"`
|
||||||
Display string `yaml:"display,omitempty"`
|
Paths map[string]struct {
|
||||||
VCS string `yaml:"vcs,omitempty"`
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
h := new(handler)
|
h := &handler{host: parsed.Host}
|
||||||
for path, e := range m {
|
for path, e := range parsed.Paths {
|
||||||
pc := pathConfig{
|
pc := pathConfig{
|
||||||
path: strings.TrimSuffix(path, "/"),
|
path: strings.TrimSuffix(path, "/"),
|
||||||
repo: e.Repo,
|
repo: e.Repo,
|
||||||
@ -89,7 +92,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
host := h.host
|
host := h.host
|
||||||
if host == "" {
|
if host == "" {
|
||||||
host = requestHost(r)
|
host = defaultHost(r)
|
||||||
}
|
}
|
||||||
if err := vanityTmpl.Execute(w, struct {
|
if err := vanityTmpl.Execute(w, struct {
|
||||||
Import string
|
Import string
|
||||||
|
@ -34,53 +34,65 @@ func TestHandler(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "explicit display",
|
name: "explicit display",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
"paths:\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" /portmidi:\n" +
|
||||||
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi",
|
path: "/portmidi",
|
||||||
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
||||||
goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _",
|
goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "display GitHub inference",
|
name: "display GitHub inference",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n",
|
"paths:\n" +
|
||||||
|
" /portmidi:\n" +
|
||||||
|
" repo: https://github.com/rakyll/portmidi\n",
|
||||||
path: "/portmidi",
|
path: "/portmidi",
|
||||||
goImport: "example.com/portmidi git https://github.com/rakyll/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}",
|
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",
|
name: "Bitbucket Mercurial",
|
||||||
config: "/gopdf:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
"paths:\n" +
|
||||||
" vcs: hg\n",
|
" /gopdf:\n" +
|
||||||
|
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
||||||
|
" vcs: hg\n",
|
||||||
path: "/gopdf",
|
path: "/gopdf",
|
||||||
goImport: "example.com/gopdf hg https://bitbucket.org/zombiezen/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}",
|
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",
|
name: "Bitbucket Git",
|
||||||
config: "/mygit:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/mygit\n" +
|
"paths:\n" +
|
||||||
" vcs: git\n",
|
" /mygit:\n" +
|
||||||
|
" repo: https://bitbucket.org/zombiezen/mygit\n" +
|
||||||
|
" vcs: git\n",
|
||||||
path: "/mygit",
|
path: "/mygit",
|
||||||
goImport: "example.com/mygit git https://bitbucket.org/zombiezen/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}",
|
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",
|
name: "subpath",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
"paths:\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" /portmidi:\n" +
|
||||||
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi/foo",
|
path: "/portmidi/foo",
|
||||||
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
||||||
goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _",
|
goSource: "example.com/portmidi https://github.com/rakyll/portmidi _ _",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "subpath with trailing config slash",
|
name: "subpath with trailing config slash",
|
||||||
config: "/portmidi/:\n" +
|
config: "host: example.com\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
"paths:\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" /portmidi/:\n" +
|
||||||
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi/foo",
|
path: "/portmidi/foo",
|
||||||
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
|
||||||
goSource: "example.com/portmidi 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)
|
t.Errorf("%s: newHandler: %v", test.name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
h.host = "example.com"
|
|
||||||
s := httptest.NewServer(h)
|
s := httptest.NewServer(h)
|
||||||
resp, err := http.Get(s.URL + test.path)
|
resp, err := http.Get(s.URL + test.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -121,11 +132,13 @@ func TestHandler(t *testing.T) {
|
|||||||
|
|
||||||
func TestBadConfigs(t *testing.T) {
|
func TestBadConfigs(t *testing.T) {
|
||||||
badConfigs := []string{
|
badConfigs := []string{
|
||||||
"/missingvcs:\n" +
|
"paths:\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n",
|
" /missingvcs:\n" +
|
||||||
"/unknownvcs:\n" +
|
" repo: https://bitbucket.org/zombiezen/gopdf\n",
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
"paths:\n" +
|
||||||
" vcs: xyzzy\n",
|
" /unknownvcs:\n" +
|
||||||
|
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
||||||
|
" vcs: xyzzy\n",
|
||||||
}
|
}
|
||||||
for _, config := range badConfigs {
|
for _, config := range badConfigs {
|
||||||
_, err := newHandler([]byte(config))
|
_, err := newHandler([]byte(config))
|
||||||
|
2
main.go
2
main.go
@ -41,6 +41,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestHost(r *http.Request) string {
|
func defaultHost(r *http.Request) string {
|
||||||
return r.Host
|
return r.Host
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/portmidi:
|
paths:
|
||||||
repo: https://github.com/rakyll/portmidi
|
/portmidi:
|
||||||
|
repo: https://github.com/rakyll/portmidi
|
||||||
|
|
||||||
/launchpad:
|
/launchpad:
|
||||||
repo: https://github.com/rakyll/launchpad
|
repo: https://github.com/rakyll/launchpad
|
||||||
|
Loading…
Reference in New Issue
Block a user