From 718014446f28791fd4b6f9b850f1c51577ec6a9b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 7 Nov 2024 02:40:29 +0100 Subject: [PATCH] Generate `nginx` config using `printf` --- .../docker/services/anki-sync/anki.conf | 12 --------- .../docker/services/anki-sync/main.fish | 8 +++--- .../Software/docker/services/service.fish | 25 +++++++++++++++---- .../docker/services/vaultwarden/main.fish | 8 +++--- .../services/vaultwarden/vaultwarden.conf | 12 --------- 5 files changed, 28 insertions(+), 37 deletions(-) delete mode 100644 scripts/Common/Software/docker/services/anki-sync/anki.conf delete mode 100644 scripts/Common/Software/docker/services/vaultwarden/vaultwarden.conf diff --git a/scripts/Common/Software/docker/services/anki-sync/anki.conf b/scripts/Common/Software/docker/services/anki-sync/anki.conf deleted file mode 100644 index 798e4939..00000000 --- a/scripts/Common/Software/docker/services/anki-sync/anki.conf +++ /dev/null @@ -1,12 +0,0 @@ -server { - listen 80; - server_name anki.nuth.ch; - - location / { - proxy_pass http://127.0.0.1:1337; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } -} diff --git a/scripts/Common/Software/docker/services/anki-sync/main.fish b/scripts/Common/Software/docker/services/anki-sync/main.fish index c6545f9a..9c849081 100644 --- a/scripts/Common/Software/docker/services/anki-sync/main.fish +++ b/scripts/Common/Software/docker/services/anki-sync/main.fish @@ -4,6 +4,10 @@ begin set -l source "$dir/docker-compose.secrets.yml" source "$dir/../service.fish" + function getServiceSubdomain + echo "anki" + end + function installSW -V dir -V source set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32") set -l userKey ".services.anki.environment.SYNC_USER1" @@ -21,10 +25,6 @@ begin configureDockerService $argv end - function getServiceNginxConfigSource -V dir - echo "$dir/anki.conf" - end - function getServiceConfigs printf "%s\0" "anki" "/" end diff --git a/scripts/Common/Software/docker/services/service.fish b/scripts/Common/Software/docker/services/service.fish index aec1e1b2..39200084 100644 --- a/scripts/Common/Software/docker/services/service.fish +++ b/scripts/Common/Software/docker/services/service.fish @@ -12,6 +12,14 @@ begin echo "$_flag_name" end + function getServiceDomain + echo "$(getServiceSubdomain $argv).nuth.ch" + end + + function getServiceSubdomain + getServiceName $argv + end + function getServiceRoot -V root echo "$root/$(getServiceName $argv)" end @@ -36,7 +44,6 @@ begin function installDockerService -V dir -V nginxRoot -V portPattern set -l services (getServiceConfigs $argv | string split0) - sudo cp (getServiceNginxConfigSource $argv) (__getServiceNginxConfig $argv) for i in (seq 1 2 (count $services)) set -l file (mktemp) @@ -64,10 +71,18 @@ begin set -l port (yq --raw-output "$portKey" "$config" | sed "s/$portPattern/\2/") set -l nginxConfig (__getServiceNginxConfig $argv) - cat "$nginxConfig" | sed \ - -e "\;location $location {;,/}/{" \ - -e "s/\(proxy_pass \)\(.\+:\)\?[[:digit:]]\+\(;\)/\1\2$port\3/;" \ - -e "}" | sudo tee "$nginxConfig" >/dev/null + printf "%s\n" \ + "server {" \ + "listen 80;" \ + "server_name $(getServiceDomain $argv);" \ + "location $location {" \ + "proxy_pass http://127.0.0.1:$port;" \ + 'proxy_set_header Host $host;' \ + 'proxy_set_header X-Real-IP $remote_addr;' \ + 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \ + 'proxy_set_header X-Forwarded-Proto $scheme;' \ + "}" \ + "}" | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null end sudo systemctl restart nginx diff --git a/scripts/Common/Software/docker/services/vaultwarden/main.fish b/scripts/Common/Software/docker/services/vaultwarden/main.fish index 1591cf40..9e0892f4 100644 --- a/scripts/Common/Software/docker/services/vaultwarden/main.fish +++ b/scripts/Common/Software/docker/services/vaultwarden/main.fish @@ -4,6 +4,10 @@ begin set -l source "$dir/docker-compose.secrets.yml" source "$dir/../service.fish" + function getServiceSubdomain + echo "passwords" + end + function installSW -V dir -V source set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32") set -l dbKey ".services.vaultwarden.environment.DATABASE_URL" @@ -22,10 +26,6 @@ begin configureDockerService $argv end - function getServiceNginxConfigSource -V dir - echo "$dir/vaultwarden.conf" - end - function getServiceConfigs argparse -i "name=" -- $argv printf "%s\0" "$_flag_name" "/" diff --git a/scripts/Common/Software/docker/services/vaultwarden/vaultwarden.conf b/scripts/Common/Software/docker/services/vaultwarden/vaultwarden.conf deleted file mode 100644 index ac540702..00000000 --- a/scripts/Common/Software/docker/services/vaultwarden/vaultwarden.conf +++ /dev/null @@ -1,12 +0,0 @@ -server { - listen 80; - server_name passwords.nuth.ch; - - location / { - proxy_pass http://127.0.0.1:1337; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } -}