From 42c13746064ef95810e268b66045e5c21ef9b8e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20D=C3=BCren?= Date: Mon, 17 Mar 2025 00:13:38 +0100 Subject: [PATCH] Add Caddy webserver implementation --- CloudronManifest.json | 2 +- Dockerfile | 14 ++++- start.sh | 134 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 144 insertions(+), 6 deletions(-) diff --git a/CloudronManifest.json b/CloudronManifest.json index f8be479..30c9b22 100644 --- a/CloudronManifest.json +++ b/CloudronManifest.json @@ -8,7 +8,7 @@ "tagline": "Open Source End-to-End Encrypted Photos & Authentication", "upstreamVersion": "1.0.0", "version": "1.0.0", - "healthCheckPath": "/healthcheck", + "healthCheckPath": "/health", "httpPort": 3080, "memoryLimit": 1073741824, "addons": { diff --git a/Dockerfile b/Dockerfile index 2e2499d..3a5ded1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -78,10 +78,16 @@ RUN mkdir -p /build/web/photos /build/web/accounts /build/web/auth /build/web/ca FROM cloudron/base:5.0.0@sha256:04fd70dbd8ad6149c19de39e35718e024417c3e01dc9c6637eaf4a41ec4e596c -# Install necessary packages +# Install necessary packages and Caddy webserver RUN apt-get update && \ apt-get install -y curl git nodejs npm libsodium23 libsodium-dev pkg-config postgresql-client && \ npm install -g yarn serve && \ + # Install Caddy for web server + 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 && \ apt-get clean && apt-get autoremove && \ rm -rf /var/cache/apt /var/lib/apt/lists @@ -94,7 +100,7 @@ RUN curl -L https://go.dev/dl/go1.24.1.linux-amd64.tar.gz -o go.tar.gz && \ ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt # Set up directory structure -RUN mkdir -p /app/code /app/data/config /app/web +RUN mkdir -p /app/code /app/data/config /app/data/caddy /app/web WORKDIR /app/code @@ -137,7 +143,9 @@ ADD config.template.yaml /app/pkg/ # Set proper permissions RUN chmod +x /app/pkg/start.sh -# Expose the API port +# Expose the web port (Cloudron expects port 3080) +EXPOSE 3080 +# Also expose API port EXPOSE 8080 # Start the application diff --git a/start.sh b/start.sh index 6bc5070..9e82680 100644 --- a/start.sh +++ b/start.sh @@ -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 < 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"