-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstart.sh
executable file
·129 lines (109 loc) · 3.48 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Store the root directory path
ROOT_DIR="$(pwd)"
echo -e "${BLUE}Project root directory: ${ROOT_DIR}${NC}"
# Function to check if a port is in use
check_port() {
lsof -i :$1 > /dev/null 2>&1
return $?
}
# Function to wait for a service to be ready
wait_for_service() {
local port=$1
local service=$2
local max_attempts=30
local attempt=1
echo -e "${BLUE}Waiting for $service to start...${NC}"
while ! nc -z localhost $port && [ $attempt -le $max_attempts ]; do
sleep 1
attempt=$((attempt + 1))
done
if [ $attempt -le $max_attempts ]; then
echo -e "${GREEN}$service is ready!${NC}"
return 0
else
echo -e "${RED}$service failed to start${NC}"
return 1
fi
}
# Kill any existing processes on our ports
if check_port 3001; then
echo "Port 3001 in use, killing process..."
lsof -ti:3001 | xargs kill -9
fi
if check_port 24125; then
echo "Port 24125 in use, killing process..."
lsof -ti:24125 | xargs kill -9
fi
# Create a log directory
mkdir -p logs
# Install Python backend dependencies
echo -e "${BLUE}Installing Python backend dependencies...${NC}"
if [ -f "backend/requirements.txt" ]; then
cd "backend"
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cd "$ROOT_DIR"
else
echo -e "${RED}Error: requirements.txt not found in backend directory${NC}"
exit 1
fi
# Start Next.js frontend on port 3001
echo -e "${BLUE}Starting Next.js frontend...${NC}"
PORT=3001 npm run dev > logs/frontend.log 2>&1 &
FRONTEND_PID=$!
# Start FastAPI backend
echo -e "${BLUE}Starting FastAPI backend...${NC}"
cd backend
source venv/bin/activate
uvicorn app.main:app --host 0.0.0.0 --port 24125 --reload > ../logs/backend.log 2>&1 &
BACKEND_PID=$!
cd "$ROOT_DIR"
# Activate MCP server's virtual environment and start it
echo -e "${BLUE}Starting MCP server...${NC}"
source fast-markdown-mcp/venv/bin/activate
PYTHONPATH="$ROOT_DIR/fast-markdown-mcp/src" \
fast-markdown-mcp/venv/bin/python -m fast_markdown_mcp.server \
"$ROOT_DIR/storage/markdown" > logs/mcp.log 2>&1 &
MCP_PID=$!
# Wait for services to be ready
wait_for_service 3001 "Frontend" && \
wait_for_service 24125 "Backend"
if [ $? -eq 0 ]; then
echo -e "${GREEN}All services are running!${NC}"
echo -e "${BLUE}Frontend:${NC} http://localhost:3001"
echo -e "${BLUE}Backend:${NC} http://localhost:24125"
echo -e "${BLUE}Logs:${NC} ./logs/"
echo -e "${BLUE}Press Ctrl+C to stop all services${NC}"
# Open the frontend in the default browser
if [[ "$OSTYPE" == "darwin"* ]]; then
open http://localhost:3001
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
xdg-open http://localhost:3001
fi
# Handle graceful shutdown
cleanup() {
echo -e "\n${BLUE}Shutting down services...${NC}"
kill $FRONTEND_PID $BACKEND_PID $MCP_PID
wait $FRONTEND_PID $BACKEND_PID $MCP_PID 2>/dev/null
echo -e "${GREEN}All services stopped${NC}"
exit 0
}
trap cleanup SIGINT SIGTERM
# Keep the script running and monitor child processes
while kill -0 $FRONTEND_PID $BACKEND_PID $MCP_PID 2>/dev/null; do
sleep 1
done
echo -e "${RED}One or more services have stopped unexpectedly${NC}"
cleanup
else
echo -e "${RED}Failed to start all services${NC}"
kill $FRONTEND_PID $BACKEND_PID $MCP_PID 2>/dev/null
exit 1
fi