Fix Go module structure for mock servers to resolve build issues

This commit is contained in:
Andreas Düren 2025-03-18 21:52:41 +01:00
parent c00be35fc7
commit 50a19a7908

831
start.sh
View File

@ -1068,7 +1068,13 @@ else
# Create a temporary directory for a simple Go server
mkdir -p /tmp/mock-server
cat > /tmp/mock-server/main.go <<"GOMOCK"
cd /tmp/mock-server
# Initialize a proper Go module
go mod init mock-server
# Write the program to a file
cat > main.go << 'GOMOCK'
package main
import (
@ -1177,6 +1183,16 @@ func main() {
"ott": verificationCode,
"exp": time.Now().Add(time.Hour).Unix(),
"email": email,
"createdAt": time.Now().Format(time.RFC3339),
"updatedAt": time.Now().Format(time.RFC3339),
"key": map[string]interface{}{
"pubKey": "mockPubKey123456",
"encPubKey": "mockEncPubKey123456",
"kty": "mockKty",
"kid": "mockKid",
"alg": "mockAlg",
"verifyKey": "mockVerifyKey123456",
},
}
json.NewEncoder(w).Encode(jsonResponse)
} else {
@ -1440,16 +1456,15 @@ func main() {
GOMOCK
# Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building mock server"
unset GO111MODULE
unset GOFLAGS
unset GOMODCACHE
# Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080"
# Make sure we're using Go 1.24.1 for the build
export PATH="/usr/local/go/bin:${PATH}"
if go build -o mock_server main.go; then
if go build -o mock_server; then
echo "==> Successfully compiled mock API server"
# Create log directory if it doesn't exist
@ -1486,10 +1501,12 @@ GOMOCK
echo "==> ERROR: Failed to build mock API server"
# Print Go version for debugging
go version
# Set a bogus server PID to prevent unbound variable error
SERVER_PID=0
fi
fi
echo "==> Server started with PID $SERVER_PID"
echo "==> Server started with PID ${SERVER_PID:-0}"
# Test if API is responding
echo "==> Testing API connectivity"
@ -1525,7 +1542,12 @@ elif [ -d "$SERVER_DIR/cmd/museum" ]; then
# Create a startup script but don't use module flags
echo "==> Creating mock Public Albums API server"
mkdir -p /tmp/mock-public-server
cat > /tmp/mock-public-server/main.go <<"EOT"
cd /tmp/mock-public-server
# Initialize a proper Go module
go mod init mock-public-server
cat > main.go <<"EOT"
package main
import (
@ -1619,51 +1641,62 @@ func main() {
}
EOT
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
unset GOFLAGS
# Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081"
cd /tmp/mock-public-server
if go build -o mock_public_server main.go; then
echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console
nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building public albums mock server"
unset GO111MODULE
unset GOFLAGS
unset GOMODCACHE
# Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..."
sleep 2
# Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then
echo "==> Public Albums mock server is running with PID $PUBLIC_SERVER_PID"
# Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081"
if go build -o mock_public_server; then
echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console
chmod +x ./mock_public_server
nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8081" > /dev/null; then
echo "==> Public Albums mock server is listening on port 8081"
# Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..."
sleep 3
# Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then
echo "==> Public Albums mock server is running with PID $PUBLIC_SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8081" > /dev/null; then
echo "==> Public Albums mock server is listening on port 8081"
else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:"
tail -n 20 /app/data/logs/mock_public_server.log
fi
else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_public_server.log
echo "==> ERROR: Public Albums mock server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_public_server.log
fi
else
echo "==> ERROR: Public Albums mock server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_public_server.log
echo "==> ERROR: Failed to build Public Albums mock server"
# Print Go version for debugging
go version
# Set a fallback value for PUBLIC_SERVER_PID
PUBLIC_SERVER_PID=0
fi
else
echo "==> ERROR: Failed to build Public Albums mock server"
fi
else
echo "==> ERROR: Museum server not found for public albums"
echo "==> Starting a mock public albums server"
# Create a temporary directory for a simple Go server
mkdir -p /tmp/mock-public-server
cat > /tmp/mock-public-server/main.go <<"EOT"
echo "==> ERROR: Museum server not found for public albums"
echo "==> Starting a mock public albums server"
# Create a temporary directory for a simple Go server
mkdir -p /tmp/mock-public-server
cd /tmp/mock-public-server
# Initialize a proper Go module
go mod init mock-public-server
cat > main.go <<"EOT"
package main
import (
@ -1757,129 +1790,135 @@ func main() {
}
EOT
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
unset GOFLAGS
# Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081"
cd /tmp/mock-public-server
if go build -o mock_public_server main.go; then
echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console
nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building public albums mock server"
unset GO111MODULE
unset GOFLAGS
unset GOMODCACHE
# Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..."
sleep 2
# Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then
echo "==> Public Albums mock server is running with PID $PUBLIC_SERVER_PID"
# Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081"
if go build -o mock_public_server; then
echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console
chmod +x ./mock_public_server
nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8081" > /dev/null; then
echo "==> Public Albums mock server is listening on port 8081"
# Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..."
sleep 3
# Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then
echo "==> Public Albums mock server is running with PID $PUBLIC_SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8081" > /dev/null; then
echo "==> Public Albums mock server is listening on port 8081"
else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:"
tail -n 20 /app/data/logs/mock_public_server.log
fi
else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_public_server.log
echo "==> ERROR: Public Albums mock server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_public_server.log
fi
else
echo "==> ERROR: Public Albums mock server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_public_server.log
echo "==> ERROR: Failed to build Public Albums mock server"
# Print Go version for debugging
go version
# Set a fallback value for PUBLIC_SERVER_PID
PUBLIC_SERVER_PID=0
fi
else
echo "==> ERROR: Failed to build Public Albums mock server"
fi
fi
echo "==> Public Albums server started with PID $PUBLIC_SERVER_PID"
echo "==> Public Albums server started with PID ${PUBLIC_SERVER_PID:-0}"
# Test if Public Albums API is responding
echo "==> Testing Public Albums API connectivity"
for i in {1..5}; do
if curl -s --max-time 2 --fail http://0.0.0.0:$PUBLIC_ALBUMS_PORT/health > /dev/null; then
echo "==> Public Albums API is responding on port $PUBLIC_ALBUMS_PORT"
break
else
if [ $i -eq 5 ]; then
echo "==> WARNING: Public Albums API is not responding after several attempts"
echo "==> Last 20 lines of public_museum.log:"
tail -20 /app/data/logs/public_museum.log || echo "==> No public_museum.log available"
# Test if Public Albums API is responding
echo "==> Testing Public Albums API connectivity"
for i in {1..5}; do
if curl -s --max-time 2 --fail http://0.0.0.0:$PUBLIC_ALBUMS_PORT/health > /dev/null; then
echo "==> Public Albums API is responding on port $PUBLIC_ALBUMS_PORT"
break
else
echo "==> Attempt $i: Waiting for Public Albums API to start... (2 seconds)"
sleep 2
if [ $i -eq 5 ]; then
echo "==> WARNING: Public Albums API is not responding after several attempts"
echo "==> Last 20 lines of public_museum.log:"
tail -20 /app/data/logs/public_museum.log || echo "==> No public_museum.log available"
else
echo "==> Attempt $i: Waiting for Public Albums API to start... (2 seconds)"
sleep 2
fi
fi
fi
done
done
# Set up Caddy web server
echo "==> Setting up Caddy web server"
# Set up Caddy web server
echo "==> Setting up Caddy web server"
# Create a simpler approach for injecting configuration
echo "==> Creating a static HTML file with config scripts already included"
# Create a simpler approach for injecting configuration
echo "==> Creating a static HTML file with config scripts already included"
# Create runtime-config.js files in writable locations
echo "==> Creating runtime-config.js in writable location"
mkdir -p /app/data/web
cat > /app/data/web/runtime-config.js <<EOT
// Runtime configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
# Create runtime-config.js files in writable locations
echo "==> Creating runtime-config.js in writable location"
mkdir -p /app/data/web
cat > /app/data/web/runtime-config.js <<EOT
// Runtime configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// 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.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${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.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
console.log('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT
console.log('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT
# Ensure runtime-config.js is readable
chmod 644 /app/data/web/runtime-config.js
# Ensure runtime-config.js is readable
chmod 644 /app/data/web/runtime-config.js
# Create the static HTML files with scripts pre-injected
for app_dir in photos accounts auth cast; do
# Create directory for our modified files
mkdir -p /app/data/web/$app_dir
# If the original index.html exists, copy and modify it
if [ -f "/app/web/$app_dir/index.html" ]; then
echo "==> Copying and modifying index.html for $app_dir app"
cp "/app/web/$app_dir/index.html" "/app/data/web/$app_dir/index.html"
# Create the static HTML files with scripts pre-injected
for app_dir in photos accounts auth cast; do
# Create directory for our modified files
mkdir -p /app/data/web/$app_dir
# Fix any potential issues with the head tag
if ! grep -q "<head>" "/app/data/web/$app_dir/index.html"; then
echo "==> Warning: No head tag found in $app_dir/index.html, adding one"
sed -i 's/<html>/<html>\n<head><\/head>/' "/app/data/web/$app_dir/index.html"
fi
# Insert config scripts right after the opening head tag, unescaped
sed -i 's/<head>/<head>\n <script src="\/config.js" type="text\/javascript"><\/script>\n <script src="\/runtime-config.js" type="text\/javascript"><\/script>/' "/app/data/web/$app_dir/index.html"
else
# Create a minimal HTML file with the scripts included but pointing to the original app
echo "==> Creating minimal pre-configured index.html for $app_dir app with redirect"
cat > "/app/data/web/$app_dir/index.html" <<HTML
# If the original index.html exists, copy and modify it
if [ -f "/app/web/$app_dir/index.html" ]; then
echo "==> Copying and modifying index.html for $app_dir app"
cp "/app/web/$app_dir/index.html" "/app/data/web/$app_dir/index.html"
# Fix any potential issues with the head tag
if ! grep -q "<head>" "/app/data/web/$app_dir/index.html"; then
echo "==> Warning: No head tag found in $app_dir/index.html, adding one"
sed -i 's/<html>/<html>\n<head><\/head>/' "/app/data/web/$app_dir/index.html"
fi
# Insert config scripts right after the opening head tag, unescaped
sed -i 's/<head>/<head>\n <script src="\/config.js" type="text\/javascript"><\/script>\n <script src="\/runtime-config.js" type="text\/javascript"><\/script>/' "/app/data/web/$app_dir/index.html"
else
# Create a minimal HTML file with the scripts included but pointing to the original app
echo "==> Creating minimal pre-configured index.html for $app_dir app with redirect"
cat > "/app/data/web/$app_dir/index.html" <<HTML
<!DOCTYPE html>
<html>
<head>
@ -1895,292 +1934,292 @@ for app_dir in photos accounts auth cast; do
</body>
</html>
HTML
fi
done
fi
done
# Modify the Caddyfile to serve our modified HTML files
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
# Modify the Caddyfile to serve our modified HTML files
cat > /app/data/caddy/Caddyfile <<EOT
# Global settings
{
admin off
auto_https off
http_port $CADDY_PORT
https_port 0
}
# Configuration scripts - directly served
handle /config.js {
header Content-Type application/javascript
respond "
// Direct configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// 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.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
# Main site configuration
:$CADDY_PORT {
# Basic logging
log {
level INFO
output file /app/data/logs/caddy.log
}
# Configuration scripts - directly served
handle /config.js {
header Content-Type application/javascript
respond "
// Direct configuration for Ente
window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
};
// 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.NEXT_PUBLIC_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
// Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.API_URL;
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
window.process.env.REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.REACT_APP_ENTE_ENDPOINT;
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = window.location.origin + window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT;
}
console.log('Ente config loaded - API_URL:', window.ENTE_CONFIG.API_URL);
console.log('Ente config loaded - PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
"
}
handle /runtime-config.js {
root * /app/data/web
file_server
}
# Root path serves the photos app
handle / {
# Special handling for index.html
@is_index path /
handle @is_index {
root * /app/data/web/photos
try_files {path} /index.html
file_server
}
console.log('Ente config loaded - API_URL:', window.ENTE_CONFIG.API_URL);
console.log('Ente config loaded - PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
"
}
handle /runtime-config.js {
root * /app/data/web
file_server
}
# Root path serves the photos app
handle / {
# Special handling for index.html
@is_index path /
handle @is_index {
root * /app/data/web/photos
try_files {path} /index.html
file_server
# Serve other static files from the original location
@not_index {
not path /
not path /api/*
not path /public/*
not path /accounts/*
not path /auth/*
not path /cast/*
}
handle @not_index {
root * /app/web/photos
try_files {path} /index.html
file_server
}
}
# Serve other static files from the original location
@not_index {
not path /
not path /api/*
not path /public/*
not path /accounts/*
not path /auth/*
not path /cast/*
}
handle @not_index {
# Next.js static files
handle /_next/* {
root * /app/web/photos
try_files {path} /index.html
file_server
}
}
# Next.js static files
handle /_next/* {
root * /app/web/photos
file_server
}
# Add global headers for common file types
header /*.js Content-Type application/javascript
header /*.css Content-Type text/css
header /*.json Content-Type application/json
header /*.svg Content-Type image/svg+xml
header /*.woff2 Content-Type font/woff2
header /_next/static/chunks/*.js Content-Type application/javascript
header /_next/static/css/*.css Content-Type text/css
# Add global headers for common file types
header /*.js Content-Type application/javascript
header /*.css Content-Type text/css
header /*.json Content-Type application/json
header /*.svg Content-Type image/svg+xml
header /*.woff2 Content-Type font/woff2
header /_next/static/chunks/*.js Content-Type application/javascript
header /_next/static/css/*.css Content-Type text/css
# Accounts app
handle /accounts {
root * /app/data/web/accounts
try_files {path} /index.html
file_server
}
handle /accounts/* {
@is_index path /accounts/ /accounts/index.html
handle @is_index {
root * /app/data/web
try_files /accounts/index.html
# Accounts app
handle /accounts {
root * /app/data/web/accounts
try_files {path} /index.html
file_server
}
@not_index {
not path /accounts/
not path /accounts/index.html
handle /accounts/* {
@is_index path /accounts/ /accounts/index.html
handle @is_index {
root * /app/data/web
try_files /accounts/index.html
file_server
}
@not_index {
not path /accounts/
not path /accounts/index.html
}
handle @not_index {
uri strip_prefix /accounts
root * /app/web/accounts
try_files {path} /index.html
file_server
}
}
handle @not_index {
uri strip_prefix /accounts
root * /app/web/accounts
try_files {path} /index.html
file_server
}
}
# Auth app
handle /auth {
root * /app/data/web/auth
try_files {path} /index.html
file_server
}
handle /auth/* {
@is_index path /auth/ /auth/index.html
handle @is_index {
root * /app/data/web
try_files /auth/index.html
# Auth app
handle /auth {
root * /app/data/web/auth
try_files {path} /index.html
file_server
}
@not_index {
not path /auth/
not path /auth/index.html
handle /auth/* {
@is_index path /auth/ /auth/index.html
handle @is_index {
root * /app/data/web
try_files /auth/index.html
file_server
}
@not_index {
not path /auth/
not path /auth/index.html
}
handle @not_index {
uri strip_prefix /auth
root * /app/web/auth
try_files {path} /index.html
file_server
}
}
handle @not_index {
uri strip_prefix /auth
root * /app/web/auth
try_files {path} /index.html
file_server
}
}
# Cast app
handle /cast {
root * /app/data/web/cast
try_files {path} /index.html
file_server
}
handle /cast/* {
@is_index path /cast/ /cast/index.html
handle @is_index {
root * /app/data/web
try_files /cast/index.html
# Cast app
handle /cast {
root * /app/data/web/cast
try_files {path} /index.html
file_server
}
@not_index {
not path /cast/
not path /cast/index.html
handle /cast/* {
@is_index path /cast/ /cast/index.html
handle @is_index {
root * /app/data/web
try_files /cast/index.html
file_server
}
@not_index {
not path /cast/
not path /cast/index.html
}
handle @not_index {
uri strip_prefix /cast
root * /app/web/cast
try_files {path} /index.html
file_server
}
}
handle @not_index {
uri strip_prefix /cast
root * /app/web/cast
try_files {path} /index.html
file_server
# Main API proxy
handle /api/* {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
# Public albums API proxy
handle /public/* {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
# Health check endpoints
handle /health {
respond "OK"
}
handle /healthcheck {
respond "OK"
}
handle /api/health {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
handle /public/health {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
}
EOT
# Main API proxy
handle /api/* {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile"
# Public albums API proxy
handle /public/* {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
# Start Caddy server
echo "==> Starting Caddy server"
# Health check endpoints
handle /health {
respond "OK"
}
caddy start --config /app/data/caddy/Caddyfile --adapter caddyfile &
CADDY_PID=$!
echo "==> Caddy started with PID $CADDY_PID"
handle /healthcheck {
respond "OK"
}
# Wait a moment for Caddy to start
sleep 2
handle /api/health {
uri strip_prefix /api
reverse_proxy 0.0.0.0:$API_PORT
}
handle /public/health {
uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
}
}
EOT
echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile"
# 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://0.0.0.0:$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"
# Test Caddy connectivity
echo "==> Testing Caddy connectivity"
for i in {1..5}; do
if curl -s --max-time 2 --fail http://0.0.0.0:$CADDY_PORT/health > /dev/null; then
echo "==> Caddy is responding on port $CADDY_PORT"
break
else
echo "==> Attempt $i: Waiting for Caddy to start... (1 second)"
sleep 1
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"
# Check communication between frontend and backend services
echo "==> Checking communication between frontend and backend services"
echo "==> Testing main API communication"
MAIN_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/api/health)
if [ $? -eq 0 ]; then
echo "==> Main API communication via frontend is working"
else
echo "==> WARNING: Main API communication via frontend is NOT working"
echo "==> Response: $MAIN_API_TEST"
fi
done
echo "==> Application is now running"
echo "==> Access your Ente instance at: $CLOUDRON_APP_ORIGIN"
echo "==> Testing public albums API communication"
PUBLIC_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/public/health)
if [ $? -eq 0 ]; then
echo "==> Public Albums API communication via frontend is working"
else
echo "==> WARNING: Public Albums API communication via frontend is NOT working"
echo "==> Response: $PUBLIC_API_TEST"
fi
# Check communication between frontend and backend services
echo "==> Checking communication between frontend and backend services"
echo "==> Testing main API communication"
MAIN_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/api/health)
if [ $? -eq 0 ]; then
echo "==> Main API communication via frontend is working"
else
echo "==> WARNING: Main API communication via frontend is NOT working"
echo "==> Response: $MAIN_API_TEST"
fi
echo "==> Testing frontend config.js"
CONFIG_JS_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/config.js)
if [[ "$CONFIG_JS_TEST" == *"API_URL"* && "$CONFIG_JS_TEST" == *"PUBLIC_ALBUMS_URL"* ]]; then
echo "==> Frontend configuration is properly loaded"
else
echo "==> WARNING: Frontend configuration is not properly loaded"
echo "==> Response: $CONFIG_JS_TEST"
fi
echo "==> Testing public albums API communication"
PUBLIC_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/public/health)
if [ $? -eq 0 ]; then
echo "==> Public Albums API communication via frontend is working"
else
echo "==> WARNING: Public Albums API communication via frontend is NOT working"
echo "==> Response: $PUBLIC_API_TEST"
fi
echo "==> Entering wait state - watching logs for registration codes"
echo "==> Registration verification codes will appear in the logs below"
echo "==> Press Ctrl+C to stop"
echo "==> Testing frontend config.js"
CONFIG_JS_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/config.js)
if [[ "$CONFIG_JS_TEST" == *"API_URL"* && "$CONFIG_JS_TEST" == *"PUBLIC_ALBUMS_URL"* ]]; then
echo "==> Frontend configuration is properly loaded"
else
echo "==> WARNING: Frontend configuration is not properly loaded"
echo "==> Response: $CONFIG_JS_TEST"
fi
# Set up a tail process to show logs in real-time while maintaining the wait state
tail -f /app/data/logs/api_requests.log &
TAIL_PID=$!
echo "==> Entering wait state - watching logs for registration codes"
echo "==> Registration verification codes will appear in the logs below"
echo "==> Press Ctrl+C to stop"
# Trap to kill the tail process when the script exits
trap 'kill -TERM $TAIL_PID; kill -TERM $SERVER_PID; kill -TERM $PUBLIC_SERVER_PID; kill -TERM $CADDY_PID; exit' TERM INT
# Set up a tail process to show logs in real-time while maintaining the wait state
tail -f /app/data/logs/api_requests.log &
TAIL_PID=$!
# Trap to kill the tail process when the script exits
trap 'kill -TERM $TAIL_PID; kill -TERM $SERVER_PID; kill -TERM $PUBLIC_SERVER_PID; kill -TERM $CADDY_PID; exit' TERM INT
# Wait for all processes
wait $SERVER_PID
wait $PUBLIC_SERVER_PID
wait $CADDY_PID
# Wait for all processes
wait $SERVER_PID
wait $PUBLIC_SERVER_PID
wait $CADDY_PID