Files
vstats/scripts/start-dev.sh
2025-12-08 20:31:42 +08:00

108 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
#
# Development server start script
# Rebuilds frontend and backend, then starts the server
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SERVER_DIR="$PROJECT_ROOT/server-go"
WEB_DIR="$PROJECT_ROOT/web"
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${BLUE}════════════════════════════════════════${NC}"
echo -e "${BLUE} vStats Development Server ${NC}"
echo -e "${BLUE}════════════════════════════════════════${NC}"
echo ""
# Check if Go is installed
if ! command -v go &> /dev/null; then
echo -e "${RED}Error: Go is not installed. Please install Go 1.22 or later.${NC}"
exit 1
fi
# Build frontend
echo -e "${YELLOW}[1/4]${NC} Building frontend..."
cd "$WEB_DIR"
if [ ! -d "node_modules" ]; then
npm install
fi
npm run build
echo -e "${GREEN}✓ Frontend built${NC}"
echo ""
# Build backend
echo -e "${YELLOW}[2/4]${NC} Building backend..."
cd "$SERVER_DIR/cmd/server"
go build -o "$PROJECT_ROOT/vstats-server-dev" .
echo -e "${GREEN}✓ Backend built${NC}"
echo ""
# Build agent (optional, for testing)
echo -e "${YELLOW}[3/4]${NC} Building agent..."
cd "$SERVER_DIR/cmd/agent"
go build -o "$PROJECT_ROOT/vstats-agent-dev" .
echo -e "${GREEN}✓ Agent built${NC}"
echo ""
# Set environment variables
export VSTATS_PORT=3001
export VSTATS_WEB_DIR="$WEB_DIR/dist"
SERVER_BINARY="$PROJECT_ROOT/vstats-server-dev"
# Kill existing vstats-server processes
echo -e "${YELLOW}[0/4]${NC} Stopping existing services..."
if pgrep -f "vstats-server" > /dev/null 2>&1; then
echo -e "${YELLOW} Found running vstats-server processes, stopping...${NC}"
pkill -f "vstats-server" || true
sleep 1
# Force kill if still running
if pgrep -f "vstats-server" > /dev/null 2>&1; then
pkill -9 -f "vstats-server" || true
sleep 0.5
fi
echo -e "${GREEN}✓ Old services stopped${NC}"
else
echo -e "${GREEN}✓ No existing services found${NC}"
fi
echo ""
# Reset password to get a fresh one for development
echo -e "${YELLOW}[4/4]${NC} Resetting admin password..."
PASSWORD_OUTPUT=$("$SERVER_BINARY" --reset-password 2>&1)
# Extract password from output (format: "║ New admin password: {password} ║")
# The password is a 16-char alphanumeric string
ADMIN_PASSWORD=$(echo "$PASSWORD_OUTPUT" | grep "New admin password:" | sed -E 's/.*New admin password:[[:space:]]+([A-Za-z0-9]+)[[:space:]]*.*$/\1/')
if [ -z "$ADMIN_PASSWORD" ]; then
# Fallback: try to match any word after "password:"
ADMIN_PASSWORD=$(echo "$PASSWORD_OUTPUT" | grep -oE 'password:[[:space:]]+[A-Za-z0-9]+' | tail -1 | sed -E 's/password:[[:space:]]+//')
fi
if [ -z "$ADMIN_PASSWORD" ]; then
echo -e "${RED}Warning: Could not extract password from output${NC}"
echo "Output was:"
echo "$PASSWORD_OUTPUT"
ADMIN_PASSWORD="(check output above)"
fi
echo -e "${GREEN}✓ Password reset${NC}"
echo ""
echo -e "${BLUE}════════════════════════════════════════${NC}"
echo -e "${GREEN}Server: http://localhost:3001${NC}"
echo -e "${GREEN}Web: $VSTATS_WEB_DIR${NC}"
echo -e "${GREEN}Pass: ${ADMIN_PASSWORD}${NC}"
echo -e "${BLUE}════════════════════════════════════════${NC}"
echo ""
# Change to project root and run
cd "$PROJECT_ROOT"
"$SERVER_BINARY"