mirror of
https://github.com/thib8956/nginx-proxy
synced 2025-02-24 09:48:14 +00:00
refactor: cleanup template
This commit is contained in:
parent
87e5b58b77
commit
fc98f4c953
57
nginx.tmpl
57
nginx.tmpl
@ -346,12 +346,9 @@ upstream {{ $vpath.upstream }} {
|
|||||||
* The provided dot dict is expected to have the following entries:
|
* The provided dot dict is expected to have the following entries:
|
||||||
* - "Containers": List of container's RuntimeContainer struct.
|
* - "Containers": List of container's RuntimeContainer struct.
|
||||||
* - "Upstream_name"
|
* - "Upstream_name"
|
||||||
* - "Has_virtual_paths": boolean
|
|
||||||
* - "Multiport_syntax": boolean
|
|
||||||
* - "Path"
|
* - "Path"
|
||||||
*
|
*
|
||||||
* The return values will be added to the dot dict with keys:
|
* The return values will be added to the dot dict with keys:
|
||||||
* - "dest"
|
|
||||||
* - "proto"
|
* - "proto"
|
||||||
* - "network_tag"
|
* - "network_tag"
|
||||||
* - "upstream"
|
* - "upstream"
|
||||||
@ -368,22 +365,13 @@ upstream {{ $vpath.upstream }} {
|
|||||||
{{- $keepalive := coalesce (first (keys (groupByLabel $.Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
|
{{- $keepalive := coalesce (first (keys (groupByLabel $.Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
|
||||||
|
|
||||||
{{- $upstream := $.Upstream_name }}
|
{{- $upstream := $.Upstream_name }}
|
||||||
{{- $dest := "" }}
|
{{- if (not (eq $.Path "/")) }}
|
||||||
{{- if $.Has_virtual_paths }}
|
|
||||||
{{- $sum := sha1 $.Path }}
|
{{- $sum := sha1 $.Path }}
|
||||||
{{- $upstream = printf "%s-%s" $upstream $sum }}
|
{{- $upstream = printf "%s-%s" $upstream $sum }}
|
||||||
{{- $dest = or (first (groupByKeys $.Containers "Env.VIRTUAL_DEST")) "" }}
|
|
||||||
{{- end }}
|
|
||||||
{{- if $.Multiport_syntax }}
|
|
||||||
{{- if (not (eq $.Path "/")) }}
|
|
||||||
{{- $sum := sha1 $.Path }}
|
|
||||||
{{- $upstream = printf "%s-%s" $upstream $sum }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- $_ := set $ "proto" $proto }}
|
{{- $_ := set $ "proto" $proto }}
|
||||||
{{- $_ := set $ "network_tag" $network_tag }}
|
{{- $_ := set $ "network_tag" $network_tag }}
|
||||||
{{- $_ := set $ "upstream" $upstream }}
|
{{- $_ := set $ "upstream" $upstream }}
|
||||||
{{- $_ := set $ "dest" $dest }}
|
|
||||||
{{- $_ := set $ "loadbalance" $loadbalance }}
|
{{- $_ := set $ "loadbalance" $loadbalance }}
|
||||||
{{- $_ := set $ "keepalive" $keepalive }}
|
{{- $_ := set $ "keepalive" $keepalive }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
@ -507,14 +495,16 @@ proxy_set_header X-Original-URI $request_uri;
|
|||||||
proxy_set_header Proxy "";
|
proxy_set_header Proxy "";
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- /* Precompute some information about each vhost. */}}
|
{{- /* Precompute some information about vhost that use VIRTUAL_HOST_YAML. */}}
|
||||||
{{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_YAML" }}
|
{{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_YAML" }}
|
||||||
{{- range $hostname, $vhost := (fromYaml $vhosts_yaml) }}
|
{{- range $hostname, $vhost := (fromYaml $vhosts_yaml) }}
|
||||||
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
|
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
|
||||||
{{- $paths := coalesce $vhost_data.paths (dict) }}
|
{{- $paths := coalesce $vhost_data.paths (dict) }}
|
||||||
|
|
||||||
{{- if (empty $vhost) }}
|
{{- if (empty $vhost) }}
|
||||||
{{ $vhost = dict "/" (dict) }}
|
{{ $vhost = dict "/" (dict) }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- range $path, $vpath := $vhost }}
|
{{- range $path, $vpath := $vhost }}
|
||||||
{{- $dest := coalesce $vpath.dest "" }}
|
{{- $dest := coalesce $vpath.dest "" }}
|
||||||
{{- $port := when (hasKey $vpath "port") (toString $vpath.port) "default" }}
|
{{- $port := when (hasKey $vpath "port") (toString $vpath.port) "default" }}
|
||||||
@ -532,18 +522,19 @@ proxy_set_header Proxy "";
|
|||||||
{{- $_ := set $vhost_data "paths" $paths }}
|
{{- $_ := set $vhost_data "paths" $paths }}
|
||||||
{{- $is_regexp := hasPrefix "~" $hostname }}
|
{{- $is_regexp := hasPrefix "~" $hostname }}
|
||||||
{{- $_ := set $vhost_data "upstream_name" (when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname) }}
|
{{- $_ := set $vhost_data "upstream_name" (when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname) }}
|
||||||
{{- $_ := set $vhost_data "has_virtual_paths" false }}
|
|
||||||
{{- $_ := set $vhost_data "multiport_syntax" true }}
|
|
||||||
{{- $_ := set $globals.vhosts $hostname $vhost_data }}
|
{{- $_ := set $globals.vhosts $hostname $vhost_data }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
|
{{- /* Precompute some information about vhost that use VIRTUAL_HOST. */}}
|
||||||
{{- range $hostname, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
|
{{- range $hostname, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
|
||||||
|
{{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
|
||||||
{{- $hostname = trim $hostname }}
|
{{- $hostname = trim $hostname }}
|
||||||
{{- if not $hostname }}
|
{{- if not $hostname }}
|
||||||
{{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
|
|
||||||
{{- continue }}
|
{{- continue }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
|
{{/* Drop containers with VIRTUAL_HOST_YAML set (VIRTUAL_HOST_YAML takes precedence). */}}
|
||||||
{{- range $_, $containers_to_drop := groupBy $containers "Env.VIRTUAL_HOST_YAML" }}
|
{{- range $_, $containers_to_drop := groupBy $containers "Env.VIRTUAL_HOST_YAML" }}
|
||||||
{{- range $container := $containers_to_drop }}
|
{{- range $container := $containers_to_drop }}
|
||||||
{{- $containers = without $containers $container }}
|
{{- $containers = without $containers $container }}
|
||||||
@ -552,36 +543,32 @@ proxy_set_header Proxy "";
|
|||||||
{{- if (eq (len $containers) 0) }}
|
{{- if (eq (len $containers) 0) }}
|
||||||
{{- continue }}
|
{{- continue }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
|
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
|
||||||
|
|
||||||
{{- $is_regexp := hasPrefix "~" $hostname }}
|
|
||||||
{{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname }}
|
|
||||||
|
|
||||||
{{- $has_virtual_paths := false }}
|
|
||||||
{{- $paths := coalesce $vhost_data.paths (dict) }}
|
{{- $paths := coalesce $vhost_data.paths (dict) }}
|
||||||
|
|
||||||
{{- $tmp_paths := groupBy $containers "Env.VIRTUAL_PATH" }}
|
{{- $tmp_paths := groupBy $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{- $has_virtual_paths = gt (len $tmp_paths) 0}}
|
{{- if not (gt (len $tmp_paths) 0) }}
|
||||||
{{- if not $has_virtual_paths }}
|
|
||||||
{{- $tmp_paths = dict "/" $containers }}
|
{{- $tmp_paths = dict "/" $containers }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- range $path, $containers := $tmp_paths }}
|
{{- range $path, $containers := $tmp_paths }}
|
||||||
|
{{- $dest := or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "" }}
|
||||||
|
{{- $port := "legacy" }}
|
||||||
{{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
|
{{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
|
||||||
{{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
|
{{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
|
||||||
{{- $port := "legacy" }}
|
|
||||||
{{- $path_port_containers := when (hasKey $path_ports $port) (get $path_ports $port) (list) }}
|
{{- $path_port_containers := when (hasKey $path_ports $port) (get $path_ports $port) (list) }}
|
||||||
{{- $path_port_containers = concat $path_port_containers $containers }}
|
{{- $path_port_containers = concat $path_port_containers $containers }}
|
||||||
|
|
||||||
{{- $_ := set $path_ports $port $path_port_containers }}
|
{{- $_ := set $path_ports $port $path_port_containers }}
|
||||||
{{- $_ := set $path_data "ports" $path_ports }}
|
{{- $_ := set $path_data "ports" $path_ports }}
|
||||||
{{- if (not (hasKey $path_data "dest")) }}
|
{{- if (not (hasKey $path_data "dest")) }}
|
||||||
{{- $_ := set $path_data "dest" (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
|
{{- $_ := set $path_data "dest" $dest }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- $_ := set $paths $path $path_data }}
|
{{- $_ := set $paths $path $path_data }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- $_ := set $vhost_data "paths" $paths }}
|
{{- $_ := set $vhost_data "paths" $paths }}
|
||||||
{{- $_ := set $vhost_data "upstream_name" $upstream_name }}
|
{{- $is_regexp := hasPrefix "~" $hostname }}
|
||||||
{{- $_ := set $vhost_data "has_virtual_paths" $has_virtual_paths }}
|
{{- $_ := set $vhost_data "upstream_name" (when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname) }}
|
||||||
{{- $_ := set $vhost_data "multiport_syntax" false }}
|
|
||||||
{{- $_ := set $globals.vhosts $hostname $vhost_data }}
|
{{- $_ := set $globals.vhosts $hostname $vhost_data }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
@ -593,11 +580,13 @@ proxy_set_header Proxy "";
|
|||||||
{{ $vpath_containers = concat $vpath_containers $vport_containers }}
|
{{ $vpath_containers = concat $vpath_containers $vport_containers }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- $args := dict "Containers" $vpath_containers "Path" $path "Upstream_name" $vhost_data.upstream_name "Has_virtual_paths" $vhost_data.has_virtual_paths "Multiport_syntax" $vhost_data.multiport_syntax }}
|
{{- $args := (dict
|
||||||
|
"Containers" $vpath_containers
|
||||||
|
"Path" $path
|
||||||
|
"Upstream_name" $vhost_data.upstream_name
|
||||||
|
)}}
|
||||||
|
|
||||||
{{- template "get_path_info" $args }}
|
{{- template "get_path_info" $args }}
|
||||||
{{- if $vhost_data.has_virtual_paths }}
|
|
||||||
{{- $_ := set $vpath_data "dest" $args.dest }}
|
|
||||||
{{- end }}
|
|
||||||
{{- $_ := set $vpath_data "proto" $args.proto }}
|
{{- $_ := set $vpath_data "proto" $args.proto }}
|
||||||
{{- $_ := set $vpath_data "network_tag" $args.network_tag }}
|
{{- $_ := set $vpath_data "network_tag" $args.network_tag }}
|
||||||
{{- $_ := set $vpath_data "upstream" $args.upstream }}
|
{{- $_ := set $vpath_data "upstream" $args.upstream }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user