路由表化:迁移登录/会话、报表/备份/同步路由,删除旧路由链,server.js 收为入口
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5c015f826
commit
2305900178
8 files changed
+632
-493
No files matched your search
@@ -13,8 +13,14 @@
|
||||
// 未匹配的 /api/、/print/ 请求:GET 直接 404;POST 照常读请求体、复核会话、进入 transact 后再 404,
|
||||
// 因此请求体过大/非 JSON、会话失效、操作号无效等错误仍先于 404 返回。
|
||||
const { fail, owner } = require('../ledger');
|
||||
const { createRouter } = require('./router');
|
||||
const { send, notFound } = require('./http');
|
||||
const { readBody } = require('./middleware/body');
|
||||
const { securityHeaders } = require('./middleware/security-headers');
|
||||
const { checkRequest } = require('./middleware/request-guard');
|
||||
const { requireActor } = require('./middleware/session');
|
||||
const { serveStatic } = require('./middleware/static');
|
||||
const { sendError } = require('./middleware/errors');
|
||||
|
||||
const UNMATCHED = {
|
||||
GET: { method: 'GET', handler: notFound },
|
||||
@@ -48,4 +54,40 @@ async function runRoute(route, ctx) {
|
||||
send(res, 200, route.project ? route.project(ctx, result) : result);
|
||||
}
|
||||
|
||||
module.exports = { runRoute, UNMATCHED };
|
||||
// 处理顺序:安全响应头 → Host/405/来源校验 → 登录前路由 → 解析当前账号 → /api/、/print/ 的 401/409
|
||||
// → 路由(含未匹配)→ 静态文件 → 统一错误响应。
|
||||
function createHandler(app, routes) {
|
||||
const router = createRouter(routes);
|
||||
const enabled = route => !route.when || route.when(app);
|
||||
return async (req, res) => {
|
||||
securityHeaders(res);
|
||||
try {
|
||||
const { url, route: pathname, isPost } = checkRequest(req, app.production);
|
||||
const hit = router.match(req.method, pathname, enabled);
|
||||
const ctx = {
|
||||
req,
|
||||
res,
|
||||
url,
|
||||
params: hit?.params ?? {},
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
body: undefined,
|
||||
actor: null,
|
||||
ledger: null,
|
||||
rawLedger: app.ledger,
|
||||
app,
|
||||
};
|
||||
if (hit?.route.auth === 'public') return await runRoute(hit.route, ctx);
|
||||
ctx.actor = app.user(req);
|
||||
ctx.ledger = app.ledger.asActor(ctx.actor);
|
||||
if (pathname.startsWith('/api/') || pathname.startsWith('/print/')) {
|
||||
requireActor(req, ctx.actor);
|
||||
return await runRoute(hit?.route ?? UNMATCHED[req.method], ctx);
|
||||
}
|
||||
serveStatic(res, pathname, isPost);
|
||||
} catch (e) {
|
||||
sendError(res, e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { createHandler, runRoute, UNMATCHED };
|
||||
Reference in new issue
Block a user