v0.15.0: Docker 化部署(OVH-US-BAK)
- Dockerfile(oven/bun 多阶段:构建 dist + 全量 node_modules 运行时) - deploy/docker-compose.yml(postgres:18 + app,uploads 双挂载同宿主目录) - .dockerignore(uploads 经卷挂载不进镜像,避免 3GB 膨胀) - prod-server /uploads/* 运行时直读磁盘(媒体上传后无需重启即可访问,含路径穿越防护) - README Docker 部署章节
This commit is contained in:
+16
-1
@@ -9,11 +9,15 @@
|
||||
// @ts-expect-error 构建产物无 d.ts
|
||||
import serverEntry from "../dist/server/server.js";
|
||||
import { readdirSync } from "node:fs";
|
||||
import { resolve, sep } from "node:path";
|
||||
|
||||
/** # 成员变量 dist/client 静态文件表(pathname → 绝对路径,启动时递归扫描一次) **/
|
||||
const CLIENT_DIR = new URL("../dist/client/", import.meta.url).pathname;
|
||||
const staticFiles = new Map<string, string>();
|
||||
|
||||
/** # 成员变量 uploads 根目录(媒体上传写入 public/uploads,运行时直读,启动静态表不含新增文件) **/
|
||||
const UPLOADS_ROOT = resolve(process.cwd(), "public", "uploads");
|
||||
|
||||
/** # 函数 递归收集 dist/client 下全部静态文件 */
|
||||
function walk(dir: string): void {
|
||||
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
||||
@@ -28,9 +32,20 @@ walk(CLIENT_DIR);
|
||||
export default {
|
||||
/** # 成员变量 监听端口(PORT 环境变量可覆盖) **/
|
||||
port: Number(process.env.PORT ?? 3312),
|
||||
/** # 函数 请求入口:静态文件优先,其余交给 Start handler */
|
||||
/** # 函数 请求入口:/uploads 直读磁盘(新增文件无需重启)→ 启动静态表 → Start handler */
|
||||
async fetch(req: Request): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
// /uploads/ 运行时直读磁盘:媒体库上传落在 public/uploads,静态表启动时已定格,
|
||||
// 不直读会导致运行中上传的图片 404。resolve 后校验前缀,防路径穿越。
|
||||
if (url.pathname.startsWith("/uploads/")) {
|
||||
const abs = resolve(UPLOADS_ROOT, "." + url.pathname.slice("/uploads".length));
|
||||
if (abs.startsWith(UPLOADS_ROOT + sep)) {
|
||||
const uf = Bun.file(abs);
|
||||
if (await uf.exists()) {
|
||||
return new Response(uf, { headers: { "cache-control": "public, max-age=3600" } });
|
||||
}
|
||||
}
|
||||
}
|
||||
// 尾斜杠归一化:偶发的 "<file>.jpg/" 形态请求(SPA 预取取回/上游转发)直接按静态
|
||||
// 文件应答,避免交给 Start handler 后落入路由 catch-all 返回 404
|
||||
let f = staticFiles.get(url.pathname);
|
||||
|
||||
Reference in New Issue
Block a user