init: Wardrobe 衣物管理系统 - 项目首页、OTA 升级、Android CI/CD
- 响应式项目首页 (Landing, 含 R2 APK 下载) + 路由 /home 调整 - OTA 升级: tauri-plugin-hotswap (lib.rs/main.rs 拆分, 客户端热替换) - OTA 服务端 (check/bundle/manifest-web) + build-ota/genkey/upload 脚本 - 密钥/凭据统一存配置中心 (signing + ota + cloudflare 分组) - Android CI (android-* 分支) + OTA CI (手动触发), 产物上传 R2 - 修复 tsc/构建阻塞问题
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import 'dotenv/config'
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import otaRouter from './routes/ota.js'
|
||||
|
||||
// 默认 3002: 与 iboard (3001) 同机部署时不冲突
|
||||
const PORT = Number(process.env.PORT) || 3002
|
||||
const DIST_DIR = path.join(process.cwd(), 'dist')
|
||||
const HAS_DIST = fs.existsSync(DIST_DIR)
|
||||
|
||||
const app = express()
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
|
||||
// 健康检查
|
||||
app.get('/api/health', (_req, res) => res.json({ ok: true, ts: Date.now() }))
|
||||
|
||||
// OTA 接口公开 (检查/下载更新不需要登录), 必须挂载在最前面
|
||||
app.use('/api', otaRouter)
|
||||
|
||||
// 静态资源 (Vite build 产物)
|
||||
if (HAS_DIST) {
|
||||
app.use(express.static(DIST_DIR, { index: false, maxAge: '1h', etag: true }))
|
||||
|
||||
// SPA fallback: 非 /api/* 的请求一律返回 index.html, 由前端路由处理
|
||||
app.get(/^\/(?!api\/).*/, (_req, res, next) => {
|
||||
const indexHtml = path.join(DIST_DIR, 'index.html')
|
||||
if (!fs.existsSync(indexHtml)) return next()
|
||||
res.sendFile(indexHtml)
|
||||
})
|
||||
}
|
||||
|
||||
// 统一错误处理
|
||||
app.use((err, _req, res, _next) => {
|
||||
console.error('[api error]', err)
|
||||
res.status(500).json({ error: err.message || '服务器内部错误' })
|
||||
})
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`[wardrobe-api] listening on http://127.0.0.1:${PORT}`)
|
||||
console.log(`[wardrobe-api] static dist: ${HAS_DIST ? DIST_DIR : '(not built, API only)'}`)
|
||||
})
|
||||
@@ -0,0 +1,100 @@
|
||||
// OTA 升级接口 (兼容 tauri-plugin-hotswap 服务端约定)
|
||||
// GET /api/ota/check/:current_sequence
|
||||
// - 客户端传当前 sequence, 服务端判断是否有更新
|
||||
// - 有: 200 + manifest JSON
|
||||
// - 无: 204 No Content
|
||||
// GET /api/ota/bundle/:filename
|
||||
// - 下载前端 bundle (tar.gz), 供 manifest.url 引用
|
||||
// GET /api/ota/manifest-web
|
||||
// - 给 Web 浏览器用的简化版 manifest (无签名, 仅展示用)
|
||||
//
|
||||
// 数据源: 环境变量 OTA_PACKAGE_DIR 指向的目录, 里面要放:
|
||||
// - latest.json (由 scripts/build-ota.mjs 生成)
|
||||
// { sequence, version, min_binary_version, signature, notes, bundle, mandatory, pub_date, bundle_size }
|
||||
// - <filename> (tar.gz 升级包)
|
||||
import { Router } from 'express'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
const router = Router()
|
||||
|
||||
function resolveOtaDir() {
|
||||
const dir = process.env.OTA_PACKAGE_DIR
|
||||
? path.resolve(process.env.OTA_PACKAGE_DIR)
|
||||
: path.resolve(process.cwd(), 'ota-package')
|
||||
return dir
|
||||
}
|
||||
|
||||
function readLatest() {
|
||||
const p = path.join(resolveOtaDir(), 'latest.json')
|
||||
if (!fs.existsSync(p)) return null
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(p, 'utf8'))
|
||||
} catch (e) {
|
||||
console.error('[ota] latest.json 解析失败:', e.message)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/ota/check/:current_sequence
|
||||
// 客户端实际请求 URL 是 endpoint 中 {{current_sequence}} 替换后的形式
|
||||
router.get('/ota/check/:current_sequence', (req, res) => {
|
||||
const currentSeq = Number(req.params.current_sequence)
|
||||
if (!Number.isFinite(currentSeq) || currentSeq < 0) {
|
||||
return res.status(400).json({ error: '无效的 sequence' })
|
||||
}
|
||||
const latest = readLatest()
|
||||
if (!latest) {
|
||||
// 无可用包, 按约定返回 204
|
||||
return res.status(204).end()
|
||||
}
|
||||
// 不新于当前则 204
|
||||
if (typeof latest.sequence === 'number' && latest.sequence <= currentSeq) {
|
||||
return res.status(204).end()
|
||||
}
|
||||
const dir = resolveOtaDir()
|
||||
const base = process.env.OTA_PUBLIC_BASE || ''
|
||||
res.json({
|
||||
version: latest.version,
|
||||
sequence: latest.sequence,
|
||||
min_binary_version: latest.min_binary_version || '0.0.0',
|
||||
url: (base ? base : '') + '/api/ota/bundle/' + encodeURIComponent(latest.bundle),
|
||||
signature: latest.signature,
|
||||
notes: latest.notes || '',
|
||||
pub_date: latest.pub_date || new Date().toISOString(),
|
||||
mandatory: !!latest.mandatory,
|
||||
bundle_size: latest.bundle_size || 0,
|
||||
})
|
||||
})
|
||||
|
||||
// GET /api/ota/bundle/:filename - 流式下载 bundle
|
||||
router.get('/ota/bundle/:filename', (req, res) => {
|
||||
const dir = resolveOtaDir()
|
||||
// 防越界: 只取 basename
|
||||
const name = path.basename(req.params.filename)
|
||||
const p = path.join(dir, name)
|
||||
if (!fs.existsSync(p)) return res.status(404).json({ error: 'bundle 不存在' })
|
||||
const stat = fs.statSync(p)
|
||||
res.setHeader('Content-Type', 'application/gzip')
|
||||
res.setHeader('Content-Length', stat.size)
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="' + name + '"')
|
||||
fs.createReadStream(p).pipe(res)
|
||||
})
|
||||
|
||||
// GET /api/ota/manifest-web - Web 浏览器用的简化版
|
||||
// 客户端会拿这个跟当前 build_id 比较, 有新版本就提示用户刷新
|
||||
router.get('/ota/manifest-web', (_req, res) => {
|
||||
const latest = readLatest()
|
||||
if (!latest) return res.json({ available: false })
|
||||
res.json({
|
||||
available: true,
|
||||
version: latest.version,
|
||||
build_id: latest.build_id || latest.version,
|
||||
note: latest.notes || '',
|
||||
size: latest.bundle_size || 0,
|
||||
})
|
||||
})
|
||||
|
||||
// GET /api/ota/info 已删除: 诊断接口会暴露服务器路径与签名, 且无调用方
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user