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,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.
```
paths:
/portmidi:
repo: https://github.com/rakyll/portmidi
```

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 {
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,7 +34,9 @@ func TestHandler(t *testing.T) {
}{
{
name: "explicit display",
config: "/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",
@ -43,7 +45,9 @@ func TestHandler(t *testing.T) {
},
{
name: "display GitHub inference",
config: "/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",
@ -51,7 +55,9 @@ func TestHandler(t *testing.T) {
},
{
name: "Bitbucket Mercurial",
config: "/gopdf:\n" +
config: "host: example.com\n" +
"paths:\n" +
" /gopdf:\n" +
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
" vcs: hg\n",
path: "/gopdf",
@ -60,7 +66,9 @@ func TestHandler(t *testing.T) {
},
{
name: "Bitbucket Git",
config: "/mygit:\n" +
config: "host: example.com\n" +
"paths:\n" +
" /mygit:\n" +
" repo: https://bitbucket.org/zombiezen/mygit\n" +
" vcs: git\n",
path: "/mygit",
@ -69,7 +77,9 @@ func TestHandler(t *testing.T) {
},
{
name: "subpath",
config: "/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",
@ -78,7 +88,9 @@ func TestHandler(t *testing.T) {
},
{
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" +
" display: https://github.com/rakyll/portmidi _ _\n",
path: "/portmidi/foo",
@ -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,8 +132,10 @@ func TestHandler(t *testing.T) {
func TestBadConfigs(t *testing.T) {
badConfigs := []string{
"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",

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,3 +1,4 @@
paths:
/portmidi:
repo: https://github.com/rakyll/portmidi