Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export context base classes on Application #737

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict';

const delegate = require('delegates');
const ContextLogger = require('egg-logger').EggContextLogger;
const Cookies = require('egg-cookies');
const co = require('co');
const ContextHttpClient = require('../../lib/core/context_httpclient');
const { assign } = require('utility');


const HELPER = Symbol('Context#helper');
const LOCALS = Symbol('Context#locals');
const LOCALS_LIST = Symbol('Context#localsList');
Expand All @@ -18,7 +14,7 @@ const CONTEXT_HTTPCLIENT = Symbol('Context#httpclient');
const proto = module.exports = {
get cookies() {
if (!this[COOKIES]) {
this[COOKIES] = new Cookies(this, this.app.keys);
this[COOKIES] = new this.app.ContextCookies(this, this.app.keys);
}
return this[COOKIES];
},
Expand All @@ -30,7 +26,7 @@ const proto = module.exports = {
*/
get httpclient() {
if (!this[CONTEXT_HTTPCLIENT]) {
this[CONTEXT_HTTPCLIENT] = new ContextHttpClient(this);
this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this);
}
return this[CONTEXT_HTTPCLIENT];
},
Expand Down Expand Up @@ -96,7 +92,7 @@ const proto = module.exports = {
if (!appLogger) return null;

// write to cache
cache[name] = new ContextLogger(this, appLogger);
cache[name] = new this.app.ContextLogger(this, appLogger);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方便上层应用和框架覆盖默认的这些 extends

return cache[name];
},

Expand Down
8 changes: 8 additions & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const fs = require('fs');
const EggCore = require('egg-core').EggCore;
const cluster = require('cluster-client');
const extend = require('extend2');
const ContextLogger = require('egg-logger').EggContextLogger;
const ContextCookies = require('egg-cookies');
const ContextHttpClient = require('./core/context_httpclient');
const Messenger = require('./core/messenger');
const createHttpClient = require('./core/httpclient');
const createLoggers = require('./core/logger');
Expand Down Expand Up @@ -34,6 +37,11 @@ class EggApplication extends EggCore {
constructor(options) {
super(options);

// export context base classes, let framework can impl sub class and over context extend easily.
this.ContextCookies = ContextCookies;
this.ContextLogger = ContextLogger;
this.ContextHttpClient = ContextHttpClient;

this.loader.loadConfig();

/**
Expand Down
15 changes: 15 additions & 0 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('test/app/extend/context.test.js', () => {
});
});

describe('app or framework can override ctx.getLogger', () => {
let app;
before(() => {
app = utils.app('apps/custom-context-getlogger');
return app.ready();
});
after(() => app.close());

it('should log with custom logger', () => {
return request(app.callback())
.get('/')
.expect('work, logger: exists');
});
});

describe('properties', () => {
let app;
before(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function* () {
const logger = this.getLogger('foo');
logger.info('hello');
this.body = 'work, logger: ' + (logger ? 'exists' : 'not exists');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
getLogger(name) {
console.log('get custom %s logger', name);
return new this.app.ContextLogger(this, console);
},
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/custom-context-getlogger/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');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/custom-context-getlogger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-context-getlogger"
}