1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 09:48:14 +00:00

chore: Factor out duplicate virtual path code

This commit is contained in:
Richard Hansen 2022-04-05 04:59:54 -04:00
parent 14d0f3f222
commit 491642b1e9

View File

@ -269,15 +269,17 @@ server {
{{ $nPaths := len $paths }} {{ $nPaths := len $paths }}
{{ if eq $nPaths 0 }} {{ if eq $nPaths 0 }}
# {{ $host }} {{ $paths = dict "/" $containers }}
{{ template "upstream" (dict "Upstream" $upstream_name "Containers" $containers "Networks" $CurrentContainer.Networks "Debug" $debug_all) }} {{ end }}
{{ else }}
{{ range $path, $containers := $paths }} {{ range $path, $containers := $paths }}
{{ $upstream := $upstream_name }}
{{ if gt $nPaths 0 }}
{{ $sum := sha1 $path }} {{ $sum := sha1 $path }}
{{ $upstream := printf "%s-%s" $upstream_name $sum }} {{ $upstream = printf "%s-%s" $upstream $sum }}
{{ end }}
# {{ $host }}{{ $path }} # {{ $host }}{{ $path }}
{{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $CurrentContainer.Networks "Debug" $debug_all) }} {{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $CurrentContainer.Networks "Debug" $debug_all) }}
{{ end }}
{{ end }} {{ end }}
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }} {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
@ -389,23 +391,19 @@ server {
include /etc/nginx/vhost.d/default; include /etc/nginx/vhost.d/default;
{{ end }} {{ end }}
{{ if eq $nPaths 0 }}
{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
{{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
{{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "VhostRoot" $vhost_root "Dest" "" "NetworkTag" $network_tag) }}
{{ else }}
{{ range $path, $containers := $paths }} {{ range $path, $containers := $paths }}
{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http" */}} {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http" */}}
{{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }} {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}} {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
{{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }} {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{ $sum := sha1 $path }} {{ $upstream := $upstream_name }}
{{ $upstream := printf "%s-%s" $upstream_name $sum }} {{ $dest := "" }}
{{ $dest := (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} {{ if gt $nPaths 0 }}
{{ $sum := sha1 $path }}
{{ $upstream = printf "%s-%s" $upstream $sum }}
{{ $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{ end }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }} {{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }}
{{ end }} {{ end }}
{{ if (not (contains $paths "/")) }} {{ if (not (contains $paths "/")) }}
@ -413,7 +411,6 @@ server {
return {{ $default_root_response }}; return {{ $default_root_response }};
} }
{{ end }} {{ end }}
{{ end }}
} }
{{ end }} {{ end }}
@ -437,23 +434,19 @@ server {
include /etc/nginx/vhost.d/default; include /etc/nginx/vhost.d/default;
{{ end }} {{ end }}
{{ if eq $nPaths 0 }}
{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
{{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
{{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "VhostRoot" $vhost_root "Dest" "" "NetworkTag" $network_tag) }}
{{ else }}
{{ range $path, $containers := $paths }} {{ range $path, $containers := $paths }}
{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http" */}} {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http" */}}
{{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }} {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}} {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
{{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }} {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{ $sum := sha1 $path }} {{ $upstream := $upstream_name }}
{{ $upstream := printf "%s-%s" $upstream_name $sum }} {{ $dest := "" }}
{{ $dest := (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} {{ if gt $nPaths 0 }}
{{ $sum := sha1 $path }}
{{ $upstream = printf "%s-%s" $upstream $sum }}
{{ $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{ end }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }} {{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }}
{{ end }} {{ end }}
{{ if (not (contains $paths "/")) }} {{ if (not (contains $paths "/")) }}
@ -461,7 +454,6 @@ server {
return {{ $default_root_response }}; return {{ $default_root_response }};
} }
{{ end }} {{ end }}
{{ end }}
} }
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }} {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}