mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-19 06:38:55 +08:00
fix: error handler and debug info
This commit is contained in:
@@ -1,43 +1,26 @@
|
||||
import { MiddlewareHandler } from "hono";
|
||||
import { getRouteNameFromPath } from '@/utils/helpers';
|
||||
|
||||
const debug = {
|
||||
hitCache: 0,
|
||||
request: 0,
|
||||
etag: 0,
|
||||
paths: [],
|
||||
routes: [],
|
||||
errorPaths: [],
|
||||
errorRoutes: [],
|
||||
}
|
||||
import { getDebugInfo, setDebugInfo } from "@/utils/debug-info";
|
||||
|
||||
const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
if (!debug.paths[ctx.req.path]) {
|
||||
debug.paths[ctx.req.path] = 0;
|
||||
{
|
||||
const debug = getDebugInfo();
|
||||
debug.request++;
|
||||
setDebugInfo(debug);
|
||||
}
|
||||
debug.paths[ctx.req.path]++;
|
||||
|
||||
debug.request++;
|
||||
|
||||
await next();
|
||||
|
||||
const routeName = getRouteNameFromPath(ctx.req.path);
|
||||
if (routeName) {
|
||||
if (!debug.routes[routeName]) {
|
||||
debug.routes[routeName] = 0;
|
||||
{
|
||||
const debug = getDebugInfo();
|
||||
if (ctx.res.headers.get('X-Koa-Redis-Cache') || ctx.res.headers.get('X-Koa-Memory-Cache')) {
|
||||
debug.hitCache++;
|
||||
}
|
||||
debug.routes[routeName]++;
|
||||
}
|
||||
|
||||
if (ctx.res.headers.get('X-Koa-Redis-Cache') || ctx.res.headers.get('X-Koa-Memory-Cache')) {
|
||||
debug.hitCache++;
|
||||
}
|
||||
|
||||
if (ctx.res.status === 304) {
|
||||
debug.etag++;
|
||||
if (ctx.res.status === 304) {
|
||||
debug.etag++;
|
||||
}
|
||||
setDebugInfo(debug);
|
||||
}
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
|
||||
export const getDebugInfo = () => debug
|
||||
|
||||
Reference in New Issue
Block a user