mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
修复 维护模式拦截异常的问题
This commit is contained in:
207
build.sh
207
build.sh
@@ -3,12 +3,91 @@
|
||||
# 遇到错误立即退出
|
||||
set -e
|
||||
|
||||
package_update_zip() {
|
||||
local os=$1
|
||||
local arch=$2
|
||||
local output_dir=$3
|
||||
|
||||
local version
|
||||
version="$(get_app_version)" || version="unknown"
|
||||
|
||||
local exe_name="${APP_NAME}"
|
||||
if [ "$os" = "windows" ]; then
|
||||
exe_name="${APP_NAME}.exe"
|
||||
fi
|
||||
|
||||
local src_exe="dist/${output_dir}/${exe_name}"
|
||||
if [ ! -f "$src_exe" ]; then
|
||||
echo -e "${RED}❌ 未找到编译产物: ${src_exe}${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! command -v zip >/dev/null 2>&1; then
|
||||
echo -e "${RED}❌ 未找到 zip 命令,无法生成更新包(请安装 zip 工具)${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
mkdir -p "dist/packages"
|
||||
local pkg_name="${APP_NAME}-${os}-${arch}-v${version}.zip"
|
||||
local pkg_path="dist/packages/${pkg_name}"
|
||||
|
||||
local tmp_dir
|
||||
tmp_dir="$(mktemp -d)"
|
||||
cp "$src_exe" "${tmp_dir}/${exe_name}"
|
||||
|
||||
(
|
||||
cd "$tmp_dir"
|
||||
zip -q -9 -r "$SCRIPT_DIR/${pkg_path}" "${exe_name}"
|
||||
)
|
||||
rm -rf "$tmp_dir"
|
||||
|
||||
echo -e "${GREEN}✅ 更新包已生成: ${pkg_path}${NC}"
|
||||
|
||||
local sha256
|
||||
sha256="$(get_sha256_hex "$pkg_path")" || sha256=""
|
||||
if [ -n "$sha256" ]; then
|
||||
echo "${sha256} ${pkg_name}" > "${pkg_path}.sha256"
|
||||
echo -e "${GREEN}✅ SHA256 已生成: ${pkg_path}.sha256${NC}\n"
|
||||
else
|
||||
echo -e "${YELLOW}⚠️ 未找到 shasum/sha256sum,跳过生成 SHA256 文件${NC}\n"
|
||||
fi
|
||||
}
|
||||
|
||||
build_all_update_packages() {
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
echo -e "${YELLOW}📦 开始构建所有架构更新包...${NC}"
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
|
||||
build_backend "windows" "amd64" "windows_amd64" "Windows 64位"
|
||||
package_update_zip "windows" "amd64" "windows_amd64"
|
||||
|
||||
build_backend "linux" "arm64" "linux_arm64" "Linux ARM64"
|
||||
package_update_zip "linux" "arm64" "linux_arm64"
|
||||
|
||||
build_backend "linux" "amd64" "linux_amd64" "Linux 64位"
|
||||
package_update_zip "linux" "amd64" "linux_amd64"
|
||||
|
||||
build_backend "darwin" "arm64" "darwin_arm64" "macOS (Apple Silicon)"
|
||||
package_update_zip "darwin" "arm64" "darwin_arm64"
|
||||
|
||||
build_backend "darwin" "amd64" "darwin_amd64" "macOS (Intel)"
|
||||
package_update_zip "darwin" "amd64" "darwin_amd64"
|
||||
|
||||
echo -e "${GREEN}🎉 所有架构更新包已生成,产物已保存至 ./dist/packages 目录下!${NC}\n"
|
||||
}
|
||||
|
||||
# ==========================================
|
||||
# 切换到脚本所在目录
|
||||
# ==========================================
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
# ==========================================
|
||||
# 项目配置
|
||||
# ==========================================
|
||||
APP_NAME="NetworkAuth"
|
||||
STATUS_FILE="constants/status.go"
|
||||
|
||||
# ==========================================
|
||||
# 颜色定义
|
||||
# ==========================================
|
||||
@@ -22,6 +101,89 @@ NC='\033[0m' # No Color
|
||||
# 功能函数
|
||||
# ==========================================
|
||||
|
||||
get_app_version() {
|
||||
local status_file="${STATUS_FILE}"
|
||||
if [ ! -f "$status_file" ]; then
|
||||
echo ""
|
||||
return 1
|
||||
fi
|
||||
local line
|
||||
line="$(awk -F'"' '/AppVersion[[:space:]]*=[[:space:]]*"/ {print $2; exit 0}' "$status_file")"
|
||||
if [ -z "$line" ]; then
|
||||
echo ""
|
||||
return 1
|
||||
fi
|
||||
echo "$line"
|
||||
return 0
|
||||
}
|
||||
|
||||
bump_patch_version() {
|
||||
local current
|
||||
current="$(get_app_version)" || return 1
|
||||
|
||||
local v="${current#v}"
|
||||
v="${v#V}"
|
||||
|
||||
local major minor patch
|
||||
IFS='.' read -r major minor patch <<EOF
|
||||
$v
|
||||
EOF
|
||||
if [ -z "$major" ] || [ -z "$minor" ] || [ -z "$patch" ]; then
|
||||
echo -e "${RED}❌ 无法解析当前版本号: ${current}${NC}"
|
||||
return 1
|
||||
fi
|
||||
if ! [[ "$major" =~ ^[0-9]+$ && "$minor" =~ ^[0-9]+$ && "$patch" =~ ^[0-9]+$ ]]; then
|
||||
echo -e "${RED}❌ 版本号必须为 x.y.z 的数字格式: ${current}${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local next_patch=$((patch + 1))
|
||||
local next_version="${major}.${minor}.${next_patch}"
|
||||
|
||||
local status_file="${STATUS_FILE}"
|
||||
local tmp_file
|
||||
tmp_file="$(mktemp)"
|
||||
awk -v ver="$next_version" '
|
||||
BEGIN { updated=0 }
|
||||
{
|
||||
if ($0 ~ /AppVersion[[:space:]]*=[[:space:]]*"/ && updated==0) {
|
||||
sub(/AppVersion[[:space:]]*=[[:space:]]*"[^"]*"/, sprintf("AppVersion = %c%s%c", 34, ver, 34))
|
||||
updated=1
|
||||
}
|
||||
print
|
||||
}
|
||||
END { if (updated==0) exit 2 }
|
||||
' "$status_file" > "$tmp_file"
|
||||
local rc=$?
|
||||
if [ $rc -ne 0 ]; then
|
||||
rm -f "$tmp_file"
|
||||
echo -e "${RED}❌ 自动迭代版本失败:未找到 AppVersion 定义${NC}"
|
||||
return 1
|
||||
fi
|
||||
mv "$tmp_file" "$status_file"
|
||||
|
||||
echo -e "${GREEN}✅ 版本号已迭代: ${current} -> ${next_version}${NC}"
|
||||
return 0
|
||||
}
|
||||
|
||||
get_sha256_hex() {
|
||||
local file_path=$1
|
||||
if [ -z "$file_path" ] || [ ! -f "$file_path" ]; then
|
||||
echo ""
|
||||
return 1
|
||||
fi
|
||||
if command -v shasum >/dev/null 2>&1; then
|
||||
shasum -a 256 "$file_path" | awk '{print $1}'
|
||||
return 0
|
||||
fi
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "$file_path" | awk '{print $1}'
|
||||
return 0
|
||||
fi
|
||||
echo ""
|
||||
return 1
|
||||
}
|
||||
|
||||
# 编译并拷贝前端
|
||||
build_frontend() {
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
@@ -54,9 +216,9 @@ build_backend() {
|
||||
local desc=$4
|
||||
|
||||
# 确定可执行文件名称
|
||||
local exe_name="NetworkAuth"
|
||||
if [ "$os" = "NetworkAuth" ]; then
|
||||
exe_name="NetworkAuth.exe"
|
||||
local exe_name="${APP_NAME}"
|
||||
if [ "${os}" = "windows" ]; then
|
||||
exe_name="${APP_NAME}.exe"
|
||||
fi
|
||||
|
||||
# 创建对应架构的输出目录
|
||||
@@ -98,15 +260,10 @@ show_menu() {
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
echo -e "${GREEN} 项目构建脚本菜单 ${NC}"
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
echo -e "1. 🚀 一键全部构建 (前端 + 所有架构后端)"
|
||||
echo -e "2. 📦 仅编译所有后端架构"
|
||||
echo -e "3. 🌐 仅编译前端并拷贝"
|
||||
echo -e "-------------------------------------"
|
||||
echo -e "4. 🪟 编译后端: Windows 64位"
|
||||
echo -e "5. 🐧 编译后端: Linux ARM64"
|
||||
echo -e "6. 🐧 编译后端: Linux 64位"
|
||||
echo -e "7. 🍎 编译后端: macOS (Apple Silicon)"
|
||||
echo -e "8. 🍎 编译后端: macOS (Intel)"
|
||||
echo -e "1. 🔁 自动构建 + 生成更新包"
|
||||
echo -e "2. 🚀 全部构建 (前端 + 后端)"
|
||||
echo -e "3. 📦 编译所有后端架构"
|
||||
echo -e "4. 🌐 仅编译前端并拷贝"
|
||||
echo -e "-------------------------------------"
|
||||
echo -e "0. ❌ 退出"
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
@@ -118,40 +275,26 @@ show_menu() {
|
||||
# ==========================================
|
||||
while true; do
|
||||
show_menu
|
||||
read choice
|
||||
read -r choice
|
||||
echo ""
|
||||
case $choice in
|
||||
1)
|
||||
bump_patch_version
|
||||
build_frontend
|
||||
build_all_backend
|
||||
build_all_update_packages
|
||||
pause_and_return
|
||||
;;
|
||||
2)
|
||||
build_frontend
|
||||
build_all_backend
|
||||
pause_and_return
|
||||
;;
|
||||
3)
|
||||
build_frontend
|
||||
build_all_backend
|
||||
pause_and_return
|
||||
;;
|
||||
4)
|
||||
build_backend "windows" "amd64" "windows_amd64" "Windows 64位"
|
||||
pause_and_return
|
||||
;;
|
||||
5)
|
||||
build_backend "linux" "arm64" "linux_arm64" "Linux ARM64"
|
||||
pause_and_return
|
||||
;;
|
||||
6)
|
||||
build_backend "linux" "amd64" "linux_amd64" "Linux 64位"
|
||||
pause_and_return
|
||||
;;
|
||||
7)
|
||||
build_backend "darwin" "arm64" "darwin_arm64" "macOS (Apple Silicon)"
|
||||
pause_and_return
|
||||
;;
|
||||
8)
|
||||
build_backend "darwin" "amd64" "darwin_amd64" "macOS (Intel)"
|
||||
build_frontend
|
||||
pause_and_return
|
||||
;;
|
||||
0)
|
||||
|
||||
@@ -131,8 +131,17 @@ func GetDefaultAppConfig() *AppConfig {
|
||||
// Init 初始化配置文件
|
||||
func Init(cfgFilePath string) {
|
||||
if !filepath.IsAbs(cfgFilePath) {
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
candidate := filepath.Clean(filepath.Join(wd, cfgFilePath))
|
||||
if _, statErr := os.Stat(candidate); statErr == nil {
|
||||
cfgFilePath = candidate
|
||||
} else {
|
||||
cfgFilePath = filepath.Join(utils.GetRootDir(), cfgFilePath)
|
||||
}
|
||||
} else {
|
||||
cfgFilePath = filepath.Join(utils.GetRootDir(), cfgFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
currentConfigFilePath = cfgFilePath
|
||||
viper.SetConfigFile(cfgFilePath)
|
||||
|
||||
@@ -21,6 +21,11 @@ func MaintenanceMiddleware() gin.HandlerFunc {
|
||||
|
||||
path := c.Request.URL.Path
|
||||
|
||||
if !strings.HasPrefix(path, "/api") {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 允许管理员后台相关接口(以便管理员登录关闭维护模式)
|
||||
// 包括登录页、登录接口、API接口、CSRF Token等
|
||||
if strings.HasPrefix(path, "/api/admin") {
|
||||
|
||||
Reference in New Issue
Block a user