ソースを参照

chore: Factor out duplicate virtual path code

Richard Hansen 3 年 前
コミット
491642b1e9
1 ファイル変更22 行追加30 行削除
  1. 22 30
      nginx.tmpl

+ 22 - 30
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")) }}