Add Caddy webserver implementation

This commit is contained in:
Andreas Düren
2025-03-17 00:13:38 +01:00
parent 6546f26d52
commit 42c1374606
3 changed files with 144 additions and 6 deletions

134
start.sh
View File

@@ -1,14 +1,14 @@
#!/bin/bash
# Better signal handling - forward signals to child processes
trap 'kill -TERM $SERVER_PID; exit' TERM INT
trap 'kill -TERM $SERVER_PID; kill -TERM $CADDY_PID; exit' TERM INT
set -eu
echo "==> Starting Ente Cloudron app..."
# Create necessary directories
mkdir -p /app/data/config /app/data/storage /app/data/go /app/data/logs
mkdir -p /app/data/config /app/data/storage /app/data/caddy /app/data/go /app/data/logs
# Add comment about Cloudron filesystem limitations
echo "==> NOTE: Running in Cloudron environment with limited write access"
@@ -226,12 +226,20 @@ echo "==> Set environment variables for web apps"
mkdir -p /app/data/public
mkdir -p /app/data/scripts
mkdir -p /app/data/logs
mkdir -p /app/data/caddy
# Define ports
CADDY_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"
else
echo "==> Port $CADDY_PORT is available for Caddy"
fi
if lsof -i:$API_PORT > /dev/null 2>&1; then
echo "==> WARNING: Port $API_PORT is already in use"
else
@@ -415,6 +423,101 @@ else
echo "==> Skipping migration state check: cmd/museum not found"
fi
# Set up Caddy web server
echo "==> Setting up Caddy web server"
# Create a Caddyfile for serving web apps and reverse proxy to API
cat > /app/data/caddy/Caddyfile <<EOT
# Global settings
{
admin off
auto_https off
http_port $CADDY_PORT
https_port 0
}
# Main site configuration
:$CADDY_PORT {
# Basic logging
log {
level INFO
output file /app/data/logs/caddy.log
}
# Root path serves the photos app
handle / {
root * /app/web/photos
try_files {path} /index.html
file_server
}
# Accounts app
handle /accounts/* {
root * /app/web/accounts
uri strip_prefix /accounts
try_files {path} /index.html
file_server
}
# Auth app
handle /auth/* {
root * /app/web/auth
uri strip_prefix /auth
try_files {path} /index.html
file_server
}
# Cast app
handle /cast/* {
root * /app/web/cast
uri strip_prefix /cast
try_files {path} /index.html
file_server
}
# API proxy
handle /api/* {
uri strip_prefix /api
reverse_proxy localhost:$API_PORT
}
# Health check endpoints
handle /health {
respond "OK"
}
handle /healthcheck {
respond "OK"
}
handle /api/health {
uri strip_prefix /api
reverse_proxy localhost:$API_PORT
}
# Configuration scripts
handle /config.js {
respond "
// 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}';
console.log('Ente config loaded - API_URL:', window.ENTE_CONFIG.API_URL);
" 200 {
Content-Type "application/javascript"
}
}
}
EOT
echo "==> Created Caddy config at /app/data/caddy/Caddyfile"
# Start the Museum server with proper environment variables
echo "==> Starting Museum server"
cd "$SERVER_DIR"
@@ -584,12 +687,39 @@ for i in {1..5}; do
fi
done
# Start Caddy server
echo "==> Starting Caddy server"
caddy start --config /app/data/caddy/Caddyfile --adapter caddyfile &
CADDY_PID=$!
echo "==> Caddy started with PID $CADDY_PID"
# Wait a moment for Caddy to start
sleep 2
# Test Caddy connectivity
echo "==> Testing Caddy connectivity"
for i in {1..5}; do
if curl -s --max-time 2 --fail http://localhost:$CADDY_PORT/health > /dev/null; then
echo "==> Caddy is responding on port $CADDY_PORT"
break
else
if [ $i -eq 5 ]; then
echo "==> WARNING: Caddy is not responding after several attempts"
echo "==> Check Caddy logs at /app/data/logs/caddy.log"
else
echo "==> Attempt $i: Waiting for Caddy to start... (1 second)"
sleep 1
fi
fi
done
echo "==> Application is now running"
echo "==> Access your Ente instance at: $CLOUDRON_APP_ORIGIN"
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
# Create a new go file to inject into the build that overrides the database connection
mkdir -p "$SERVER_DIR/overrides"