Skip to content

Commit

Permalink
feat: dump application router json
Browse files Browse the repository at this point in the history
Store on `run/router.json`
  • Loading branch information
fengmk2 committed Nov 29, 2017
1 parent 76ff783 commit 503b69b
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 3 deletions.
34 changes: 32 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

const path = require('path');
const fs = require('fs');
const graceful = require('graceful');
const http = require('http');
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;
const cluster = require('cluster-client');
const { assign } = require('utility');
const eggUtils = require('egg-core').utils;
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
Expand Down Expand Up @@ -116,6 +117,35 @@ class Application extends EggApplication {
return context;
}

/**
* save routers to `run/router.json`
* @private
*/
dumpConfig() {
super.dumpConfig();

// dump routers to router.json
const rundir = this.config.rundir;
const FULLPATH = this.loader.FileLoader.FULLPATH;
try {
const dumpRouterFile = path.join(rundir, 'router.json');
const routers = [];
for (const layer of this.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[FULLPATH] || stack._name || stack.name || 'anonymous'),
});
}
fs.writeFileSync(dumpRouterFile, JSON.stringify(routers, null, 2));
} catch (err) {
this.coreLogger.warn(`dumpConfig router.json error: ${err.message}`);
}
}

/**
* Create an anonymous context, the context isn't request level, so the request is mocked.
* then you can use context level API like `ctx.service`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"delegates": "^1.0.0",
"egg-cluster": "^1.12.5",
"egg-cookies": "^2.2.1",
"egg-core": "^4.1.0",
"egg-core": "^4.2.0",
"egg-development": "^2.0.0",
"egg-i18n": "^2.0.0",
"egg-jsonp": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.logger = {
consoleLevel: 'NONE',
};
2 changes: 2 additions & 0 deletions test/fixtures/apps/demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
module.exports = app => {
app.ready(() => {
app.config.tips = 'hello egg started';
// dynamic router
app.all('/all', app.controller.home);
});
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/demo/app/controller/obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = app => {
return {
* bar() {
this.ctx.body = 'this is obj bar!';
},

* error() {
aaa;
},

subObj: {
* hello() {
this.ctx.body = 'this is subObj hello!';
},
},
};
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/demo/app/controller/obj2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = {
* bar() {
this.ctx.body = 'this is obj bar!';
},

subObj: {
* hello() {
this.ctx.body = 'this is subObj hello!';
},

subSubObj: {
* hello() {
this.ctx.body = 'this is subSubObj hello!';
},
},
},
};
8 changes: 8 additions & 0 deletions test/fixtures/apps/demo/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ module.exports = app => {
app.get('/ip', app.controller.ip);

app.get('/class-controller', 'foo.bar');

app.get('/obj-controller', 'obj.bar');
app.get('/obj-error', 'obj.error');
app.get('/subobj-controller', 'obj.subObj.hello');

app.get('/obj2-controller', app.controller.obj2.bar);
app.get('/subobj2-controller', app.controller.obj2.subObj.hello);
app.get('/subSubObj-hello', app.controller.obj2.subObj.subSubObj.hello);
};
6 changes: 6 additions & 0 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,20 @@ describe('test/lib/core/httpclient.test.js', () => {

let res = await httpclient.request(url, {
method: 'GET',
timeout: 20000,
});
assert(res.status === 200);

res = await httpclient.request('https://github.com', {
method: 'GET',
timeout: 20000,
});

assert(res.status === 200);

res = await httpclient.request('https://www.npmjs.com', {
method: 'GET',
timeout: 20000,
});
assert(res.status === 200);

Expand All @@ -360,16 +363,19 @@ describe('test/lib/core/httpclient.test.js', () => {

res = await httpclient.request(url, {
method: 'GET',
timeout: 20000,
});
assert(res.status === 200);

res = await httpclient.request('https://github.com', {
method: 'GET',
timeout: 20000,
});
assert(res.status === 200);

res = await httpclient.request('https://www.npmjs.com', {
method: 'GET',
timeout: 20000,
});
assert(res.status === 200);

Expand Down
17 changes: 17 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,26 @@ describe('test/lib/egg.test.js', () => {
assert(json.config.tips === 'hello egg');
json = require(path.join(baseDir, 'run/application_config.json'));
assert(/\d+\.\d+\.\d+/.test(json.plugins.onerror.version));
// should dump dynamic config
assert(json.config.tips === 'hello egg started');
});

it('should dump router json', () => {
const routers = require(path.join(baseDir, 'run/router.json'));
// 13 static routers on app/router.js and 1 dynamic router on app.js
assert(routers.length === 14);
for (const router of routers) {
assert.deepEqual(Object.keys(router), [
'name',
'methods',
'paramNames',
'path',
'regexp',
'stack',
]);
}
});

it('should dump config meta', () => {
let json = require(path.join(baseDir, 'run/agent_config_meta.json'));
assert(json.name === path.join(__dirname, '../../config/config.default.js'));
Expand Down

0 comments on commit 503b69b

Please sign in to comment.