From cd14e88cc06bd5278f0988856b53789f4b3803dc Mon Sep 17 00:00:00 2001 From: coolxitech Date: Sat, 13 Dec 2025 14:08:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(server):=20=E5=AE=9E=E7=8E=B0=E8=B7=A8?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E8=87=AA=E5=8A=A8=E6=89=93=E5=BC=80=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除对 'open' 包的依赖,改用 Node.js 内置模块实现自动打开浏览器 - 新增 openUrl 函数,支持 macOS、Windows 和 Linux 系统 - 更新网络接口遍历逻辑中的变量命名以提高可读性 - 静态资源处理中间件改为使用 Bun.file API 并内嵌到可执行文件中 - 优化控制台输出信息,增强用户体验和提示清晰度 - 调整服务器监听地址为 0.0.0.0,并移除 trust proxy 设置 - 修改获取局域网 IP 的函数名称和注释结构使其更加明确 - 删除 package.json 和 lock 文件中不再使用的依赖项及相关条目 - 更新 GitHub Actions 工作流配置以适配新的编译和打包方式 - 在 CI 流程中启用代码压缩选项以减小最终二进制文件体积 --- .github/workflows/build.yml | 51 +++++++------------- package.json | 1 - pnpm-lock.yaml | 96 ------------------------------------- server.js | 70 +++++++++++++++++---------- 4 files changed, 62 insertions(+), 156 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8e35655..6004c04 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,7 @@ name: 构建多平台可执行程序 on: push: branches: - - main # 监听 main 分支的推送,不再强制要求手动推送 Tag + - main jobs: build: @@ -13,17 +13,11 @@ jobs: matrix: include: - os: windows-latest - artifact_name: ogame-windows - asset_name: ogame-windows.zip - executable: ogame.exe + executable: ogame-windows.exe - os: ubuntu-latest - artifact_name: ogame-linux - asset_name: ogame-linux.tar.gz - executable: ogame + executable: ogame-linux - os: macos-latest - artifact_name: ogame-macos - asset_name: ogame-macos.tar.gz - executable: ogame + executable: ogame-macos steps: - uses: actions/checkout@v4 @@ -33,39 +27,32 @@ jobs: with: bun-version: latest - # --- 关键步骤:读取 package.json 的版本号 --- - - name: Get version from package.json + # 获取 package.json 中的版本号 + - name: Get version id: get_version shell: bash run: | VERSION=$(node -p "require('./package.json').version") echo "VERSION=v$VERSION" >> $GITHUB_OUTPUT - echo "Detected version: v$VERSION" - name: Install Dependencies run: bun install + # 构建前端 Vue 项目 - name: Build Vue Frontend run: bun run build - - name: Compile Executable - run: bun build ./server.js --compile --outfile ${{ matrix.executable }} - - - name: Package Assets (Windows) - if: matrix.os == 'windows-latest' - run: Compress-Archive -Path "${{ matrix.executable }}", "docs" -DestinationPath "${{ matrix.asset_name }}" - - - name: Package Assets (Linux/macOS) - if: matrix.os != 'windows-latest' - run: tar -czvf ${{ matrix.asset_name }} ${{ matrix.executable }} docs/ + # 编译单文件二进制程序 + - name: Compile Single Executable + run: | + bun build ./server.js --compile --minify --outfile ${{ matrix.executable }} - name: Upload Artifact uses: actions/upload-artifact@v4 with: - name: ${{ matrix.artifact_name }} - path: ${{ matrix.asset_name }} + name: ${{ matrix.executable }} + path: ${{ matrix.executable }} - # 将版本号传递给 release 任务 outputs: app_version: ${{ steps.get_version.outputs.VERSION }} @@ -81,16 +68,12 @@ jobs: - name: Create GitHub Release uses: softprops/action-gh-release@v1 with: - # 使用从 build 任务传递过来的版本号 tag_name: ${{ needs.build.outputs.app_version }} name: Release ${{ needs.build.outputs.app_version }} - draft: false - prerelease: false - # 自动创建 Tag (非常重要) - generate_release_notes: true files: | - ogame-windows/ogame-windows.zip - ogame-linux/ogame-linux.tar.gz - ogame-macos/ogame-macos.tar.gz + ogame-windows.exe/ogame-windows.exe + ogame-linux/ogame-linux + ogame-macos/ogame-macos + generate_release_notes: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/package.json b/package.json index 2cb72e4..d111223 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "express": "^5.2.1", "file-saver": "^2.0.5", "lucide-vue-next": "^0.556.0", - "open": "^11.0.0", "pinia": "^3.0.4", "pinia-plugin-persistedstate": "^4.7.1", "reka-ui": "^2.6.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 613887c..fef97c8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,9 +38,6 @@ importers: lucide-vue-next: specifier: ^0.556.0 version: 0.556.0(vue@3.5.25(typescript@5.9.3)) - open: - specifier: ^11.0.0 - version: 11.0.0 pinia: specifier: ^3.0.4 version: 3.0.4(typescript@5.9.3)(vue@3.5.25(typescript@5.9.3)) @@ -513,10 +510,6 @@ packages: buffer-from@1.1.2: resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} - bundle-name@4.1.0: - resolution: {integrity: sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==} - engines: {node: '>=18'} - bytes@3.1.2: resolution: {integrity: sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==} engines: {node: '>= 0.8'} @@ -574,18 +567,6 @@ packages: supports-color: optional: true - default-browser-id@5.0.1: - resolution: {integrity: sha512-x1VCxdX4t+8wVfd1so/9w+vQ4vx7lKd2Qp5tDRutErwmR85OgmfX7RlLRMWafRMY7hbEiXIbudNrjOAPa/hL8Q==} - engines: {node: '>=18'} - - default-browser@5.4.0: - resolution: {integrity: sha512-XDuvSq38Hr1MdN47EDvYtx3U0MTqpCEn+F6ft8z2vYDzMrvQhVp0ui9oQdqW3MvK3vqUETglt1tVGgjLuJ5izg==} - engines: {node: '>=18'} - - define-lazy-prop@3.0.0: - resolution: {integrity: sha512-N+MeXYoqr3pOgn8xfyRPREN7gHakLYjhsHhWGT3fWAiL4IkAt0iDw14QiiEm2bE30c5XX5q0FtAA3CK5f9/BUg==} - engines: {node: '>=12'} - defu@6.1.4: resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==} @@ -715,20 +696,6 @@ packages: resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==} engines: {node: '>= 0.10'} - is-docker@3.0.0: - resolution: {integrity: sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - hasBin: true - - is-in-ssh@1.0.0: - resolution: {integrity: sha512-jYa6Q9rH90kR1vKB6NM7qqd1mge3Fx4Dhw5TVlK1MUBqhEOuCagrEHMevNuCcbECmXZ0ThXkRm+Ymr51HwEPAw==} - engines: {node: '>=20'} - - is-inside-container@1.0.0: - resolution: {integrity: sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA==} - engines: {node: '>=14.16'} - hasBin: true - is-promise@4.0.0: resolution: {integrity: sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==} @@ -736,10 +703,6 @@ packages: resolution: {integrity: sha512-oG7cgbmg5kLYae2N5IVd3jm2s+vldjxJzK1pcu9LfpGuQ93MQSzo0okvRna+7y5ifrD+20FE8FvjusyGaz14fw==} engines: {node: '>=18'} - is-wsl@3.1.0: - resolution: {integrity: sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==} - engines: {node: '>=16'} - jiti@2.6.1: resolution: {integrity: sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==} hasBin: true @@ -878,10 +841,6 @@ packages: once@1.4.0: resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} - open@11.0.0: - resolution: {integrity: sha512-smsWv2LzFjP03xmvFoJ331ss6h+jixfA4UUV/Bsiyuu4YJPfN+FIQGOIiv4w9/+MoHkfkJ22UIaQWRVFRfH6Vw==} - engines: {node: '>=20'} - parseurl@1.3.3: resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==} engines: {node: '>= 0.8'} @@ -929,10 +888,6 @@ packages: resolution: {integrity: sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==} engines: {node: ^10 || ^12 || >=14} - powershell-utils@0.1.0: - resolution: {integrity: sha512-dM0jVuXJPsDN6DvRpea484tCUaMiXWjuCn++HGTqUWzGDjv5tZkEZldAJ/UMlqRYGFrD/etByo4/xOuC/snX2A==} - engines: {node: '>=20'} - proxy-addr@2.0.7: resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} engines: {node: '>= 0.10'} @@ -1006,10 +961,6 @@ packages: resolution: {integrity: sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==} engines: {node: '>= 18'} - run-applescript@7.1.0: - resolution: {integrity: sha512-DPe5pVFaAsinSaV6QjQ6gdiedWDcRCbUuiQfQa2wmWV7+xC9bGulGI8+TdRmoFkAPaBXk8CrAbnlY2ISniJ47Q==} - engines: {node: '>=18'} - safer-buffer@2.1.2: resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} @@ -1162,10 +1113,6 @@ packages: wrappy@1.0.2: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} - wsl-utils@0.3.0: - resolution: {integrity: sha512-3sFIGLiaDP7rTO4xh3g+b3AzhYDIUGGywE/WsmqzJWDxus5aJXVnPTNC/6L+r2WzrwXqVOdD262OaO+cEyPMSQ==} - engines: {node: '>=20'} - snapshots: '@babel/helper-string-parser@7.27.1': {} @@ -1580,10 +1527,6 @@ snapshots: buffer-from@1.1.2: optional: true - bundle-name@4.1.0: - dependencies: - run-applescript: 7.1.0 - bytes@3.1.2: {} call-bind-apply-helpers@1.0.2: @@ -1625,15 +1568,6 @@ snapshots: dependencies: ms: 2.1.3 - default-browser-id@5.0.1: {} - - default-browser@5.4.0: - dependencies: - bundle-name: 4.1.0 - default-browser-id: 5.0.1 - - define-lazy-prop@3.0.0: {} - defu@6.1.4: {} depd@2.0.0: {} @@ -1776,22 +1710,10 @@ snapshots: ipaddr.js@1.9.1: {} - is-docker@3.0.0: {} - - is-in-ssh@1.0.0: {} - - is-inside-container@1.0.0: - dependencies: - is-docker: 3.0.0 - is-promise@4.0.0: {} is-what@5.5.0: {} - is-wsl@3.1.0: - dependencies: - is-inside-container: 1.0.0 - jiti@2.6.1: {} lightningcss-android-arm64@1.30.2: @@ -1885,15 +1807,6 @@ snapshots: dependencies: wrappy: 1.0.2 - open@11.0.0: - dependencies: - default-browser: 5.4.0 - define-lazy-prop: 3.0.0 - is-in-ssh: 1.0.0 - is-inside-container: 1.0.0 - powershell-utils: 0.1.0 - wsl-utils: 0.3.0 - parseurl@1.3.3: {} path-browserify@1.0.1: {} @@ -1925,8 +1838,6 @@ snapshots: picocolors: 1.1.1 source-map-js: 1.2.1 - powershell-utils@0.1.0: {} - proxy-addr@2.0.7: dependencies: forwarded: 0.2.0 @@ -2009,8 +1920,6 @@ snapshots: transitivePeerDependencies: - supports-color - run-applescript@7.1.0: {} - safer-buffer@2.1.2: {} send@1.2.0: @@ -2156,8 +2065,3 @@ snapshots: typescript: 5.9.3 wrappy@1.0.2: {} - - wsl-utils@0.3.0: - dependencies: - is-wsl: 3.1.0 - powershell-utils: 0.1.0 diff --git a/server.js b/server.js index 968eceb..d81800a 100644 --- a/server.js +++ b/server.js @@ -1,20 +1,23 @@ const express = require('express'); -const path = require('node:path'); -const os = require('node:os'); +const path = require('path'); +const { exec } = require('child_process'); +const os = require('os'); const app = express(); -const HOST = '0.0.0.0'; -app.set('trust proxy', true); -// 指向 Vue 构建后的 docs 目录 -app.use(express.static(path.join(process.cwd(), 'docs'))); +// 1. 跨平台自动打开浏览器函数 +function openUrl(url) { + const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open'; + exec(`${start} "${url}"`); +} -const getLocalIp = () => { +// 2. 获取局域网 IP +function getLocalIp() { const interfaces = os.networkInterfaces(); for (let devName in interfaces) { - let face = interfaces[devName]; - for (let i = 0; i < face.length; i++) { - let alias = face[i]; + let iface = interfaces[devName]; + for (let i = 0; i < iface.length; i++) { + let alias = iface[i]; if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { return alias.address; } @@ -22,23 +25,40 @@ const getLocalIp = () => { } return 'localhost'; } -const server = app.listen(0, HOST, async () => { - const { port } = server.address(); - const url = `http://localhost:${port}`; +// 3. 核心:静态资源拦截器(实现单文件嵌入的关键) +app.get('*', async (req, res) => { + // 处理请求路径,默认为 index.html + let reqPath = req.path === '/' ? '/index.html' : req.path; - console.log('-----------------------------------'); - console.log(`🚀 服务器已成功启动!`); - console.log(`🔗 本地地址: ${url}`); - console.log(`🌐 默认允许局域网访问:http://${getLocalIp()}:${port}`); - console.log('-----------------------------------'); - console.log('提示: 关闭此控制台窗口将停止服务。'); + // 这里的路径必须在构建时能找到对应的 docs 目录 + // Bun 编译时会自动将 Bun.file 引用的静态资源打包进去 + const filePath = path.join(__dirname, "docs", reqPath); + const file = Bun.file(filePath); - // 3. 自动打开浏览器 - try { - const { default: openUrl } = await import('open'); - await openUrl(url); - } catch (err) { - console.error('无法自动打开浏览器:', err); + if (await file.exists()) { + res.type(file.type); + res.send(Buffer.from(await file.arrayBuffer())); + } else { + // Vue History 模式支持:找不到的文件指向 index.html + const indexFile = Bun.file(path.join(__dirname, "docs", "index.html")); + res.type('text/html'); + res.send(Buffer.from(await indexFile.arrayBuffer())); } +}); + +// 4. 启动服务器(随机端口) +const server = app.listen(0, '0.0.0.0', () => { + const { port } = server.address(); + const url = `http://localhost:${port}`; + const lanUrl = `http://${getLocalIp()}:${port}`; + + console.log("-----------------------------------"); + console.log(`🚀 OGame 程序已启动!`); + console.log(`🔗 本地访问: ${url}`); + console.log(`🌐 局域网访问: ${lanUrl}`); + console.log("-----------------------------------"); + console.log("提示: 关闭此窗口将停止服务。"); + + openUrl(url); }); \ No newline at end of file