Compare commits

..

2 Commits

Author SHA1 Message Date
Andreas Düren
c00be35fc7 Fix mock API server startup issues on port 8080 2025-03-18 21:39:20 +01:00
Andreas Düren
b223843bcd Fix mock server startup to ensure it starts properly and binds to the correct ports 2025-03-18 21:23:24 +01:00

225
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,51 @@ 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
cd /tmp/mock-server
# Set environment variables for database connectivity # Build and run the mock server in the background
export ENTE_PG_HOST="${MUSEUM_DB_HOST}" echo "==> Building and starting mock API server on port 8080"
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"
# Make sure we pass the standard PostgreSQL environment variables too # Make sure we're using Go 1.24.1 for the build
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}" export PATH="/usr/local/go/bin:${PATH}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}"
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}"
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable"
go run main.go > /app/data/logs/museum.log 2>&1 & if go build -o mock_server main.go; then
SERVER_PID=$! echo "==> Successfully compiled mock API server"
echo "==> Mock API server started with PID $SERVER_PID" # Create log directory if it doesn't exist
mkdir -p /app/data/logs
# Start the server and log both to file and to console
chmod +x ./mock_server
nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Wait to ensure the server is up
echo "==> Waiting for server to start..."
sleep 3
# 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"
# 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 20 /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"
# Print Go version for debugging
go version
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 +1082,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"regexp"
"encoding/base64"
) )
func main() { func main() {
@ -1318,31 +1344,13 @@ func main() {
validSixDigitCode := len(code) == 6 && regexp.MustCompile(`^\d{6}$`).MatchString(code) validSixDigitCode := len(code) == 6 && regexp.MustCompile(`^\d{6}$`).MatchString(code)
if (exists && code == expectedCode) || code == "123456" || validSixDigitCode { if (exists && code == expectedCode) || code == "123456" || validSixDigitCode {
isValid = true
logger.Printf("✅ SUCCESS: Code verified successfully for email: %s (expected: %s, provided: %s)", email, expectedCode, code) logger.Printf("✅ SUCCESS: Code verified successfully for email: %s (expected: %s, provided: %s)", email, expectedCode, code)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{
"id": 12345,
"token": "mock-token-for-testing",
"email": "%s",
"key": {
"masterKey": "%s",
"verificationKey": "mockVerificationKey1234",
"kty": "mockKty",
"alg": "mockAlg",
"ext": true
},
"name": "Test User",
"createdAt": "%s",
"updatedAt": "%s"
}`, email, base64.StdEncoding.EncodeToString([]byte("mockMasterKey")), time.Now().Format(time.RFC3339), time.Now().Format(time.RFC3339))
// Clear the verification code after successful verification // Clear the verification code after successful verification
delete(verificationCodes, email) delete(verificationCodes, email)
} else { } else {
logger.Printf("❌ ERROR: Invalid verification code for email: %s (expected: %s, provided: %s)", email, expectedCode, code) logger.Printf("❌ ERROR: Invalid verification code for email: %s (expected: %s, provided: %s)", email, expectedCode, code)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"error": "Invalid verification code"}`)
} }
} else { } else {
logger.Printf("❌ INCOMPLETE VERIFICATION REQUEST - email: '%s', code: '%s'", email, code) logger.Printf("❌ INCOMPLETE VERIFICATION REQUEST - email: '%s', code: '%s'", email, code)
@ -1434,29 +1442,51 @@ 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
cd /tmp/mock-server
# Set environment variables for database connectivity # Build and run the mock server in the background
export ENTE_PG_HOST="${MUSEUM_DB_HOST}" echo "==> Building and starting mock API server on port 8080"
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"
# Make sure we pass the standard PostgreSQL environment variables too # Make sure we're using Go 1.24.1 for the build
export PGHOST="${CLOUDRON_POSTGRESQL_HOST}" export PATH="/usr/local/go/bin:${PATH}"
export PGPORT="${CLOUDRON_POSTGRESQL_PORT}"
export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}"
export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
export PGSSLMODE="disable"
go run main.go > /app/data/logs/museum.log 2>&1 & if go build -o mock_server main.go; then
SERVER_PID=$! echo "==> Successfully compiled mock API server"
echo "==> Mock API server started with PID $SERVER_PID" # Create log directory if it doesn't exist
mkdir -p /app/data/logs
# Start the server and log both to file and to console
chmod +x ./mock_server
nohup ./mock_server > /app/data/logs/mock_server.log 2>&1 &
SERVER_PID=$!
echo "==> Mock API server started with PID $SERVER_PID"
# Wait to ensure the server is up
echo "==> Waiting for server to start..."
sleep 3
# 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"
# 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 20 /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"
# Print Go version for debugging
go version
fi
fi fi
echo "==> Server started with PID $SERVER_PID" echo "==> Server started with PID $SERVER_PID"
@ -1495,7 +1525,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 (
@ -1592,18 +1622,48 @@ 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 (
@ -1700,12 +1760,41 @@ 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"