Generate nginx config using printf

This commit is contained in:
Manuel Thalmann 2024-11-07 02:40:29 +01:00
parent 1f4fef116a
commit 5e0ca4ec47
5 changed files with 28 additions and 37 deletions

View file

@ -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;
}
}

View file

@ -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

View file

@ -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

View file

@ -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" "/"

View file

@ -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;
}
}