Skip to content

Commit

Permalink
feat: enable request started log on meta middleware (eggjs#1877)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and dead-horse committed Dec 26, 2017
1 parent 8ce9611 commit 9fe5b85
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 13 deletions.
5 changes: 4 additions & 1 deletion app/middleware/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

'use strict';

module.exports = () => {
module.exports = options => {
return async function meta(ctx, next) {
if (options.logging) {
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s', ctx.host, ctx.header['user-agent']);
}
await next();
// total response time header
ctx.set('x-readtime', Date.now() - ctx.starttime);
Expand Down
12 changes: 12 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@ module.exports = appInfo => {
},
};

/**
* The option of `meta` middleware
*
* @member Config#meta
* @property {Boolean} enable - enable meta or not, default is true
* @property {Boolean} logging - enable logging start request, default is false
*/
config.meta = {
enable: true,
logging: false,
};

/**
* core enable middlewares
* @member {Array} Config#middleware
Expand Down
46 changes: 34 additions & 12 deletions test/app/middleware/meta.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
'use strict';

const assert = require('assert');
const mm = require('egg-mock');
const fs = require('mz/fs');
const utils = require('../../utils');

describe('test/app/middleware/meta.test.js', () => {
let app;
before(() => {
app = utils.app('apps/middlewares');
return app.ready();
});
afterEach(mm.restore);

after(() => app.close());
describe('default config', () => {
let app;
before(() => {
app = utils.app('apps/middlewares');
return app.ready();
});
after(() => app.close());

afterEach(mm.restore);
it('should get X-Readtime header', () => {
return app.httpRequest()
.get('/')
.expect('X-Readtime', /\d+/)
.expect(200);
});
});

describe('meta.logging = true', () => {
let app;
before(() => {
app = utils.app('apps/meta-logging-app');
return app.ready();
});
after(() => app.close());

it('should get X-Readtime header', () => {
return app.httpRequest()
.get('/')
.expect('X-Readtime', /\d+/)
.expect(200);
it('should get X-Readtime header', async () => {
await app.httpRequest()
.get('/?foo=bar')
.expect('X-Readtime', /\d+/)
.expect('hello world')
.expect(200);
const content = (await fs.readFile(app.coreLogger.options.file, 'utf8')).split('\n').slice(-2, -1)[0];
assert(content.includes('[meta] request started, host: '));
});
});
});
9 changes: 9 additions & 0 deletions test/fixtures/apps/meta-logging-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = app => {
app.ready(() => {
app.config.tips = 'hello egg started';
// dynamic router
app.all('/all', app.controller.home);
});
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/meta-logging-app/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = async ctx => {
ctx.body = 'hello world';
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/meta-logging-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.get('home', '/', 'home');
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/meta-logging-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.keys = 'foo';

exports.meta = {
logging: true,
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/meta-logging-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "meta-logging-app"
}

0 comments on commit 9fe5b85

Please sign in to comment.