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 0854c44 commit 710defc
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 4 deletions.
34 changes: 32 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'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 EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
Expand Down Expand Up @@ -115,6 +116,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 @@ -24,7 +24,7 @@
"delegates": "^1.0.0",
"egg-cluster": "^1.12.4",
"egg-cookies": "^2.2.1",
"egg-core": "^3.18.0",
"egg-core": "^3.19.1",
"egg-development": "^1.3.2",
"egg-i18n": "^1.2.0",
"egg-jsonp": "^1.2.1",
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,18 +327,21 @@ describe('test/lib/core/httpclient.test.js', () => {

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


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

assert(res.status === 200);

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

Expand All @@ -361,17 +364,20 @@ describe('test/lib/core/httpclient.test.js', () => {

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


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

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

Expand Down
18 changes: 17 additions & 1 deletion 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 Expand Up @@ -219,7 +236,6 @@ describe('test/lib/egg.test.js', () => {
after(() => app.close());

it('should access base context properties success', function* () {
mm(app.config.logger, 'level', 'DEBUG');
yield app.httpRequest()
.get('/')
.expect('hello')
Expand Down

0 comments on commit 710defc

Please sign in to comment.