From 491642b1e92cb1c058ebd72d6bc0074eec6af387 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 5 Apr 2022 04:59:54 -0400 Subject: [PATCH] chore: Factor out duplicate virtual path code --- nginx.tmpl | 52 ++++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 10dd720..c2409e1 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -269,15 +269,17 @@ server { {{ $nPaths := len $paths }} {{ if eq $nPaths 0 }} - # {{ $host }} - {{ template "upstream" (dict "Upstream" $upstream_name "Containers" $containers "Networks" $CurrentContainer.Networks "Debug" $debug_all) }} -{{ else }} - {{ range $path, $containers := $paths }} + {{ $paths = dict "/" $containers }} +{{ end }} + +{{ range $path, $containers := $paths }} + {{ $upstream := $upstream_name }} + {{ if gt $nPaths 0 }} {{ $sum := sha1 $path }} - {{ $upstream := printf "%s-%s" $upstream_name $sum }} + {{ $upstream = printf "%s-%s" $upstream $sum }} + {{ end }} # {{ $host }}{{ $path }} {{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $CurrentContainer.Networks "Debug" $debug_all) }} - {{ end }} {{ end }} {{ $default_host := or ($.Env.DEFAULT_HOST) "" }} @@ -389,23 +391,19 @@ server { include /etc/nginx/vhost.d/default; {{ 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 }} {{/* 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") }} {{/* 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" }} - {{ $sum := sha1 $path }} - {{ $upstream := printf "%s-%s" $upstream_name $sum }} - {{ $dest := (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} + {{ $upstream := $upstream_name }} + {{ $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) }} {{ end }} {{ if (not (contains $paths "/")) }} @@ -413,7 +411,6 @@ server { return {{ $default_root_response }}; } {{ end }} - {{ end }} } {{ end }} @@ -437,23 +434,19 @@ server { include /etc/nginx/vhost.d/default; {{ 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 }} {{/* 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") }} {{/* 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" }} - {{ $sum := sha1 $path }} - {{ $upstream := printf "%s-%s" $upstream_name $sum }} - {{ $dest := (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} + {{ $upstream := $upstream_name }} + {{ $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) }} {{ end }} {{ if (not (contains $paths "/")) }} @@ -461,7 +454,6 @@ server { return {{ $default_root_response }}; } {{ end }} - {{ end }} } {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}