Support paths with VIRTUAL_PATH

* Require `dict` and `sha1` function support in docker-gen
This commit is contained in:
Mike Dillon 2014-10-26 09:19:58 -07:00
parent 1aef35678e
commit 73c4afbd22

View file

@ -35,10 +35,9 @@ server {
return 503;
}
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
upstream {{ $host }} {
{{ range $container := $containers }}
{{ define "upstream" }}
upstream {{ .Host }}{{ .Suffix }} {
{{ range $index, $container := .Containers }}
{{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
@ -65,6 +64,20 @@ upstream {{ $host }} {
{{ end }}
{{ end }}
}
{{ end }}
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
{{ $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
{{ $pathCount := len $paths }}
{{ if eq $pathCount 0 }}
{{ template "upstream" dict "Host" $host "Suffix" "" "Containers" $containers }}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
{{ template "upstream" dict "Host" $host "Suffix" $suffix "Containers" $containers }}
{{ end }}
{{ end }}
{{/* Get the first cert name defined by containers w/ the same vhost */}}
{{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
@ -102,18 +115,38 @@ server {
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
{{ if eq $pathCount 0 }}
location / {
proxy_pass http://{{ $host }};
}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
location {{ $path }} {
proxy_pass http://{{ $host }}{{ $suffix }};
}
{{ end }}
{{ end }}
}
{{ else }}
server {
server_name {{ $host }};
{{ if eq $pathCount 0 }}
location / {
proxy_pass http://{{ $host }};
}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
location {{ $path }} {
proxy_pass http://{{ $host }}{{ $suffix }};
}
{{ end }}
{{ end }}
}
server {