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

fix: add missing framework support for single process mode #3445

Merged
merged 1 commit into from
Feb 4, 2019
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
22 changes: 20 additions & 2 deletions lib/start.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

const detectPort = require('detect-port');
const Application = require('./application');
const Agent = require('./agent');
const path = require('path');

module.exports = async (options = {}) => {
console.warn('single process mode is still in experiment, please don\'t use it in production environment');
Expand All @@ -11,6 +10,25 @@ module.exports = async (options = {}) => {
options.mode = 'single';
// FIXME: cluster-client support single process mode
options.clusterPort = await detectPort();

// get agent from options.framework and package.egg.framework
if (!options.framework) {
try {
options.framework = require(path.join(options.baseDir, 'package.json')).egg.framework;
} catch (_) {
// ignore
}
}
let Agent;
let Application;
if (options.framework) {
Agent = require(options.framework).Agent;
Application = require(options.framework).Application;
} else {
Application = require('./application');
Agent = require('./agent');
}

const agent = new Agent(Object.assign({}, options));
await agent.ready();
const application = new Application(Object.assign({}, options));
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/apps/custom-framework-demo/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);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = app => {
return class Foo extends app.Controller {
* bar() {
this.ctx.body = 'this is bar!';
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function*() {
this.cookies.set('hi', 'foo');
this.body = 'hello';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function* () {
this.body = {
workerTitle: process.title
};
};
10 changes: 10 additions & 0 deletions test/fixtures/apps/custom-framework-demo/app/controller/ip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function* () {
if (this.query.set_ip) {
this.ip = this.query.set_ip;
}
this.body = {
ip: this.ip,
};
};
17 changes: 17 additions & 0 deletions test/fixtures/apps/custom-framework-demo/app/controller/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = function*() {
const message = this.query.message;

this.logger.debug('debug %s', message);
this.logger.info('info %s', message);
this.logger.warn('warn %s', message);
this.logger.error(new Error('error ' + message));

this.coreLogger.debug('core debug %s', message);
this.coreLogger.info('core info %s', message);
this.coreLogger.warn('core warn %s', message);
this.coreLogger.error(new Error('core error ' + message));

this.body = 'logger';
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/custom-framework-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/custom-framework-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!';
},
},
},
};
23 changes: 23 additions & 0 deletions test/fixtures/apps/custom-framework-demo/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = app => {
app.get('home', '/', 'home');
app.get('/hello', app.controller.hello);
app.get('/logger', app.controller.logger);
app.get('/protocol', function*() {
this.body = this.protocol;
});

app.get('/user.json', app.jsonp(), function*() {
this.body = { name: 'fengmk2' };
});
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);
};
22 changes: 22 additions & 0 deletions test/fixtures/apps/custom-framework-demo/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
exports.keys = 'foo';
exports.proxy = true;
exports.buffer = Buffer.from('test');

exports.pass = 'this is pass';
exports.pwd = 'this is pwd';
exports.password = 'this is password';
exports.passwordNew = 'this is passwordNew';
exports.mysql = {
passd: 'this is passd',
passwd: 'this is passwd',
secret: 'secret 123',
secretNumber: 123,
secretBoolean: true,
masterKey: 'this is masterKey',
accessKey: 'this is accessKey',
accessId: 'this is accessId',
consumerSecret: 'this is consumerSecret',
someSecret: null,
};

exports.tips = 'hello egg';
6 changes: 6 additions & 0 deletions test/fixtures/apps/custom-framework-demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const egg = require('../../../../');

egg.start().then(app => {
app.listen(3000);
console.log('listen 3000');
});
6 changes: 6 additions & 0 deletions test/fixtures/apps/custom-framework-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "custom-framework-demo",
"egg": {
"framework": "../test/fixtures/custom-egg"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/custom-egg/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
'use strict';

module.exports = require('../../../index');
module.exports.Application = class Application extends module.exports.Application {
constructor(...args) {
super(...args);
this.customEgg = true;
}

get [Symbol.for('egg#eggPath')]() {
return __dirname;
}
};
47 changes: 40 additions & 7 deletions test/lib/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ const utils = require('../utils');
const assert = require('assert');
const path = require('path');

let app;

describe('test/lib/start.test.js', () => {
afterEach(() => app.close());

describe('start', () => {
const baseDir = utils.getFilepath('apps/demo');
let app;
before(async () => {
it('should dump config and plugins', async () => {
app = await utils.singleProcessApp('apps/demo');
});
after(() => app.close());

it('should dump config and plugins', () => {
const baseDir = utils.getFilepath('apps/demo');
let json = require(path.join(baseDir, 'run/agent_config.json'));
assert(/\d+\.\d+\.\d+/.test(json.plugins.onerror.version));
assert(json.config.name === 'demo');
Expand All @@ -33,6 +32,40 @@ describe('test/lib/start.test.js', () => {
});

it('should request work', async () => {
app = await utils.singleProcessApp('apps/demo');
await app.httpRequest().get('/protocol')
.expect(200)
.expect('http');

await app.httpRequest().get('/class-controller')
.expect(200)
.expect('this is bar!');
});

it('should env work', async () => {
app = await utils.singleProcessApp('apps/demo', { env: 'prod' });
assert(app.config.env === 'prod');
});
});

describe('custom framework work', () => {
it('should work with options.framework', async () => {
app = await utils.singleProcessApp('apps/demo', { framework: path.join(__dirname, '../fixtures/custom-egg') });
assert(app.customEgg);

await app.httpRequest().get('/protocol')
.expect(200)
.expect('http');

await app.httpRequest().get('/class-controller')
.expect(200)
.expect('this is bar!');
});

it('should work with package.egg.framework', async () => {
app = await utils.singleProcessApp('apps/custom-framework-demo');
assert(app.customEgg);

await app.httpRequest().get('/protocol')
.expect(200)
.expect('http');
Expand Down