refactor: 前后端目录结构模块化 + v2 优化移植与缺陷修复

前端:
- src/srv/api → src/api,按域分文件夹(auth/cluster/host/storage/deployment/collector)
- 新增 http.ts 引擎(http.get/post/put/delete),域模块改用语义化调用
- index.html 增加 favicon(匹配应用 logo 的 emerald + Activity 脉冲)

后端分层增强(保留包名):
- 新增 repository/:从 models 拆出 JsonDB 持久化层(InitDB/GetDB/Shutdown/RequestSave)
- 新增 dto/:从 controllers 抽出各域请求契约(带 binding 校验)
- 新增 pkg/crypto、pkg/jwt:从 utils 拆出安全原语(外部 jwt 别名 jwtv5 避免冲突)
- models 只留领域实体 + sanitizer;utils 只留 gin/应用相关(response/errors)

v2 优化移植与缺陷修复:
- 移植密钥分离/SSH TOFU/并发写安全/Loki 生产级配置/Docker 加固
- 修复 deployment.go 并发丢更新、Helm values 重复键、EncryptField 哨兵、loki-gateway 端点等审计缺陷
- 实现真正的优雅关停(main.go http.Server + signal + repository.Shutdown)
- 修复登录契约:后端返回 accessToken,前端 auth.ts/Login.tsx 对齐
- 补 crypto/jwt/repository 单元测试(含并发写回归)

验证: go build/vet/test 全绿;npm run build 通过;登录→鉴权→优雅关停冒烟通过

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Logan.LiandClaude committed 2026-06-30 10:46:26 +08:00
1 parent fb8d10231a
commit 327e282356
93 files changed
+8433 -3089

No files matched your search

+31 -8
View File
@@ -116,6 +116,12 @@ start_backend() {
export SERVER_PORT=$BACKEND_PORT
export DATA_DIR="$SCRIPT_DIR/data"
export JWT_SECRET="${JWT_SECRET:-lokideploy-secret-key-change-in-production}"
# v2 安全模型:AES 加密需要独立密钥(用于主机密码/私钥/kubeconfig 等敏感字段)。
# config.go 在 ENCRYPTION_KEY 缺失或 <32 字符时会 log.Fatal 拒绝启动,故此处提供开发默认值。
if [ -z "${ENCRYPTION_KEY:-}" ]; then
warning "未设置 ENCRYPTION_KEY,使用开发默认值。生产环境请通过 .env 或环境变量提供独立随机密钥!"
export ENCRYPTION_KEY="lokideploy-dev-encryption-key-change-me!!"
fi
nohup "./$BACKEND_BIN" >> logs/backend.log 2>&1 &
echo $! > "$BACKEND_PID_FILE"
@@ -136,15 +142,35 @@ start_backend() {
return 0
}
# 等待进程退出:先 SIGTERM 优雅关停,超时再 SIGKILL
graceful_kill() {
local pid=$1
local name=$2
local grace=${3:-8}
if [ -z "$pid" ] || ! kill -0 "$pid" 2>/dev/null; then
return 0
fi
info "正在优雅停止 $name (PID: $pid)..."
kill -TERM "$pid" 2>/dev/null || true
local i=0
while kill -0 "$pid" 2>/dev/null && [ $i -lt $((grace * 2)) ]; do
sleep 0.5
i=$((i + 1))
done
if kill -0 "$pid" 2>/dev/null; then
warning "$name 未在 ${grace}s 内退出,强制终止..."
kill -KILL "$pid" 2>/dev/null || true
fi
success "$name 已停止"
}
stop_backend() {
if [ -f "$BACKEND_PID_FILE" ]; then
local pid=$(cat "$BACKEND_PID_FILE" 2>/dev/null || echo "")
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
success "后端服务已停止"
fi
graceful_kill "$pid" "后端服务" 8
rm -f "$BACKEND_PID_FILE"
fi
# 兜底:清理仍占用端口的残留进程(非 PID 文件管理的)
kill_port $BACKEND_PORT
}
@@ -186,10 +212,7 @@ start_frontend() {
stop_frontend() {
if [ -f "$FRONTEND_PID_FILE" ]; then
local pid=$(cat "$FRONTEND_PID_FILE" 2>/dev/null || echo "")
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
success "前端服务已停止"
fi
graceful_kill "$pid" "前端服务" 5
rm -f "$FRONTEND_PID_FILE"
fi
kill_port $FRONTEND_PORT