Skip to content

Commit 837f5f5

Browse files
committed
Refactor launch script in Docker deployment to dynamically set web root and update Nginx configuration based on BASE_HREF and PROXY_BASE_HREF
1 parent 24433fe commit 837f5f5

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

docker/deploy-local-build/start.sh

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,57 @@ else
1616
echo "Using existing SSL certificates."
1717
fi
1818

19-
# Set base href
20-
sed -i "s|<base href=\"[^\"]*\"|<base href=\"${BASE_HREF}\"|g" /usr/share/nginx/html/index.html
19+
# Determine web root based on BASE_HREF
20+
WEB_ROOT="/usr/share/nginx/html"
21+
if [ "$BASE_HREF" != "/" ]; then
22+
# Ensure BASE_HREF starts with a single slash and does not end with a slash
23+
# Example: /bdip
24+
BASE_HREF=$(echo "$BASE_HREF" | sed 's|^\(/*\)|/|; s|/*$||')
25+
26+
WEB_ROOT="/usr/share/nginx/html${BASE_HREF}"
27+
28+
# Use a temporary directory to move files without including the target directory
29+
TEMP_DIR=$(mktemp -d)
30+
mv /usr/share/nginx/html/* "$TEMP_DIR"
31+
# Create the new web root directory
32+
mkdir -p "$WEB_ROOT"
33+
# Move from the temporary directory to the new web root
34+
mv "$TEMP_DIR"/* "$WEB_ROOT"
35+
# Clean up the temporary directory
36+
rmdir "$TEMP_DIR"
37+
38+
# Update Nginx try_files directive to reflect BASE_HREF
39+
sed -i "s|try_files \$uri \$uri/ /index.html;|try_files \$uri \$uri/ ${BASE_HREF}/index.html;|g" /etc/nginx/conf.d/default.conf
40+
fi
41+
42+
# Set base href in index.html
43+
sed -i "s|<base href=\"[^\"]*\"|<base href=\"${BASE_HREF%/}/\"|g" "$WEB_ROOT"/index.html
2144

2245
# Ensure PROXY_BASE_HREF is set, defaulting to BASE_HREF if not provided
2346
PROXY_BASE_HREF=${PROXY_BASE_HREF:-$BASE_HREF}
47+
if [ "$PROXY_BASE_HREF" != "/" ]; then
48+
# Ensure PROXY_BASE_HREF starts and ends with a slash
49+
# Example: /bdip-proxy/
50+
PROXY_BASE_HREF=$(echo "$PROXY_BASE_HREF" | sed 's|^\(/*\)|/|; s|/*$|/|')
51+
fi
52+
# Update Nginx configuration for proxy
53+
if [ "$PROXY_BASE_HREF" != "/" ]; then
54+
# Replace proxy_pass with upstream_endpoint
55+
sed -i "/location \/bdip-proxy\/v2\/namespaces\/pegi3s\/repositories/{
56+
:a
57+
n
58+
/^[[:space:]]*proxy_pass/{
59+
i\
60+
rewrite ^$PROXY_BASE_HREF(.*)$ /\$1 break;
61+
s|proxy_pass .*;|proxy_pass \$upstream_endpoint;|
62+
b
63+
}
64+
ba
65+
}" /etc/nginx/conf.d/default.conf
66+
fi
67+
sed -i "s|/v2/namespaces/pegi3s/repositories|${PROXY_BASE_HREF}v2/namespaces/pegi3s/repositories|g" /etc/nginx/conf.d/default.conf
2468
# Set proxy base href
25-
sed -i "s|/v2/namespaces/pegi3s/repositories|${PROXY_BASE_HREF}v2/namespaces/pegi3s/repositories|g" /usr/share/nginx/html/main*.js
69+
sed -i "s|/v2/namespaces/pegi3s/repositories|${PROXY_BASE_HREF}v2/namespaces/pegi3s/repositories|g" "$WEB_ROOT"/main*.js
2670

2771
# Start nginx
2872
echo "Starting Nginx..."

0 commit comments

Comments
 (0)