From 50a19a79080069561d04e4dfb4b187f2c9066e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20D=C3=BCren?= Date: Tue, 18 Mar 2025 21:52:41 +0100 Subject: [PATCH] Fix Go module structure for mock servers to resolve build issues --- start.sh | 831 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 435 insertions(+), 396 deletions(-) diff --git a/start.sh b/start.sh index efc5a82..fab905c 100644 --- a/start.sh +++ b/start.sh @@ -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 < Creating runtime-config.js in writable location" + mkdir -p /app/data/web + cat > /app/data/web/runtime-config.js < 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 "" "/app/data/web/$app_dir/index.html"; then - echo "==> Warning: No head tag found in $app_dir/index.html, adding one" - sed -i 's//\n<\/head>/' "/app/data/web/$app_dir/index.html" - fi - - # Insert config scripts right after the opening head tag, unescaped - sed -i 's//\n