Fix mock server startup to ensure it starts properly and binds to the correct ports

This commit is contained in:
Andreas Düren 2025-03-18 21:23:24 +01:00
parent d32c366683
commit b223843bcd

201
start.sh
View File

@ -639,6 +639,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"regexp"
"encoding/base64"
) )
func main() { func main() {
@ -1015,29 +1017,41 @@ GOMOCK
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
# Run without any module flags
# Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080"
cd /tmp/mock-server cd /tmp/mock-server
if go build -o mock_server main.go; then
# Set environment variables for database connectivity echo "==> Successfully compiled mock API server"
export ENTE_PG_HOST="${MUSEUM_DB_HOST}" # Start the server and log both to file and to console
export ENTE_PG_PORT="${MUSEUM_DB_PORT}" nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
export ENTE_PG_USER="${MUSEUM_DB_USER}" SERVER_PID=$!
export ENTE_PG_PASSWORD="${MUSEUM_DB_PASSWORD}" echo "==> Mock API server started with PID $SERVER_PID"
export ENTE_PG_DATABASE="${MUSEUM_DB_NAME}"
export ENTE_PG_DSN="postgres://${MUSEUM_DB_USER}:${MUSEUM_DB_PASSWORD}@${MUSEUM_DB_HOST}:${MUSEUM_DB_PORT}/${MUSEUM_DB_NAME}?sslmode=disable" # Wait to ensure the server is up
echo "==> Waiting for server to start..."
# Make sure we pass the standard PostgreSQL environment variables too sleep 2
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}" # Check if the server is actually running
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}" if ps -p $SERVER_PID > /dev/null; then
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}" echo "==> Mock API server is running with PID $SERVER_PID"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable" # Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8080" > /dev/null; then
go run main.go > /app/data/logs/museum.log 2>&1 & echo "==> Mock API server is listening on port 8080"
SERVER_PID=$! else
echo "==> WARNING: Mock API server doesn't appear to be listening on port 8080"
echo "==> Mock API server started with PID $SERVER_PID" echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_server.log
fi
else
echo "==> ERROR: Mock API server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_server.log
fi
else
echo "==> ERROR: Failed to build mock API server"
fi
else else
echo "==> ERROR: Museum server not found" echo "==> ERROR: Museum server not found"
echo "==> Starting a mock server" echo "==> Starting a mock server"
@ -1058,6 +1072,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"regexp"
"encoding/base64"
) )
func main() { func main() {
@ -1434,29 +1450,41 @@ GOMOCK
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
# Run without any module flags
# Build and run the mock server in the background
echo "==> Building and starting mock API server on port 8080"
cd /tmp/mock-server cd /tmp/mock-server
if go build -o mock_server main.go; then
# Set environment variables for database connectivity echo "==> Successfully compiled mock API server"
export ENTE_PG_HOST="${MUSEUM_DB_HOST}" # Start the server and log both to file and to console
export ENTE_PG_PORT="${MUSEUM_DB_PORT}" nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
export ENTE_PG_USER="${MUSEUM_DB_USER}" SERVER_PID=$!
export ENTE_PG_PASSWORD="${MUSEUM_DB_PASSWORD}" echo "==> Mock API server started with PID $SERVER_PID"
export ENTE_PG_DATABASE="${MUSEUM_DB_NAME}"
export ENTE_PG_DSN="postgres://${MUSEUM_DB_USER}:${MUSEUM_DB_PASSWORD}@${MUSEUM_DB_HOST}:${MUSEUM_DB_PORT}/${MUSEUM_DB_NAME}?sslmode=disable" # Wait to ensure the server is up
echo "==> Waiting for server to start..."
# Make sure we pass the standard PostgreSQL environment variables too sleep 2
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}" # Check if the server is actually running
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}" if ps -p $SERVER_PID > /dev/null; then
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}" echo "==> Mock API server is running with PID $SERVER_PID"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable" # Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8080" > /dev/null; then
go run main.go > /app/data/logs/museum.log 2>&1 & echo "==> Mock API server is listening on port 8080"
SERVER_PID=$! else
echo "==> WARNING: Mock API server doesn't appear to be listening on port 8080"
echo "==> Mock API server started with PID $SERVER_PID" echo "==> Checking server logs:"
tail -n 10 /app/data/logs/mock_server.log
fi
else
echo "==> ERROR: Mock API server failed to start"
echo "==> Server log:"
cat /app/data/logs/mock_server.log
fi
else
echo "==> ERROR: Failed to build mock API server"
fi
fi fi
echo "==> Server started with PID $SERVER_PID" echo "==> Server started with PID $SERVER_PID"
@ -1495,7 +1523,7 @@ 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 cat > /tmp/mock-public-server/main.go <<"EOT"
package main package main
import ( import (
@ -1588,22 +1616,52 @@ func main() {
} }
} }
EOT EOT
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
# Run without any module flags
# Build and run the public albums mock server
echo "==> Building and starting Public Albums mock server on port 8081"
cd /tmp/mock-public-server cd /tmp/mock-public-server
go run main.go > /app/data/logs/public_museum.log 2>&1 & if go build -o mock_public_server main.go; then
PUBLIC_SERVER_PID=$! echo "==> Successfully compiled Public Albums mock server"
echo "==> Mock Public Albums API server started with PID $PUBLIC_SERVER_PID" # 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"
# 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"
# 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 10 /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
fi
else
echo "==> ERROR: Failed to build Public Albums mock server"
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 cat > /tmp/mock-public-server/main.go <<"EOT"
package main package main
import ( import (
@ -1696,16 +1754,45 @@ func main() {
} }
} }
EOT EOT
# Unset any module-related flags before running standalone Go program # Unset any module-related flags before running standalone Go program
unset GO111MODULE unset GO111MODULE
unset GOFLAGS unset GOFLAGS
# Run without any module flags
cd /tmp/mock-public-server
go run main.go > /app/data/logs/public_museum.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Mock Public Albums server started with PID $PUBLIC_SERVER_PID" # 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"
# 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"
# 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 10 /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
fi
else
echo "==> ERROR: Failed to build Public Albums mock server"
fi
fi fi
echo "==> Public Albums server started with PID $PUBLIC_SERVER_PID" echo "==> Public Albums server started with PID $PUBLIC_SERVER_PID"