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,6 +23,7 @@ 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.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
paths:
|
||||||
/portmidi:
|
/portmidi:
|
||||||
repo: https://github.com/rakyll/portmidi
|
repo: https://github.com/rakyll/portmidi
|
||||||
```
|
```
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
|
13
handler.go
13
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 {
|
||||||
|
Host string `yaml:"host,omitempty"`
|
||||||
|
Paths map[string]struct {
|
||||||
Repo string `yaml:"repo,omitempty"`
|
Repo string `yaml:"repo,omitempty"`
|
||||||
Display string `yaml:"display,omitempty"`
|
Display string `yaml:"display,omitempty"`
|
||||||
VCS string `yaml:"vcs,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,7 +34,9 @@ func TestHandler(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "explicit display",
|
name: "explicit display",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /portmidi:\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi",
|
path: "/portmidi",
|
||||||
@ -43,7 +45,9 @@ func TestHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "display GitHub inference",
|
name: "display GitHub inference",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /portmidi:\n" +
|
||||||
" repo: https://github.com/rakyll/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",
|
||||||
@ -51,7 +55,9 @@ func TestHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Bitbucket Mercurial",
|
name: "Bitbucket Mercurial",
|
||||||
config: "/gopdf:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /gopdf:\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
||||||
" vcs: hg\n",
|
" vcs: hg\n",
|
||||||
path: "/gopdf",
|
path: "/gopdf",
|
||||||
@ -60,7 +66,9 @@ func TestHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Bitbucket Git",
|
name: "Bitbucket Git",
|
||||||
config: "/mygit:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /mygit:\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/mygit\n" +
|
" repo: https://bitbucket.org/zombiezen/mygit\n" +
|
||||||
" vcs: git\n",
|
" vcs: git\n",
|
||||||
path: "/mygit",
|
path: "/mygit",
|
||||||
@ -69,7 +77,9 @@ func TestHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "subpath",
|
name: "subpath",
|
||||||
config: "/portmidi:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /portmidi:\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi/foo",
|
path: "/portmidi/foo",
|
||||||
@ -78,7 +88,9 @@ func TestHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "subpath with trailing config slash",
|
name: "subpath with trailing config slash",
|
||||||
config: "/portmidi/:\n" +
|
config: "host: example.com\n" +
|
||||||
|
"paths:\n" +
|
||||||
|
" /portmidi/:\n" +
|
||||||
" repo: https://github.com/rakyll/portmidi\n" +
|
" repo: https://github.com/rakyll/portmidi\n" +
|
||||||
" display: https://github.com/rakyll/portmidi _ _\n",
|
" display: https://github.com/rakyll/portmidi _ _\n",
|
||||||
path: "/portmidi/foo",
|
path: "/portmidi/foo",
|
||||||
@ -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,8 +132,10 @@ func TestHandler(t *testing.T) {
|
|||||||
|
|
||||||
func TestBadConfigs(t *testing.T) {
|
func TestBadConfigs(t *testing.T) {
|
||||||
badConfigs := []string{
|
badConfigs := []string{
|
||||||
|
"paths:\n" +
|
||||||
" /missingvcs:\n" +
|
" /missingvcs:\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n",
|
" repo: https://bitbucket.org/zombiezen/gopdf\n",
|
||||||
|
"paths:\n" +
|
||||||
" /unknownvcs:\n" +
|
" /unknownvcs:\n" +
|
||||||
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
|
||||||
" vcs: xyzzy\n",
|
" vcs: xyzzy\n",
|
||||||
|
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,3 +1,4 @@
|
|||||||
|
paths:
|
||||||
/portmidi:
|
/portmidi:
|
||||||
repo: https://github.com/rakyll/portmidi
|
repo: https://github.com/rakyll/portmidi
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user