mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
framework: build routeMap from koa router stack
This commit is contained in:
1 file changed
+18
-3
@@ -355,7 +355,6 @@ export class WebService extends Service<never> {
|
|||||||
|
|
||||||
private registry: Record<string, any> = Object.create(null);
|
private registry: Record<string, any> = Object.create(null);
|
||||||
private registrationCount = Counter();
|
private registrationCount = Counter();
|
||||||
public routeMap: Record<string, string> = Object.create(null);
|
|
||||||
private serverLayers: LayerEntry[] = [];
|
private serverLayers: LayerEntry[] = [];
|
||||||
private handlerLayers: LayerEntry[] = [];
|
private handlerLayers: LayerEntry[] = [];
|
||||||
private wsLayers: LayerEntry[] = [];
|
private wsLayers: LayerEntry[] = [];
|
||||||
@@ -367,6 +366,22 @@ export class WebService extends Service<never> {
|
|||||||
server = koa;
|
server = koa;
|
||||||
router = router;
|
router = router;
|
||||||
HandlerCommon = HandlerCommon;
|
HandlerCommon = HandlerCommon;
|
||||||
|
private _routeMap: Record<string, string> = Object.create(null);
|
||||||
|
|
||||||
|
get routeMap(): Record<string, string> {
|
||||||
|
return this._routeMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private rebuildRouteMap() {
|
||||||
|
const map: Record<string, string> = Object.create(null);
|
||||||
|
for (const layer of this.router.stack) {
|
||||||
|
if (layer.name && typeof layer.path === 'string') {
|
||||||
|
map[layer.name] = layer.path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._routeMap = map;
|
||||||
|
}
|
||||||
|
|
||||||
Handler = Handler;
|
Handler = Handler;
|
||||||
ConnectionHandler = ConnectionHandler;
|
ConnectionHandler = ConnectionHandler;
|
||||||
|
|
||||||
@@ -724,7 +739,6 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
}
|
}
|
||||||
this.registry[name] = HandlerClass;
|
this.registry[name] = HandlerClass;
|
||||||
this.registrationCount[name]++;
|
this.registrationCount[name]++;
|
||||||
this.routeMap[routeName] = path as string;
|
|
||||||
|
|
||||||
const Checker = (args) => {
|
const Checker = (args) => {
|
||||||
let perm: bigint;
|
let perm: bigint;
|
||||||
@@ -772,13 +786,14 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const dispose = router.disposeLastOp;
|
const dispose = router.disposeLastOp;
|
||||||
|
this.rebuildRouteMap();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.ctx.parallel(`handler/register/${name}`, HandlerClass);
|
this.ctx.parallel(`handler/register/${name}`, HandlerClass);
|
||||||
this.ctx.effect(() => () => {
|
this.ctx.effect(() => () => {
|
||||||
this.registrationCount[name]--;
|
this.registrationCount[name]--;
|
||||||
if (!this.registrationCount[name]) delete this.registry[name];
|
if (!this.registrationCount[name]) delete this.registry[name];
|
||||||
delete this.routeMap[routeName];
|
|
||||||
dispose();
|
dispose();
|
||||||
|
this.rebuildRouteMap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user