mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 01:38:19 +08:00
core: fix 404 page display
This commit is contained in:
11 files changed
+48
-30
No files matched your search
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user