-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·351 lines (303 loc) · 8.9 KB
/
start.sh
File metadata and controls
executable file
·351 lines (303 loc) · 8.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# 项目启动脚本 - 后台启动前端和后端,支持热加载
# 特性:
# 1. 自动杀死之前的进程,保证单实例运行
# 2. 后台启动,即使重启 IDE 也不会被重启
# 3. 支持热加载(前端使用 react-scripts,后端使用 air)
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"
# PID 文件路径
FRONTEND_PID_FILE="$PROJECT_ROOT/.frontend.pid"
BACKEND_PID_FILE="$PROJECT_ROOT/.backend.pid"
# 日志文件路径
LOG_DIR="$PROJECT_ROOT/logs"
FRONTEND_LOG="$LOG_DIR/frontend.log"
BACKEND_LOG="$LOG_DIR/backend.log"
# 创建日志目录
mkdir -p "$LOG_DIR"
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查进程是否存在
is_process_running() {
local pid=$1
if [ -z "$pid" ]; then
return 1
fi
if ps -p "$pid" > /dev/null 2>&1; then
return 0
else
return 1
fi
}
# 杀死进程及其子进程
kill_process_tree() {
local pid=$1
local name=$2
if is_process_running "$pid"; then
print_info "正在停止 $name (PID: $pid)..."
# 获取所有子进程
local children=$(pgrep -P "$pid" 2>/dev/null || true)
# 先杀死子进程
if [ -n "$children" ]; then
for child in $children; do
kill -TERM "$child" 2>/dev/null || true
done
fi
# 杀死主进程
kill -TERM "$pid" 2>/dev/null || true
# 等待进程结束(最多5秒)
local count=0
while is_process_running "$pid" && [ $count -lt 50 ]; do
sleep 0.1
count=$((count + 1))
done
# 如果还没结束,强制杀死
if is_process_running "$pid"; then
print_warning "$name 未响应 SIGTERM,使用 SIGKILL 强制终止..."
kill -9 "$pid" 2>/dev/null || true
sleep 0.5
fi
if is_process_running "$pid"; then
print_error "无法停止 $name (PID: $pid)"
return 1
else
print_success "$name 已停止"
return 0
fi
else
print_info "$name 未运行"
return 0
fi
}
# 停止前端
stop_frontend() {
if [ -f "$FRONTEND_PID_FILE" ]; then
local pid=$(cat "$FRONTEND_PID_FILE")
kill_process_tree "$pid" "前端服务"
rm -f "$FRONTEND_PID_FILE"
else
# 尝试通过端口查找进程
local pid=$(lsof -ti:54990 2>/dev/null || true)
if [ -n "$pid" ]; then
print_warning "发现占用端口 54990 的进程 (PID: $pid),正在停止..."
kill_process_tree "$pid" "前端服务"
fi
fi
}
# 停止后端
stop_backend() {
if [ -f "$BACKEND_PID_FILE" ]; then
local pid=$(cat "$BACKEND_PID_FILE")
kill_process_tree "$pid" "后端服务"
rm -f "$BACKEND_PID_FILE"
else
# 尝试通过端口查找进程(默认端口 8083)
local pid=$(lsof -ti:8083 2>/dev/null || true)
if [ -n "$pid" ]; then
print_warning "发现占用端口 8083 的进程 (PID: $pid),正在停止..."
kill_process_tree "$pid" "后端服务"
fi
fi
}
# 启动前端
start_frontend() {
print_info "正在启动前端服务..."
cd "$PROJECT_ROOT/frontend"
# 检查 node_modules 是否存在
if [ ! -d "node_modules" ]; then
print_info "首次运行,正在安装前端依赖..."
npm install
fi
# 后台启动前端,输出重定向到日志文件
nohup npm start > "$FRONTEND_LOG" 2>&1 &
local pid=$!
# 保存 PID
echo "$pid" > "$FRONTEND_PID_FILE"
print_success "前端服务已启动 (PID: $pid)"
print_info "前端日志: $FRONTEND_LOG"
print_info "前端地址: http://localhost:54990"
}
# 启动后端
start_backend() {
print_info "正在启动后端服务..."
cd "$PROJECT_ROOT"
# 设置 Go bin 路径
export GOPATH=$(go env GOPATH)
export PATH="$GOPATH/bin:$PATH"
# 检查是否安装了 air(用于热加载)
if ! command -v air &> /dev/null; then
print_warning "未安装 air,正在安装..."
go install github.com/air-verse/air@latest
fi
# 检查是否存在 .air.toml 配置文件
if [ ! -f ".air.toml" ]; then
print_info "创建 air 配置文件..."
cat > .air.toml << 'EOF'
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = ["server"]
bin = "./tmp/main"
cmd = "go build -tags dev -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "frontend", "data", "logs"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true
EOF
fi
# 后台启动后端,输出重定向到日志文件
nohup air > "$BACKEND_LOG" 2>&1 &
local pid=$!
# 保存 PID
echo "$pid" > "$BACKEND_PID_FILE"
print_success "后端服务已启动 (PID: $pid)"
print_info "后端日志: $BACKEND_LOG"
print_info "后端地址: http://localhost:8083"
}
# 显示状态
show_status() {
echo ""
print_info "========== 服务状态 =========="
# 前端状态
if [ -f "$FRONTEND_PID_FILE" ]; then
local pid=$(cat "$FRONTEND_PID_FILE")
if is_process_running "$pid"; then
print_success "前端服务: 运行中 (PID: $pid)"
else
print_error "前端服务: 已停止 (PID 文件存在但进程不存在)"
rm -f "$FRONTEND_PID_FILE"
fi
else
print_warning "前端服务: 未运行"
fi
# 后端状态
if [ -f "$BACKEND_PID_FILE" ]; then
local pid=$(cat "$BACKEND_PID_FILE")
if is_process_running "$pid"; then
print_success "后端服务: 运行中 (PID: $pid)"
else
print_error "后端服务: 已停止 (PID 文件存在但进程不存在)"
rm -f "$BACKEND_PID_FILE"
fi
else
print_warning "后端服务: 未运行"
fi
echo ""
print_info "查看日志:"
print_info " 前端: tail -f $FRONTEND_LOG"
print_info " 后端: tail -f $BACKEND_LOG"
echo ""
print_info "停止服务: ./start.sh stop"
print_info "重启服务: ./start.sh restart"
print_info "================================"
}
# 主函数
main() {
local command=${1:-start}
case "$command" in
start)
print_info "正在启动服务..."
stop_frontend
stop_backend
start_frontend
start_backend
show_status
;;
stop)
print_info "正在停止服务..."
stop_frontend
stop_backend
print_success "所有服务已停止"
;;
restart)
print_info "正在重启服务..."
stop_frontend
stop_backend
sleep 1
start_frontend
start_backend
show_status
;;
status)
show_status
;;
logs)
local service=${2:-all}
case "$service" in
frontend)
tail -f "$FRONTEND_LOG"
;;
backend)
tail -f "$BACKEND_LOG"
;;
all|*)
print_info "同时查看前端和后端日志 (Ctrl+C 退出)..."
tail -f "$FRONTEND_LOG" "$BACKEND_LOG"
;;
esac
;;
*)
echo "用法: $0 {start|stop|restart|status|logs [frontend|backend|all]}"
echo ""
echo "命令说明:"
echo " start - 启动前端和后端服务(会先停止已有进程)"
echo " stop - 停止所有服务"
echo " restart - 重启所有服务"
echo " status - 查看服务状态"
echo " logs - 查看日志(可选参数: frontend, backend, all)"
exit 1
;;
esac
}
# 执行主函数
main "$@"