core: fix 404 page display

This commit is contained in:
undefined committed 2025-05-07 21:37:52 +08:00
1 parent 83da1c8d66
commit cd56ffe55b
11 files changed
+48 -30

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@
"sanitize-filename": "^1.6.3",
"saslprep": "^1.0.3",
"shorty.js": "^1.0.1",
"ws": "^8.18.1"
"ws": "^8.18.2"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
+17 -1
View File
@@ -343,6 +343,7 @@ export class WebService<C extends CordisContext = CordisContext> extends Service
private handlerLayers = [];
private wsLayers = [];
private captureAllRoutes = Object.create(null);
private customDefaultContext: C;
renderers: Record<string, Renderer> = Object.create(null);
server = koa;
@@ -449,7 +450,7 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
...this.handlerLayers,
{
name: '404',
func: (t) => this.handleHttp(t, NotFoundHandler, () => true, this.ctx),
func: (t) => this.handleHttp(t, NotFoundHandler, () => true, this.customDefaultContext || this.ctx),
},
]));
this.addLayer('base', base(logger, this.config.xff, this.config.xhost));
@@ -736,6 +737,21 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
});
}
public setDefaultContext(ctx: C) {
try {
if (!ctx.server) throw new Error();
} catch (e) {
throw new Error('Must provide a valid context with server.');
}
this.ctx.effect(async () => {
if (this.customDefaultContext) logger.warn('Default context already set.');
this.customDefaultContext = ctx;
return () => {
this.customDefaultContext = null;
};
});
}
public withHandlerClass<T extends string>(
name: T, callback: (HandlerClass: T extends `${string}ConnectionHandler` ? typeof ConnectionHandler<C> : typeof Handler<C>) => any,
) {