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

249
start.sh
View File

@ -1068,7 +1068,13 @@ else
# Create a temporary directory for a simple Go server # Create a temporary directory for a simple Go server
mkdir -p /tmp/mock-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 package main
import ( import (
@ -1177,6 +1183,16 @@ func main() {
"ott": verificationCode, "ott": verificationCode,
"exp": time.Now().Add(time.Hour).Unix(), "exp": time.Now().Add(time.Hour).Unix(),
"email": email, "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) json.NewEncoder(w).Encode(jsonResponse)
} else { } else {
@ -1440,16 +1456,15 @@ func main() {
GOMOCK GOMOCK
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building mock server"
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
unset GOMODCACHE
# Build and run the mock server in the background # Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080" echo "==> Building and starting mock API server on port 8080"
# Make sure we're using Go 1.24.1 for the build if go build -o mock_server; then
export PATH="/usr/local/go/bin:${PATH}"
if go build -o mock_server main.go; then
echo "==> Successfully compiled mock API server" echo "==> Successfully compiled mock API server"
# Create log directory if it doesn't exist # Create log directory if it doesn't exist
@ -1486,10 +1501,12 @@ GOMOCK
echo "==> ERROR: Failed to build mock API server" echo "==> ERROR: Failed to build mock API server"
# Print Go version for debugging # Print Go version for debugging
go version go version
# Set a bogus server PID to prevent unbound variable error
SERVER_PID=0
fi fi
fi fi
echo "==> Server started with PID $SERVER_PID" echo "==> Server started with PID ${SERVER_PID:-0}"
# Test if API is responding # Test if API is responding
echo "==> Testing API connectivity" 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 # Create a startup script but don't use module flags
echo "==> Creating mock Public Albums API server" echo "==> Creating mock Public Albums API server"
mkdir -p /tmp/mock-public-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 package main
import ( import (
@ -1620,22 +1642,24 @@ func main() {
EOT EOT
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building public albums mock server"
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
unset GOMODCACHE
# Build and run the public albums mock server # Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081" echo "==> Building and starting Public Albums mock server on port 8081"
cd /tmp/mock-public-server if go build -o mock_public_server; then
if go build -o mock_public_server main.go; then
echo "==> Successfully compiled Public Albums mock server" echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console # 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 & nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$! PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID" echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Wait to ensure the server is up # Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..." echo "==> Waiting for Public Albums server to start..."
sleep 2 sleep 3
# Check if the server is actually running # Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then if ps -p $PUBLIC_SERVER_PID > /dev/null; then
@ -1647,7 +1671,7 @@ EOT
else else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081" echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:" echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_public_server.log tail -n 20 /app/data/logs/mock_public_server.log
fi fi
else else
echo "==> ERROR: Public Albums mock server failed to start" echo "==> ERROR: Public Albums mock server failed to start"
@ -1656,14 +1680,23 @@ EOT
fi fi
else else
echo "==> ERROR: Failed to build Public Albums mock server" 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 fi
else else
echo "==> ERROR: Museum server not found for public albums" echo "==> ERROR: Museum server not found for public albums"
echo "==> Starting a mock public albums server" echo "==> Starting a mock public albums server"
# Create a temporary directory for a simple Go server # Create a temporary directory for a simple Go server
mkdir -p /tmp/mock-public-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 package main
import ( import (
@ -1758,22 +1791,24 @@ func main() {
EOT EOT
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
echo "==> Unsetting module flags before building public albums mock server"
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
unset GOMODCACHE
# Build and run the public albums mock server # Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081" echo "==> Building and starting Public Albums mock server on port 8081"
cd /tmp/mock-public-server if go build -o mock_public_server; then
if go build -o mock_public_server main.go; then
echo "==> Successfully compiled Public Albums mock server" echo "==> Successfully compiled Public Albums mock server"
# Start the server and log both to file and to console # 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 & nohup ./mock_public_server > /app/data/logs/mock_public_server.log 2>&1 &
PUBLIC_SERVER_PID=$! PUBLIC_SERVER_PID=$!
echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID" echo "==> Public Albums mock server started with PID $PUBLIC_SERVER_PID"
# Wait to ensure the server is up # Wait to ensure the server is up
echo "==> Waiting for Public Albums server to start..." echo "==> Waiting for Public Albums server to start..."
sleep 2 sleep 3
# Check if the server is actually running # Check if the server is actually running
if ps -p $PUBLIC_SERVER_PID > /dev/null; then if ps -p $PUBLIC_SERVER_PID > /dev/null; then
@ -1785,7 +1820,7 @@ EOT
else else
echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081" echo "==> WARNING: Public Albums mock server doesn't appear to be listening on port 8081"
echo "==> Checking server logs:" echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_public_server.log tail -n 20 /app/data/logs/mock_public_server.log
fi fi
else else
echo "==> ERROR: Public Albums mock server failed to start" echo "==> ERROR: Public Albums mock server failed to start"
@ -1794,14 +1829,18 @@ EOT
fi fi
else else
echo "==> ERROR: Failed to build Public Albums mock server" 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
fi 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 # Test if Public Albums API is responding
echo "==> Testing Public Albums API connectivity" echo "==> Testing Public Albums API connectivity"
for i in {1..5}; do 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 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" echo "==> Public Albums API is responding on port $PUBLIC_ALBUMS_PORT"
break break
@ -1815,51 +1854,51 @@ for i in {1..5}; do
sleep 2 sleep 2
fi fi
fi fi
done done
# Set up Caddy web server # Set up Caddy web server
echo "==> Setting up Caddy web server" echo "==> Setting up Caddy web server"
# Create a simpler approach for injecting configuration # Create a simpler approach for injecting configuration
echo "==> Creating a static HTML file with config scripts already included" echo "==> Creating a static HTML file with config scripts already included"
# Create runtime-config.js files in writable locations # Create runtime-config.js files in writable locations
echo "==> Creating runtime-config.js in writable location" echo "==> Creating runtime-config.js in writable location"
mkdir -p /app/data/web mkdir -p /app/data/web
cat > /app/data/web/runtime-config.js <<EOT cat > /app/data/web/runtime-config.js <<EOT
// Runtime configuration for Ente // Runtime configuration for Ente
window.ENTE_CONFIG = { window.ENTE_CONFIG = {
API_URL: '${API_ENDPOINT}', API_URL: '${API_ENDPOINT}',
PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public' PUBLIC_ALBUMS_URL: '${CLOUDRON_APP_ORIGIN}/public'
}; };
// Next.js environment variables // Next.js environment variables
window.process = window.process || {}; window.process = window.process || {};
window.process.env = window.process.env || {}; window.process.env = window.process.env || {};
window.process.env.NEXT_PUBLIC_ENTE_ENDPOINT = '${API_ENDPOINT}'; 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_ENTE_PUBLIC_ALBUMS_ENDPOINT = '${CLOUDRON_APP_ORIGIN}/public';
window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}'; window.process.env.NEXT_PUBLIC_REACT_APP_ENTE_ENDPOINT = '${API_ENDPOINT}';
window.process.env.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 // Make sure all URLs are properly formatted for URL constructor
if (!window.ENTE_CONFIG.API_URL.startsWith('http')) { if (!window.ENTE_CONFIG.API_URL.startsWith('http')) {
console.log('Adding https:// prefix to API_URL'); console.log('Adding https:// prefix to API_URL');
window.ENTE_CONFIG.API_URL = window.location.origin + window.ENTE_CONFIG.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.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.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; 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('Ente runtime config loaded from runtime-config.js');
console.log('API_URL:', window.ENTE_CONFIG.API_URL); console.log('API_URL:', window.ENTE_CONFIG.API_URL);
console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL); console.log('PUBLIC_ALBUMS_URL:', window.ENTE_CONFIG.PUBLIC_ALBUMS_URL);
EOT EOT
# Ensure runtime-config.js is readable # Ensure runtime-config.js is readable
chmod 644 /app/data/web/runtime-config.js chmod 644 /app/data/web/runtime-config.js
# Create the static HTML files with scripts pre-injected # Create the static HTML files with scripts pre-injected
for app_dir in photos accounts auth cast; do for app_dir in photos accounts auth cast; do
# Create directory for our modified files # Create directory for our modified files
mkdir -p /app/data/web/$app_dir mkdir -p /app/data/web/$app_dir
@ -1896,20 +1935,20 @@ for app_dir in photos accounts auth cast; do
</html> </html>
HTML HTML
fi fi
done done
# Modify the Caddyfile to serve our modified HTML files # Modify the Caddyfile to serve our modified HTML files
cat > /app/data/caddy/Caddyfile <<EOT cat > /app/data/caddy/Caddyfile <<EOT
# Global settings # Global settings
{ {
admin off admin off
auto_https off auto_https off
http_port $CADDY_PORT http_port $CADDY_PORT
https_port 0 https_port 0
} }
# Main site configuration # Main site configuration
:$CADDY_PORT { :$CADDY_PORT {
# Basic logging # Basic logging
log { log {
level INFO level INFO
@ -2105,24 +2144,24 @@ cat > /app/data/caddy/Caddyfile <<EOT
uri strip_prefix /public uri strip_prefix /public
reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT reverse_proxy 0.0.0.0:$PUBLIC_ALBUMS_PORT
} }
} }
EOT EOT
echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile" echo "==> Created Caddy config with properly modified HTML files at /app/data/caddy/Caddyfile"
# Start Caddy server # Start Caddy server
echo "==> Starting Caddy server" echo "==> Starting Caddy server"
caddy start --config /app/data/caddy/Caddyfile --adapter caddyfile & caddy start --config /app/data/caddy/Caddyfile --adapter caddyfile &
CADDY_PID=$! CADDY_PID=$!
echo "==> Caddy started with PID $CADDY_PID" echo "==> Caddy started with PID $CADDY_PID"
# Wait a moment for Caddy to start # Wait a moment for Caddy to start
sleep 2 sleep 2
# Test Caddy connectivity # Test Caddy connectivity
echo "==> Testing Caddy connectivity" echo "==> Testing Caddy connectivity"
for i in {1..5}; do for i in {1..5}; do
if curl -s --max-time 2 --fail http://0.0.0.0:$CADDY_PORT/health > /dev/null; then 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" echo "==> Caddy is responding on port $CADDY_PORT"
break break
@ -2135,52 +2174,52 @@ for i in {1..5}; do
sleep 1 sleep 1
fi fi
fi fi
done done
echo "==> Application is now running" echo "==> Application is now running"
echo "==> Access your Ente instance at: $CLOUDRON_APP_ORIGIN" echo "==> Access your Ente instance at: $CLOUDRON_APP_ORIGIN"
# Check communication between frontend and backend services # Check communication between frontend and backend services
echo "==> Checking communication between frontend and backend services" echo "==> Checking communication between frontend and backend services"
echo "==> Testing main API communication" echo "==> Testing main API communication"
MAIN_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/api/health) MAIN_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/api/health)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "==> Main API communication via frontend is working" echo "==> Main API communication via frontend is working"
else else
echo "==> WARNING: Main API communication via frontend is NOT working" echo "==> WARNING: Main API communication via frontend is NOT working"
echo "==> Response: $MAIN_API_TEST" echo "==> Response: $MAIN_API_TEST"
fi fi
echo "==> Testing public albums API communication" echo "==> Testing public albums API communication"
PUBLIC_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/public/health) PUBLIC_API_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/public/health)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "==> Public Albums API communication via frontend is working" echo "==> Public Albums API communication via frontend is working"
else else
echo "==> WARNING: Public Albums API communication via frontend is NOT working" echo "==> WARNING: Public Albums API communication via frontend is NOT working"
echo "==> Response: $PUBLIC_API_TEST" echo "==> Response: $PUBLIC_API_TEST"
fi fi
echo "==> Testing frontend config.js" echo "==> Testing frontend config.js"
CONFIG_JS_TEST=$(curl -s --max-time 2 http://0.0.0.0:$CADDY_PORT/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 if [[ "$CONFIG_JS_TEST" == *"API_URL"* && "$CONFIG_JS_TEST" == *"PUBLIC_ALBUMS_URL"* ]]; then
echo "==> Frontend configuration is properly loaded" echo "==> Frontend configuration is properly loaded"
else else
echo "==> WARNING: Frontend configuration is not properly loaded" echo "==> WARNING: Frontend configuration is not properly loaded"
echo "==> Response: $CONFIG_JS_TEST" echo "==> Response: $CONFIG_JS_TEST"
fi fi
echo "==> Entering wait state - watching logs for registration codes" echo "==> Entering wait state - watching logs for registration codes"
echo "==> Registration verification codes will appear in the logs below" echo "==> Registration verification codes will appear in the logs below"
echo "==> Press Ctrl+C to stop" echo "==> Press Ctrl+C to stop"
# Set up a tail process to show logs in real-time while maintaining the wait state # 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 -f /app/data/logs/api_requests.log &
TAIL_PID=$! TAIL_PID=$!
# Trap to kill the tail process when the script exits # 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 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 for all processes
wait $SERVER_PID wait $SERVER_PID
wait $PUBLIC_SERVER_PID wait $PUBLIC_SERVER_PID
wait $CADDY_PID wait $CADDY_PID