-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstart_dev_web_client.sh
More file actions
executable file
·286 lines (244 loc) · 8.67 KB
/
Copy pathstart_dev_web_client.sh
File metadata and controls
executable file
·286 lines (244 loc) · 8.67 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
#--------------------------------------------------------------------------
#
# pgEdge AI DBA Workbench Web Client Development Startup Script
#
# Copyright (c) 2025 - 2026, pgEdge, Inc.
# This software is released under The PostgreSQL License
#
# Note: For production deployments, use Docker Compose instead:
# docker-compose up
#
#--------------------------------------------------------------------------
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_DIR="$SCRIPT_DIR/bin"
CLIENT_DIR="$SCRIPT_DIR/client"
# Source directories
SERVER_SRC_DIR="$SCRIPT_DIR/server/src"
# Configuration files
SERVER_CONFIG="$BIN_DIR/ai-dba-server.yaml"
EXAMPLE_CONFIG="$SCRIPT_DIR/examples/ai-dba-server.yaml"
SERVER_BIN="$BIN_DIR/ai-dba-server"
# Log files
SERVER_LOG="/tmp/ai-dba-server.log"
VITE_LOG="/tmp/ai-dba-vite.log"
# PID files for cleanup
SERVER_PID=""
CLIENT_PID=""
# Cleanup function
cleanup() {
echo -e "\n${YELLOW}Shutting down services...${NC}"
if [ ! -z "$CLIENT_PID" ]; then
echo "Stopping Vite dev server (PID: $CLIENT_PID)..."
kill $CLIENT_PID 2>/dev/null || true
fi
if [ ! -z "$SERVER_PID" ]; then
echo "Stopping MCP server (PID: $SERVER_PID)..."
kill $SERVER_PID 2>/dev/null || true
fi
echo -e "${GREEN}Cleanup complete${NC}"
}
# Set up trap for cleanup
trap cleanup EXIT INT TERM
# Create bin directory if needed
mkdir -p "$BIN_DIR"
# Copy example config if no config exists
if [ ! -f "$SERVER_CONFIG" ]; then
if [ -f "$EXAMPLE_CONFIG" ]; then
echo -e "${YELLOW}No config found, copying example config to $SERVER_CONFIG${NC}"
cp "$EXAMPLE_CONFIG" "$SERVER_CONFIG"
echo -e "${YELLOW}Please edit $SERVER_CONFIG with your settings${NC}"
else
echo -e "${RED}Error: Example config not found at $EXAMPLE_CONFIG${NC}"
exit 1
fi
fi
# Build or rebuild server binary if needed
if [ ! -f "$SERVER_BIN" ]; then
echo -e "${BLUE}Building MCP server binary...${NC}"
cd "$SERVER_SRC_DIR"
go build -o "$SERVER_BIN" ./cmd/mcp-server
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build MCP server${NC}"
exit 1
fi
cd "$SCRIPT_DIR"
else
echo -e "${BLUE}Checking if server binary needs rebuilding...${NC}"
# Check if any Go source files are newer than the binary
if [ -n "$(find "$SERVER_SRC_DIR" -name "*.go" -newer "$SERVER_BIN" 2>/dev/null)" ]; then
echo -e "${BLUE}Source files changed, rebuilding MCP server...${NC}"
cd "$SERVER_SRC_DIR"
go build -o "$SERVER_BIN" ./cmd/mcp-server
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build MCP server${NC}"
exit 1
fi
cd "$SCRIPT_DIR"
else
echo -e "${GREEN}Server binary is up to date${NC}"
fi
fi
# Check if client dependencies need updating
# Platform marker tracks OS/arch to detect cross-platform node_modules
PLATFORM_MARKER="$CLIENT_DIR/node_modules/.platform_marker"
CURRENT_PLATFORM="$(uname -s)-$(uname -m)"
needs_npm_install() {
# No node_modules directory
if [ ! -d "$CLIENT_DIR/node_modules" ]; then
echo "node_modules directory missing"
return 0
fi
# Platform mismatch (cross-platform development)
if [ ! -f "$PLATFORM_MARKER" ] || [ "$(cat "$PLATFORM_MARKER" 2>/dev/null)" != "$CURRENT_PLATFORM" ]; then
echo "platform changed (native modules need rebuild)"
return 0
fi
# Package files newer than node_modules
if [ "$CLIENT_DIR/package.json" -nt "$CLIENT_DIR/node_modules" ]; then
echo "package.json changed"
return 0
fi
if [ -f "$CLIENT_DIR/package-lock.json" ] && [ "$CLIENT_DIR/package-lock.json" -nt "$CLIENT_DIR/node_modules" ]; then
echo "package-lock.json changed"
return 0
fi
return 1
}
echo -e "${BLUE}Checking if client dependencies need updating...${NC}"
INSTALL_REASON=$(needs_npm_install) && NEEDS_INSTALL=true || NEEDS_INSTALL=false
if $NEEDS_INSTALL; then
echo -e "${BLUE}Installing client dependencies ($INSTALL_REASON)...${NC}"
cd "$CLIENT_DIR"
npm install
# Record the platform for future cross-platform detection
echo "$CURRENT_PLATFORM" > "$PLATFORM_MARKER"
cd "$SCRIPT_DIR"
else
echo -e "${GREEN}Client dependencies are up to date${NC}"
fi
echo -e "${BLUE}+------------------------------------------------------------+${NC}"
echo -e "${BLUE}| pgEdge AI DBA Workbench Development Startup |${NC}"
echo -e "${BLUE}+------------------------------------------------------------+${NC}"
echo ""
# Start MCP server
echo -e "${GREEN}[1/2] Starting MCP Server (HTTP mode with auth)...${NC}"
cd "$BIN_DIR"
"$SERVER_BIN" --config "$SERVER_CONFIG" > "$SERVER_LOG" 2>&1 &
SERVER_PID=$!
cd "$SCRIPT_DIR"
echo " PID: $SERVER_PID"
echo " Config: $SERVER_CONFIG"
echo " Log: $SERVER_LOG"
# Wait a moment for process to stabilize
sleep 1
# Check if MCP server process is still running (catch immediate failures like port conflicts)
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo -e "${RED}Error: MCP Server process exited immediately${NC}"
echo "This usually means the port is already in use or there's a configuration error."
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
# Wait for MCP server to be ready
echo -e "${GREEN}Waiting for MCP Server to be ready...${NC}"
MAX_RETRIES=30
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
echo " MCP Server is ready!"
break
fi
# Check if process is still running
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo -e "${RED}Error: MCP Server process died during startup${NC}"
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 1
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo -e "${RED}Error: MCP Server failed to start within 30 seconds${NC}"
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
# Start Vite dev server
echo -e "${GREEN}[2/2] Starting Vite Dev Server (Frontend)...${NC}"
cd "$CLIENT_DIR"
npm run dev > "$VITE_LOG" 2>&1 &
CLIENT_PID=$!
cd "$SCRIPT_DIR"
echo " PID: $CLIENT_PID"
echo " Port: 5173 (Vite default)"
echo " Log: $VITE_LOG"
# Wait a moment for process to stabilize
sleep 1
# Check if Vite server process is still running (catch immediate failures like port conflicts)
if ! kill -0 $CLIENT_PID 2>/dev/null; then
echo -e "${RED}Error: Vite Server process exited immediately${NC}"
echo "This usually means port 5173 is already in use or there's a configuration error."
echo "Check the log file: $VITE_LOG"
tail -20 "$VITE_LOG"
exit 1
fi
# Wait for Vite server to be ready
echo -e "${GREEN}Waiting for Vite Server to be ready...${NC}"
MAX_RETRIES=30
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if grep -q "Local:" "$VITE_LOG" 2>/dev/null; then
break
fi
# Check if process is still running
if ! kill -0 $CLIENT_PID 2>/dev/null; then
echo -e "${RED}Error: Vite Server process died during startup${NC}"
echo "Check the log file: $VITE_LOG"
tail -20 "$VITE_LOG"
exit 1
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 1
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo -e "${RED}Error: Vite Server failed to start within 30 seconds${NC}"
echo "Check the log file: $VITE_LOG"
tail -20 "$VITE_LOG"
exit 1
fi
echo ""
echo -e "${GREEN}+------------------------------------------------------------+${NC}"
echo -e "${GREEN}| AI DBA Workbench Development is now running! |${NC}"
echo -e "${GREEN}+------------------------------------------------------------+${NC}"
echo ""
echo -e "${BLUE}Services:${NC}"
echo " - MCP Server: http://localhost:8080"
echo " - Web Interface: http://localhost:5173"
echo ""
echo -e "${BLUE}Logs:${NC}"
echo " - MCP Server: $SERVER_LOG"
echo " - Vite Dev: $VITE_LOG"
echo ""
echo -e "${BLUE}Login Credentials (for web interface):${NC}"
echo " - Create users with: ./bin/ai-dba-server add-user"
echo " - List users with: ./bin/ai-dba-server list-users"
echo ""
echo -e "${BLUE}Architecture:${NC}"
echo " - Web client communicates with MCP server via REST API"
echo " - LLM API keys are stored server-side (configured in ai-dba-server.yaml)"
echo " - MCP server provides tool/resource access and LLM proxy endpoints"
echo ""
echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}"
echo ""
# Wait for interrupt
wait $CLIENT_PID