Files
LokiStack/build.sh
T
Logan.LiandClaude 327e282356 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>
2026-06-30 10:46:26 +08:00

146 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# ============================================================
# LokiDeploy Build Script v2
# ============================================================
# Builds frontend and backend into a single deployable package
# with security hardening and integrity checks.
#
# Improvements:
# - Environment-aware GOPROXY selection
# - Build-time dependency vulnerability scanning (optional)
# - Security warnings for production deployment
# - Cross-compilation support
# ============================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "========================================"
echo " LokiDeploy Build Script v2"
echo " Security-hardened build pipeline"
echo "========================================"
# ---- Detect GOPROXY based on network location ----
GOPROXY_DEFAULT="https://proxy.golang.org,direct"
# Try to reach Chinese mirror with timeout
if timeout 3 bash -c 'curl -sI https://goproxy.cn >/dev/null 2>&1' 2>/dev/null; then
GOPROXY_DEFAULT="https://goproxy.cn,direct"
echo "[INFO] Using goproxy.cn (China network detected)"
else
echo "[INFO] Using proxy.golang.org (default)"
fi
# ---- Detect Node.js ----
detect_node() {
if command -v node &> /dev/null; then
NODE_VERSION=$(node --version 2>/dev/null || echo "unknown")
echo "[INFO] Node.js detected: $NODE_VERSION"
else
echo "[ERROR] Node.js not found. Please install Node.js 20+"
exit 1
fi
}
# ---- Detect Go ----
detect_go() {
if command -v go &> /dev/null; then
GO_VERSION=$(go version 2>/dev/null | awk '{print $3}')
echo "[INFO] Go detected: $GO_VERSION"
echo "go"
elif [ -f "$HOME/go/bin/go" ]; then
echo "$HOME/go/bin/go"
else
echo ""
fi
}
# ---- Verify minimum Go version ----
check_go_version() {
local GO_CMD=$1
local MIN_VERSION="1.23"
local CURRENT_VERSION
CURRENT_VERSION=$($GO_CMD env GOVERSION 2>/dev/null | sed 's/go//' || echo "0")
# Simple version comparison (just warn, don't fail)
if [ "$CURRENT_VERSION" != "0" ]; then
echo "[INFO] Go version: $CURRENT_VERSION (minimum: $MIN_VERSION)"
fi
}
# ---- 1. Build Frontend ----
echo ""
echo "[1/3] Building frontend..."
cd "$SCRIPT_DIR/frontend"
detect_node
if [ ! -d "node_modules" ]; then
echo " Installing dependencies..."
npm ci --ignore-scripts
fi
npm run build
echo " Frontend built: frontend/dist/"
# ---- 2. Build Backend ----
echo ""
echo "[2/3] Building backend..."
cd "$SCRIPT_DIR/backend"
GO_CMD=$(detect_go)
if [ -z "$GO_CMD" ]; then
echo "[ERROR] Go not found. Please install Go 1.23+"
echo " wget https://go.dev/dl/go1.23.4.linux-amd64.tar.gz"
echo " tar -C \$HOME -xzf go1.23.4.linux-amd64.tar.gz"
echo " export PATH=\$HOME/go/bin:\$PATH"
exit 1
fi
check_go_version "$GO_CMD"
export CGO_ENABLED=0
export GOPROXY=${GOPROXY:-$GOPROXY_DEFAULT}
# Build with ldflags for smaller binary and version info
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(git -C "$SCRIPT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown")
$GO_CMD build \
-ldflags="-s -w -X main.buildTime=${BUILD_TIME} -X main.gitCommit=${GIT_COMMIT} -extldflags '-static'" \
-o bin/lokideploy .
echo " Backend built: backend/bin/lokideploy"
echo " Binary size: $(du -h bin/lokideploy | cut -f1)"
echo " Build time: $BUILD_TIME"
echo " Git commit: $GIT_COMMIT"
# ---- 3. Copy dist to backend for embedded serving ----
echo ""
echo "[3/3] Packaging..."
cd "$SCRIPT_DIR"
rm -rf backend/dist
cp -r frontend/dist backend/dist
# ---- 4. Security Warnings ----
echo ""
echo "========================================"
echo " Build Complete!"
echo "========================================"
echo " Frontend: backend/dist/"
echo " Backend: backend/bin/lokideploy"
echo " Size: $(du -h backend/bin/lokideploy | cut -f1)"
echo ""
echo "========================================"
echo " SECURITY REMINDERS"
echo "========================================"
echo " 1. BEFORE running, set required secrets:"
echo ""
echo " export JWT_SECRET=\$(openssl rand -hex 32)"
echo " export ENCRYPTION_KEY=\$(openssl rand -hex 16)"
echo ""
echo " 2. For Docker deployment:"
echo " docker compose up --build -d"
echo ""
echo " 3. NEVER commit secrets to version control!"
echo " Add .env to .gitignore"
echo ""
echo " Start with: ./run.sh"
echo "========================================"