Switch from Caddy to NGINX and fix URL construction error

This commit is contained in:
Andreas Düren 2025-03-16 23:29:27 +01:00
parent 64b7570cc6
commit 23c9581f7b

425
start.sh
View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Better signal handling - forward signals to child processes # 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 set -eu
@ -172,7 +172,7 @@ fi
# Source S3 configuration # Source S3 configuration
if [ -f /app/data/config/s3.env ]; then if [ -f /app/data/config/s3.env ]; then
echo "==> Sourcing S3 configuration from /app/data/config/s3.env" echo "==> Sourcing S3 configuration from /app/data/config/s3.env"
source /app/data/config/s3.env source /app/data/config/s3.env
fi fi
# Display S3 configuration (masking sensitive values) # 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 # Install or verify required packages
echo "==> Checking for required packages" echo "==> Checking for required packages"
if ! command -v caddy &> /dev/null; then if ! command -v nginx &> /dev/null; then
echo "==> Installing Caddy" echo "==> Installing NGINX"
apt-get update && apt-get install -y debian-keyring debian-archive-keyring apt-transport-https apt-get update && apt-get install -y nginx
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
fi fi
# Set up the API endpoint for the web apps # 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 # Create directory for configuration files
mkdir -p /app/data/public mkdir -p /app/data/public
mkdir -p /app/data/scripts mkdir -p /app/data/scripts
mkdir -p /app/data/nginx
mkdir -p /app/data/logs/nginx
# Create a debugging script # Create a debugging script
cat > /app/data/public/debug.js <<EOT cat > /app/data/public/debug.js <<EOT
@ -242,27 +241,6 @@ cat > /app/data/public/debug.js <<EOT
(function() { (function() {
console.log("Debug script loaded"); console.log("Debug script loaded");
// Intercept URL constructor
const originalURL = window.URL;
window.URL = function(url, base) {
console.log("URL constructor called with:", url, base);
try {
return new originalURL(url, base);
} catch (e) {
console.error("URL construction failed:", e.message);
console.error("URL:", url);
console.error("Base:", base);
console.error("Stack:", e.stack);
// Try to fix common issues
if (url && !url.startsWith("http") && !url.startsWith("/")) {
console.log("Attempting to fix relative URL by adding leading slash");
return new originalURL("/" + url, base);
}
throw e;
}
};
// Create debug overlay // Create debug overlay
const debugDiv = document.createElement('div'); const debugDiv = document.createElement('div');
debugDiv.style.position = 'fixed'; debugDiv.style.position = 'fixed';
@ -306,31 +284,6 @@ cat > /app/data/public/debug.js <<EOT
})(); })();
EOT EOT
# Create a configuration script
cat > /app/data/public/config.js <<EOT
// Direct configuration for Ente
window.ENTE_CONFIG = {
API_URL: "${API_ENDPOINT}"
};
// Next.js environment variables
window.process = window.process || {};
window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = "${API_ENDPOINT}";
window.process.env.REACT_APP_ENTE_ENDPOINT = "${API_ENDPOINT}";
window.process.env.VUE_APP_ENTE_ENDPOINT = "${API_ENDPOINT}";
// Store in localStorage for persistence
try {
localStorage.setItem('ENTE_CONFIG', JSON.stringify(window.ENTE_CONFIG));
localStorage.setItem('NEXT_PUBLIC_ENTE_ENDPOINT', "${API_ENDPOINT}");
} catch (e) {
console.error("Failed to store config in localStorage:", e);
}
console.log("Ente config loaded - API_URL:", window.ENTE_CONFIG.API_URL);
EOT
# Create debug info HTML page # Create debug info HTML page
cat > /app/data/public/debug.html <<EOT cat > /app/data/public/debug.html <<EOT
<!DOCTYPE html> <!DOCTYPE html>
@ -417,39 +370,73 @@ cat > /app/data/public/debug.html <<EOT
</html> </html>
EOT EOT
# Create a simple app that loads the config and redirects to the main app # Create a configuration script with properly formatted URL
cat > /app/data/public/index.html <<EOT cat > /app/data/public/config.js <<EOT
<!DOCTYPE html> // Direct configuration for Ente
<html> window.ENTE_CONFIG = {
<head> API_URL: "${API_ENDPOINT}"
<meta charset="utf-8"> };
<title>Ente</title>
<script src="/config.js"></script> // Next.js environment variables
<script src="/debug.js"></script> window.process = window.process || {};
</head> window.process.env = window.process.env || {};
<body> window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = "${API_ENDPOINT}";
<h1>Ente</h1> window.process.env.REACT_APP_ENTE_ENDPOINT = "${API_ENDPOINT}";
<p>Welcome to Ente!</p> window.process.env.VUE_APP_ENTE_ENDPOINT = "${API_ENDPOINT}";
<p><a href="/debug">Debug Information</a></p>
</body> // Create absolute URL helper function to prevent URL construction errors
</html> window.createApiUrl = function(path) {
// Handle paths with or without leading slash
if (path && path.startsWith('/')) {
return "${API_ENDPOINT}" + path;
} else if (path) {
return "${API_ENDPOINT}/" + path;
}
return "${API_ENDPOINT}";
};
// Store in localStorage for persistence
try {
localStorage.setItem('ENTE_CONFIG', JSON.stringify(window.ENTE_CONFIG));
localStorage.setItem('NEXT_PUBLIC_ENTE_ENDPOINT', "${API_ENDPOINT}");
} catch (e) {
console.error("Failed to store config in localStorage:", e);
}
console.log("Ente config loaded - API_URL:", window.ENTE_CONFIG.API_URL);
// Override URL constructor to prevent errors
const originalURL = window.URL;
window.URL = function(url, base) {
try {
// Fix common URL construction issues
if (url && typeof url === 'string') {
// If URL doesn't have a protocol or start with /, add a /
if (!url.match(/^[a-z]+:\/\//) && !url.startsWith('/') && !base) {
url = '/' + url;
}
}
return new originalURL(url, base);
} catch (e) {
console.error("URL construction error:", e, "url:", url, "base:", base);
// Fallback - return a working URL
return new originalURL("${CLOUDRON_APP_ORIGIN}");
}
};
EOT EOT
# Set up Caddy # Set up NGINX
echo "==> Setting up Caddy server" echo "==> Setting up NGINX server"
mkdir -p /app/data/caddy
chmod -R 777 /app/data/caddy
# Define ports # Define ports
CADDY_PORT=3080 NGINX_PORT=3080
API_PORT=8080 API_PORT=8080
# Check if ports are available # Check if ports are available
echo "==> Checking port availability" echo "==> Checking port availability"
if lsof -i:$CADDY_PORT > /dev/null 2>&1; then if lsof -i:$NGINX_PORT > /dev/null 2>&1; then
echo "==> WARNING: Port $CADDY_PORT is already in use" echo "==> WARNING: Port $NGINX_PORT is already in use"
else else
echo "==> Port $CADDY_PORT is available for Caddy" echo "==> Port $NGINX_PORT is available for NGINX"
fi fi
if lsof -i:$API_PORT > /dev/null 2>&1; then if lsof -i:$API_PORT > /dev/null 2>&1; then
@ -458,230 +445,156 @@ else
echo "==> Port $API_PORT is available for API server" echo "==> Port $API_PORT is available for API server"
fi fi
# Create the Caddyfile # Create the NGINX config
cat > /app/data/caddy/Caddyfile <<EOT cat > /app/data/nginx/nginx.conf <<EOT
{ worker_processes 1;
admin off error_log /app/data/logs/nginx/error.log warn;
auto_https off pid /app/data/nginx/nginx.pid;
log {
output file /app/data/caddy/caddy.log { events {
roll_size 10MB worker_connections 1024;
roll_keep 10
}
}
} }
:$CADDY_PORT { http {
# Ensure we listen on all interfaces include /etc/nginx/mime.types;
bind 0.0.0.0 default_type application/octet-stream;
log { log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
output file /app/data/caddy/access.log { '\$status \$body_bytes_sent "\$http_referer" '
roll_size 10MB '"\$http_user_agent" "\$http_x_forwarded_for"';
roll_keep 10
} access_log /app/data/logs/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Security headers
map \$sent_http_content_type \$cors_header {
default "";
"~*text/html" "credentialless";
} }
# Headers for WebAssembly and IndexedDB server {
header { listen $NGINX_PORT default_server;
Cross-Origin-Embedder-Policy "credentialless" listen [::]:$NGINX_PORT default_server;
Cross-Origin-Opener-Policy "same-origin"
Cross-Origin-Resource-Policy "cross-origin" root /app/web/photos;
index index.html;
# Security headers
add_header Cross-Origin-Embedder-Policy \$cors_header always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Resource-Policy "cross-origin" always;
# Configuration scripts
location /config.js {
alias /app/data/public/config.js;
} }
# Serve configuration scripts location /debug.js {
handle /config.js { alias /app/data/public/debug.js;
root * /app/data/public
file_server
} }
handle /debug.js { # Debug page
root * /app/data/public location /debug {
file_server alias /app/data/public/debug.html;
}
# Serve debug page
handle /debug {
root * /app/data/public
rewrite * /debug.html
file_server
} }
# Health check endpoints # Health check endpoints
handle /health { location /health {
respond "OK" 200 return 200 "OK";
} }
handle /healthcheck { location /healthcheck {
respond "OK" 200 return 200 "OK";
} }
# API health check endpoint - direct proxy to API # API health check and API endpoints
handle /api/health { location /api/ {
reverse_proxy localhost:$API_PORT proxy_pass http://localhost:$API_PORT/;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
} }
# Serve photos app at the root # Root serves the photos app
handle /* { location / {
root * /app/web/photos try_files \$uri \$uri/ /index.html;
# Inject config script # Insert config script for HTML files
@html { sub_filter '</head>' '<script src="/config.js"></script><script src="/debug.js"></script></head>';
path *.html index.html sub_filter_once on;
not path */assets/* */static/* */img/* */css/* */js/* sub_filter_types text/html;
}
header @html Content-Type "text/html; charset=utf-8"
# Use rewrite to modify HTML content
@isIndex path /index.html
rewrite @isIndex /photos-injected.html
file_server
} }
# Accounts app # Accounts app
handle /accounts* { location /accounts/ {
uri replace /accounts / alias /app/web/accounts/;
root * /app/web/accounts try_files \$uri \$uri/ /accounts/index.html;
handle_path /* { # Insert config script for HTML files
@html { sub_filter '</head>' '<script src="/config.js"></script><script src="/debug.js"></script></head>';
path *.html index.html sub_filter_once on;
not path */assets/* */static/* */img/* */css/* */js/* sub_filter_types text/html;
}
header @html Content-Type "text/html; charset=utf-8"
# Use rewrite to modify HTML content
@isIndex path /index.html
rewrite @isIndex /accounts-injected.html
file_server
}
} }
# Auth app # Auth app
handle /auth* { location /auth/ {
uri replace /auth / alias /app/web/auth/;
root * /app/web/auth try_files \$uri \$uri/ /auth/index.html;
handle_path /* { # Insert config script for HTML files
@html { sub_filter '</head>' '<script src="/config.js"></script><script src="/debug.js"></script></head>';
path *.html index.html sub_filter_once on;
not path */assets/* */static/* */img/* */css/* */js/* sub_filter_types text/html;
}
header @html Content-Type "text/html; charset=utf-8"
# Use rewrite to modify HTML content
@isIndex path /index.html
rewrite @isIndex /auth-injected.html
file_server
}
} }
# Cast app # Cast app
handle /cast* { location /cast/ {
uri replace /cast / alias /app/web/cast/;
root * /app/web/cast try_files \$uri \$uri/ /cast/index.html;
handle_path /* { # Insert config script for HTML files
@html { sub_filter '</head>' '<script src="/config.js"></script><script src="/debug.js"></script></head>';
path *.html index.html sub_filter_once on;
not path */assets/* */static/* */img/* */css/* */js/* sub_filter_types text/html;
} }
header @html Content-Type "text/html; charset=utf-8"
# Use rewrite to modify HTML content
@isIndex path /index.html
rewrite @isIndex /cast-injected.html
file_server
}
}
# Proxy API calls to the backend server
handle /api* {
reverse_proxy localhost:$API_PORT
} }
} }
EOT 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 # Start NGINX
for app_name in "photos" "accounts" "auth" "cast"; do echo "==> Starting NGINX on port $NGINX_PORT"
# Create an injected HTML file that loads the config nginx -c /app/data/nginx/nginx.conf -p /app/data/nginx &
cat > "/app/data/public/${app_name}-injected.html" <<EOT NGINX_PID=$!
<!DOCTYPE html> echo "==> NGINX started with PID $NGINX_PID"
<html>
<head>
<meta charset="utf-8">
<title>Ente ${app_name^}</title>
<script src="/config.js"></script>
<script>
// Redirect to the actual app after loading config
window.onload = function() {
const iframe = document.createElement('iframe');
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';
iframe.style.position = 'absolute';
iframe.style.top = '0';
iframe.style.left = '0';
iframe.src = "/${app_name}/index.html";
document.body.appendChild(iframe);
};
</script>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
overflow: hidden;
}
</style>
</head>
<body>
<div id="loading" style="display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column;">
<h1>Loading Ente ${app_name^}...</h1>
<p>Please wait while the application initializes.</p>
</div>
</body>
</html>
EOT
done
echo "==> Created injected HTML files for all apps" # Wait for NGINX to start
# 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
sleep 2 sleep 2
# Test Caddy connectivity # Test NGINX connectivity
echo "==> Testing Caddy connectivity" echo "==> Testing NGINX connectivity"
for i in {1..5}; do for i in {1..5}; do
if curl -s --max-time 2 --head --fail http://localhost:$CADDY_PORT/health > /dev/null; then if curl -s --max-time 2 --head --fail http://localhost:$NGINX_PORT/health > /dev/null; then
echo "==> Caddy is running properly on port $CADDY_PORT" echo "==> NGINX is running properly on port $NGINX_PORT"
break break
else else
if [ $i -eq 5 ]; then if [ $i -eq 5 ]; then
echo "==> Failed to connect to Caddy after multiple attempts" echo "==> Failed to connect to NGINX after multiple attempts"
echo "==> Last 20 lines of Caddy log:" echo "==> Last 20 lines of NGINX error log:"
tail -20 /app/data/caddy/caddy.log || echo "==> No Caddy log available" tail -20 /app/data/logs/nginx/error.log || echo "==> No NGINX error log available"
echo "==> Network ports in use:" echo "==> Network ports in use:"
netstat -tuln || echo "==> netstat command not available" netstat -tuln || echo "==> netstat command not available"
else else
echo "==> Attempt $i: Waiting for Caddy to start... (1 second)" echo "==> Attempt $i: Waiting for NGINX to start... (1 second)"
sleep 1 sleep 1
fi fi
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" echo "==> Entering wait state - press Ctrl+C to stop"
# Wait for all background processes to complete (or for user to interrupt) # Wait for all background processes to complete (or for user to interrupt)
wait $SERVER_PID wait $SERVER_PID
wait $CADDY_PID wait $NGINX_PID