Files
chatrtc/server/start-server.sh
2025-06-15 16:32:37 +02:00

223 líneas
5.6 KiB
Bash
Archivo Ejecutable

Este archivo contiene caracteres Unicode invisibles
Este archivo contiene caracteres Unicode invisibles que son indistinguibles para los humanos, pero que pueden ser procesados de forma diferente por un ordenador. Si crees que esto es intencional, puedes ignorar esta advertencia. Usa el botón de Escape para revelarlos.
Este archivo contiene caracteres Unicode que pueden confundirse con otros caracteres. Si crees que esto es intencional, puedes ignorar esta advertencia. Usa el botón de Escape para revelarlos.
#!/bin/bash
# ChatRTC Signaling Server Startup Script
set -e
SERVER_DIR="/home/ale/projects/android/chatrtc/server"
LOG_FILE="$SERVER_DIR/logs/startup.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Create logs directory if it doesn't exist
mkdir -p "$SERVER_DIR/logs"
echo -e "${BLUE}🚀 ChatRTC Signaling Server${NC}"
echo -e "${BLUE}================================${NC}"
# Function to print colored messages
print_status() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_info() {
echo -e "${BLUE} $1${NC}"
}
# Check if Node.js is installed
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed. Please install Node.js 16 or later."
exit 1
fi
# Check Node.js version
NODE_VERSION=$(node --version | cut -d'.' -f1 | cut -d'v' -f2)
if [ "$NODE_VERSION" -lt 16 ]; then
print_warning "Node.js version is $NODE_VERSION. Recommended version is 16 or later."
fi
# Check if npm is installed
if ! command -v npm &> /dev/null; then
print_error "npm is not installed. Please install npm."
exit 1
fi
# Change to server directory
cd "$SERVER_DIR"
print_info "Working directory: $SERVER_DIR"
# Check if package.json exists
if [ ! -f "package.json" ]; then
print_error "package.json not found. Make sure you're in the correct directory."
exit 1
fi
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_info "Installing dependencies..."
npm install
print_status "Dependencies installed"
else
print_status "Dependencies already installed"
fi
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
print_info "Creating .env file from template..."
cp .env.example .env
print_status ".env file created"
fi
# Get the local IP address
if command -v hostname &> /dev/null; then
LOCAL_IP=$(hostname -I | awk '{print $1}')
else
LOCAL_IP="localhost"
fi
# Function to start server
start_server() {
local MODE=$1
print_info "Starting server in $MODE mode..."
case $MODE in
"dev")
npm run dev
;;
"prod")
npm start
;;
"pm2")
if ! command -v pm2 &> /dev/null; then
print_warning "PM2 not installed. Installing PM2..."
npm install -g pm2
fi
npm run pm2:start
print_status "Server started with PM2"
echo
print_info "Useful PM2 commands:"
echo " pm2 status - Check server status"
echo " pm2 logs chatrtc-server - View logs"
echo " pm2 restart chatrtc-server - Restart server"
echo " pm2 stop chatrtc-server - Stop server"
;;
*)
print_error "Invalid mode. Use: dev, prod, or pm2"
exit 1
;;
esac
}
# Function to test server
test_server() {
local SERVER_URL=${1:-"http://localhost:3000"}
print_info "Testing server at $SERVER_URL..."
if [ -f "test-client.js" ]; then
node test-client.js "$SERVER_URL"
else
print_warning "test-client.js not found. Skipping server test."
fi
}
# Function to show server info
show_info() {
local PORT=${PORT:-3000}
echo
print_status "Server Information:"
echo " 📍 Local URL: http://localhost:$PORT"
echo " 🌐 Network URL: http://$LOCAL_IP:$PORT"
echo " 📱 Android Emulator URL: http://10.0.2.2:$PORT"
echo
print_info "Update your Android app's WebRTCManager.java:"
echo " private static final String SIGNALING_SERVER_URL = \"http://$LOCAL_IP:$PORT\";"
echo
print_info "Available endpoints:"
echo " GET / - Server status page"
echo " GET /api/status - Server statistics"
echo " GET /api/rooms - Active rooms"
echo " GET /health - Health check"
echo
}
# Function to show usage
show_usage() {
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo
echo "Commands:"
echo " start [dev|prod|pm2] - Start the server (default: dev)"
echo " test [URL] - Test server connection"
echo " info - Show server information"
echo " stop - Stop PM2 server"
echo " logs - Show PM2 logs"
echo " status - Show PM2 status"
echo
echo "Examples:"
echo " $0 start dev - Start in development mode"
echo " $0 start pm2 - Start with PM2 process manager"
echo " $0 test - Test local server"
echo " $0 info - Show connection information"
}
# Parse command line arguments
case ${1:-start} in
"start")
MODE=${2:-dev}
show_info
start_server "$MODE"
;;
"test")
test_server "$2"
;;
"info")
show_info
;;
"stop")
if command -v pm2 &> /dev/null; then
pm2 stop chatrtc-server
print_status "Server stopped"
else
print_error "PM2 not installed"
fi
;;
"logs")
if command -v pm2 &> /dev/null; then
pm2 logs chatrtc-server
else
print_error "PM2 not installed"
fi
;;
"status")
if command -v pm2 &> /dev/null; then
pm2 status chatrtc-server
else
print_error "PM2 not installed"
fi
;;
"help"|"-h"|"--help")
show_usage
;;
*)
print_error "Unknown command: $1"
show_usage
exit 1
;;
esac