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

185
start.sh
View File

@ -639,6 +639,8 @@ import (
"strconv"
"strings"
"time"
"regexp"
"encoding/base64"
)
func main() {
@ -1015,29 +1017,41 @@ GOMOCK
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
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
if go build -o mock_server main.go; then
echo "==> Successfully compiled mock API server"
# Start the server and log both to file and to console
nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Set environment variables for database connectivity
export ENTE_PG_HOST="${MUSEUM_DB_HOST}"
export ENTE_PG_PORT="${MUSEUM_DB_PORT}"
export ENTE_PG_USER="${MUSEUM_DB_USER}"
export ENTE_PG_PASSWORD="${MUSEUM_DB_PASSWORD}"
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..."
sleep 2
# Make sure we pass the standard PostgreSQL environment variables too
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}"
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}"
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable"
# Check if the server is actually running
if ps -p $SERVER_PID > /dev/null; then
echo "==> Mock API server is running with PID $SERVER_PID"
go run main.go > /app/data/logs/museum.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8080" > /dev/null; then
echo "==> Mock API server is listening on port 8080"
else
echo "==> WARNING: Mock API server doesn't appear to be listening on port 8080"
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
echo "==> ERROR: Museum server not found"
echo "==> Starting a mock server"
@ -1058,6 +1072,8 @@ import (
"strconv"
"strings"
"time"
"regexp"
"encoding/base64"
)
func main() {
@ -1434,29 +1450,41 @@ GOMOCK
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
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
if go build -o mock_server main.go; then
echo "==> Successfully compiled mock API server"
# Start the server and log both to file and to console
nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Set environment variables for database connectivity
export ENTE_PG_HOST="${MUSEUM_DB_HOST}"
export ENTE_PG_PORT="${MUSEUM_DB_PORT}"
export ENTE_PG_USER="${MUSEUM_DB_USER}"
export ENTE_PG_PASSWORD="${MUSEUM_DB_PASSWORD}"
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..."
sleep 2
# Make sure we pass the standard PostgreSQL environment variables too
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}"
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}"
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable"
# Check if the server is actually running
if ps -p $SERVER_PID > /dev/null; then
echo "==> Mock API server is running with PID $SERVER_PID"
go run main.go > /app/data/logs/museum.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Check if the port is actually listening
if netstat -tulpn 2>/dev/null | grep ":8080" > /dev/null; then
echo "==> Mock API server is listening on port 8080"
else
echo "==> WARNING: Mock API server doesn't appear to be listening on port 8080"
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
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
echo "==> Creating mock Public Albums API 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
import (
@ -1592,18 +1620,48 @@ EOT
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
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
go run main.go > /app/data/logs/public_museum.log 2>&1 &
PUBLIC_SERVER_PID=$!
echo "==> Mock Public Albums API server started with PID $PUBLIC_SERVER_PID"
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
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
cat > /tmp/mock-public-server/main.go <<"EOT"
package main
import (
@ -1700,12 +1758,41 @@ EOT
# Unset any module-related flags before running standalone Go program
unset GO111MODULE
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
echo "==> Public Albums server started with PID $PUBLIC_SERVER_PID"