Newer
Older
cortex-hub / ui / run_web.sh
#!/bin/bash

AI_HUB_HOST="0.0.0.0"
AI_HUB_PORT="8001"
APP_MODULE="app.main:app"

AI_HUB_DIR="/app/project/cortex-hub/ai-hub"
TTS_CLIENT_DIR="/app/project/cortex-hub/ui/tts-client-app"

echo "--- Cleaning up existing processes ---"

# Kill existing uvicorn processes on the expected port
EXISTING_UVICORN_PID=$(lsof -ti tcp:${AI_HUB_PORT})
if [ -n "$EXISTING_UVICORN_PID" ]; then
  echo "Killing existing process on port ${AI_HUB_PORT} (PID: $EXISTING_UVICORN_PID)"
  kill -9 "$EXISTING_UVICORN_PID"
fi

# Kill existing React frontend on port 8000
EXISTING_REACT_PID=$(lsof -ti tcp:8000)
if [ -n "$EXISTING_REACT_PID" ]; then
  echo "Killing existing frontend process on port 8000 (PID: $EXISTING_REACT_PID)"
  kill -9 "$EXISTING_REACT_PID"
fi

pushd "$AI_HUB_DIR" > /dev/null

pip install -e .

echo "--- Generating self-signed SSL certificates ---"

# Create a temporary directory for certs
SSL_TEMP_DIR=$(mktemp -d)
SSL_KEYFILE="${SSL_TEMP_DIR}/key.pem"
SSL_CERTFILE="${SSL_TEMP_DIR}/cert.pem"

# Generate self-signed certificate
openssl req -x509 -nodes -days 1 -newkey rsa:2048 \
  -keyout "$SSL_KEYFILE" \
  -out "$SSL_CERTFILE" \
  -subj "/CN=localhost"

# Cleanup function to remove certs on exit
cleanup() {
  echo "--- Cleaning up SSL certificates ---"
  rm -rf "$SSL_TEMP_DIR"
}
trap cleanup EXIT

echo "--- Starting AI Hub Server, React frontend, and backend proxy ---"

# Run AI Hub backend (HTTPS), React frontend (HTTPS), concurrently
concurrently \
  --prefix "[{name}]" \
  --names "aihub,tts-frontend" \
  "uvicorn $APP_MODULE --host $AI_HUB_HOST --port $AI_HUB_PORT --ssl-keyfile $SSL_KEYFILE --ssl-certfile $SSL_CERTFILE" \
  "cd $TTS_CLIENT_DIR && HTTPS=true HOST=0.0.0.0 PORT=8000 npm start"

popd > /dev/null