Skip to content

Commit

Permalink
refactor: add router dump config (#454)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4e45061)
  • Loading branch information
czy88840616 committed Apr 10, 2020
1 parent 54c35ea commit d4f48c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/midway-web/src/loader/webLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MidwayWebLoader extends EggLoader {
public appDir: string;
public appInfo: EggAppInfo;
private controllerIds: string[] = [];
private prioritySortRouters: Array<{
public prioritySortRouters: Array<{
priority: number,
router: Router,
}> = [];
Expand Down
24 changes: 23 additions & 1 deletion packages/midway-web/src/midway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,36 @@ class MidwayApplication extends (Application as {

dumpConfig() {
super.dumpConfig();
const rundir = this.config.rundir;
try {
const tree = this.loader.dumpDependency();
const rundir = this.config.rundir;
const dumpFile = path.join(rundir, `${this.type}_dependency_${process.pid}`);
fs.writeFileSync(dumpFile, tree);
} catch (err) {
this.coreLogger.warn(`dump dependency dot error: ${err.message}`);
}

// dump routers to router.json
try {
const dumpRouterFile = path.join(rundir, 'midway-router.json');
const routers = [];
for (const router of this.loader.prioritySortRouters) {
for (const layer of router['router'].stack) {
routers.push({
name: layer.name,
methods: layer.methods,
paramNames: layer.paramNames,
path: layer.path,
regexp: layer.regexp.toString(),
stack: layer.stack.map(stack => stack._name || stack.name || 'anonymous'),
});
}
}

fs.writeFileSync(dumpRouterFile, JSON.stringify(routers, null, 2));
} catch (err) {
this.coreLogger.warn(`dumpConfig midway-router.json error: ${err.message}`);
}
}
}

Expand Down

0 comments on commit d4f48c7

Please sign in to comment.