diff --git a/start.sh b/start.sh index 3f8856b..84d7f72 100644 --- a/start.sh +++ b/start.sh @@ -1,7 +1,7 @@ #!/bin/bash # Better signal handling - forward signals to child processes -trap 'kill -TERM $SERVER_PID; kill -TERM $CADDY_PID; exit' TERM INT +trap 'kill -TERM $SERVER_PID; kill -TERM $NGINX_PID; exit' TERM INT set -eu @@ -172,7 +172,7 @@ fi # Source S3 configuration if [ -f /app/data/config/s3.env ]; then echo "==> Sourcing S3 configuration from /app/data/config/s3.env" - source /app/data/config/s3.env +source /app/data/config/s3.env fi # Display S3 configuration (masking sensitive values) @@ -213,12 +213,9 @@ sed -i 's|s3.are_local_buckets: true|s3.are_local_buckets: false|g' /app/data/co # Install or verify required packages echo "==> Checking for required packages" -if ! command -v caddy &> /dev/null; then - echo "==> Installing Caddy" - apt-get update && apt-get install -y debian-keyring debian-archive-keyring apt-transport-https - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list - apt-get update && apt-get install -y caddy +if ! command -v nginx &> /dev/null; then + echo "==> Installing NGINX" + apt-get update && apt-get install -y nginx fi # Set up the API endpoint for the web apps @@ -235,6 +232,8 @@ echo "==> Set environment variables for web apps" # Create directory for configuration files mkdir -p /app/data/public mkdir -p /app/data/scripts +mkdir -p /app/data/nginx +mkdir -p /app/data/logs/nginx # Create a debugging script cat > /app/data/public/debug.js < /app/data/public/debug.js < /app/data/public/debug.js < /app/data/public/config.js < /app/data/public/debug.html < @@ -417,39 +370,73 @@ cat > /app/data/public/debug.html < EOT -# Create a simple app that loads the config and redirects to the main app -cat > /app/data/public/index.html < - - - - Ente - - - - -

Ente

-

Welcome to Ente!

-

Debug Information

- - +# Create a configuration script with properly formatted URL +cat > /app/data/public/config.js < Setting up Caddy server" -mkdir -p /app/data/caddy -chmod -R 777 /app/data/caddy +# Set up NGINX +echo "==> Setting up NGINX server" # Define ports -CADDY_PORT=3080 +NGINX_PORT=3080 API_PORT=8080 # Check if ports are available echo "==> Checking port availability" -if lsof -i:$CADDY_PORT > /dev/null 2>&1; then - echo "==> WARNING: Port $CADDY_PORT is already in use" +if lsof -i:$NGINX_PORT > /dev/null 2>&1; then + echo "==> WARNING: Port $NGINX_PORT is already in use" else - echo "==> Port $CADDY_PORT is available for Caddy" + echo "==> Port $NGINX_PORT is available for NGINX" fi if lsof -i:$API_PORT > /dev/null 2>&1; then @@ -458,230 +445,156 @@ else echo "==> Port $API_PORT is available for API server" fi -# Create the Caddyfile -cat > /app/data/caddy/Caddyfile < /app/data/nginx/nginx.conf <' ''; + sub_filter_once on; + sub_filter_types text/html; + } + + # Accounts app + location /accounts/ { + alias /app/web/accounts/; + try_files \$uri \$uri/ /accounts/index.html; + + # Insert config script for HTML files + sub_filter '' ''; + sub_filter_once on; + sub_filter_types text/html; + } + + # Auth app + location /auth/ { + alias /app/web/auth/; + try_files \$uri \$uri/ /auth/index.html; + + # Insert config script for HTML files + sub_filter '' ''; + sub_filter_once on; + sub_filter_types text/html; + } + + # Cast app + location /cast/ { + alias /app/web/cast/; + try_files \$uri \$uri/ /cast/index.html; + + # Insert config script for HTML files + sub_filter '' ''; + sub_filter_once on; + sub_filter_types text/html; } - } - - # Proxy API calls to the backend server - handle /api* { - reverse_proxy localhost:$API_PORT } } EOT -echo "==> Created Caddyfile at /app/data/caddy/Caddyfile" +echo "==> Created NGINX config at /app/data/nginx/nginx.conf" -# Create injected HTML files for each app -for app_name in "photos" "accounts" "auth" "cast"; do - # Create an injected HTML file that loads the config - cat > "/app/data/public/${app_name}-injected.html" < - - - - Ente ${app_name^} - - - - - -
-

Loading Ente ${app_name^}...

-

Please wait while the application initializes.

-
- - -EOT -done +# Start NGINX +echo "==> Starting NGINX on port $NGINX_PORT" +nginx -c /app/data/nginx/nginx.conf -p /app/data/nginx & +NGINX_PID=$! +echo "==> NGINX started with PID $NGINX_PID" -echo "==> Created injected HTML files for all apps" - -# Start Caddy -echo "==> Starting Caddy on port $CADDY_PORT" -caddy run --config /app/data/caddy/Caddyfile --adapter caddyfile & -CADDY_PID=$! -echo "==> Caddy started with PID $CADDY_PID" - -# Wait for Caddy to start +# Wait for NGINX to start sleep 2 -# Test Caddy connectivity -echo "==> Testing Caddy connectivity" +# Test NGINX connectivity +echo "==> Testing NGINX connectivity" for i in {1..5}; do - if curl -s --max-time 2 --head --fail http://localhost:$CADDY_PORT/health > /dev/null; then - echo "==> Caddy is running properly on port $CADDY_PORT" + if curl -s --max-time 2 --head --fail http://localhost:$NGINX_PORT/health > /dev/null; then + echo "==> NGINX is running properly on port $NGINX_PORT" break else if [ $i -eq 5 ]; then - echo "==> Failed to connect to Caddy after multiple attempts" - echo "==> Last 20 lines of Caddy log:" - tail -20 /app/data/caddy/caddy.log || echo "==> No Caddy log available" + echo "==> Failed to connect to NGINX after multiple attempts" + echo "==> Last 20 lines of NGINX error log:" + tail -20 /app/data/logs/nginx/error.log || echo "==> No NGINX error log available" echo "==> Network ports in use:" netstat -tuln || echo "==> netstat command not available" else - echo "==> Attempt $i: Waiting for Caddy to start... (1 second)" + echo "==> Attempt $i: Waiting for NGINX to start... (1 second)" sleep 1 fi fi @@ -903,4 +816,4 @@ echo "==> To view debug information, visit: $CLOUDRON_APP_ORIGIN/debug" echo "==> Entering wait state - press Ctrl+C to stop" # Wait for all background processes to complete (or for user to interrupt) wait $SERVER_PID -wait $CADDY_PID \ No newline at end of file +wait $NGINX_PID \ No newline at end of file