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

View File

@ -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": {

View File

@ -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

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"