Skip to content

Commit

Permalink
feat: support serverScope (#120)
Browse files Browse the repository at this point in the history
scope is a deploy unit, you can set different config between scopes.

Example:

Deploy application in us and asia with different config,you can set

- config.us_prod.js
- config.ap_prod.js
  • Loading branch information
popomore authored and fengmk2 committed Oct 20, 2017
1 parent 15f785a commit eedfd3d
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 13 deletions.
30 changes: 30 additions & 0 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class EggLoader {
* @since 1.0.0
*/
this.appInfo = this.getAppInfo();

/**
* @member {String} EggLoader#serverScope
* @see AppInfo#serverScope
*/
this.serverScope = this.getServerScope();
}

/**
Expand Down Expand Up @@ -112,6 +118,15 @@ class EggLoader {
return serverEnv;
}

/**
* Get {@link AppInfo#scope}
* @return {String} serverScope
* @private
*/
getServerScope() {
return process.env.EGG_SERVER_SCOPE || '';
}

/**
* Get {@link AppInfo#name}
* @return {String} appname
Expand Down Expand Up @@ -144,6 +159,7 @@ class EggLoader {
*/
getAppInfo() {
const env = this.serverEnv;
const scope = this.serverScope;
const home = this.getHomedir();
const baseDir = this.options.baseDir;

Expand Down Expand Up @@ -183,6 +199,11 @@ class EggLoader {
*/
env,

/**
* @member {String} AppInfo#scope
*/
scope,

/**
* The use directory, same as `process.env.HOME`
* @member {String} AppInfo#HOME
Expand Down Expand Up @@ -366,6 +387,15 @@ class EggLoader {
return ContextLoader;
}

getTypeFiles(filename) {
const files = [ `${filename}.default.js` ];
if (this.serverScope) files.push(`${filename}.${this.serverScope}.js`);
if (this.serverEnv === 'default') return files;

files.push(`${filename}.${this.serverEnv}.js`);
if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}.js`);
return files;
}
}

/**
Expand Down
7 changes: 1 addition & 6 deletions lib/loader/mixin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ module.exports = {

const target = {};

const names = [
'config.default.js',
`config.${this.serverEnv}.js`,
];

// Load Application config first
const appConfig = this._preloadAppConfig();

Expand All @@ -37,7 +32,7 @@ module.exports = {
// plugin config.{env}
// framework config.{env}
// app config.{env}
for (const filename of names) {
for (const filename of this.getTypeFiles('config')) {
for (const unit of this.getLoadUnits()) {
const isApp = unit.type === 'app';
const config = this._loadConfig(unit.path, filename, isApp ? undefined : appConfig, unit.type);
Expand Down
18 changes: 11 additions & 7 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,21 @@ module.exports = {
configPaths = [ configPaths ];
}

// read plugin.default.js and plugins.${env}.js
if (this.serverEnv !== 'default') {
// note: can't use for-of
for (let i = 0, l = configPaths.length; i < l; i++) {
const configPath = configPaths[i];
configPaths.push(configPath.replace(/plugin\.default\.js$/, `plugin.${this.serverEnv}.js`));
// Get all plugin configurations
// plugin.default.js
// plugin.${scope}.js
// plugin.${env}.js
// plugin.${scope}_${env}.js
const newConfigPaths = [];
for (const filename of this.getTypeFiles('plugin')) {
for (let configPath of configPaths) {
configPath = path.join(path.dirname(configPath), filename);
newConfigPaths.push(configPath);
}
}

const plugins = {};
for (let configPath of configPaths) {
for (let configPath of newConfigPaths) {
// let plugin.js compatible
if (configPath.endsWith('plugin.default.js') && !fs.existsSync(configPath)) {
configPath = configPath.replace(/plugin\.default\.js$/, 'plugin.js');
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/scope-env/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
from: 'default',
};
5 changes: 5 additions & 0 deletions test/fixtures/scope-env/config/config.en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
from: 'en',
};
5 changes: 5 additions & 0 deletions test/fixtures/scope-env/config/config.en_prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
from: 'en_prod',
};
5 changes: 5 additions & 0 deletions test/fixtures/scope-env/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
from: 'prod',
};
12 changes: 12 additions & 0 deletions test/fixtures/scope-env/config/plugin.en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
a: {
enable: false,
package: 'a',
},
b: {
enable: true,
package: 'b',
},
};
9 changes: 9 additions & 0 deletions test/fixtures/scope-env/config/plugin.en_prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
c: false,
d: {
enable: true,
package: 'd',
}
};
8 changes: 8 additions & 0 deletions test/fixtures/scope-env/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
a: {
enable: true,
package: 'a',
},
};
9 changes: 9 additions & 0 deletions test/fixtures/scope-env/config/plugin.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
b: false,
c: {
enable: true,
package: 'c',
},
};
5 changes: 5 additions & 0 deletions test/fixtures/scope-env/node_modules/a/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/scope-env/node_modules/b/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/fixtures/scope-env/node_modules/c/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/scope-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "scope"
}
5 changes: 5 additions & 0 deletions test/fixtures/scope/config/plugin.en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
a: false,
};
8 changes: 8 additions & 0 deletions test/fixtures/scope/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
a: {
enable: true,
package: 'a',
},
};
5 changes: 5 additions & 0 deletions test/fixtures/scope/node_modules/a/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/scope/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "scope"
}
41 changes: 41 additions & 0 deletions test/loader/mixin/load_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

const path = require('path');
const assert = require('assert');
const mm = require('mm');
const utils = require('../../utils');
const Application = require('../../..').EggCore;

describe('test/loader/mixin/load_config.test.js', () => {
let app;
afterEach(() => app.close());
afterEach(mm.restore);

it('should load application config overriding default of egg', () => {
app = utils.createApp('config');
Expand Down Expand Up @@ -144,4 +146,43 @@ describe('test/loader/mixin/load_config.test.js', () => {
assert(!configMeta.urllib.bar);
});

describe('get config with scope', () => {
it('should return without scope when env = default', function* () {
mm(process.env, 'EGG_SERVER_ENV', 'default');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
app.loader.loadConfig();
assert(loader.config.from === 'default');
});

it('should return without scope when env = prod', function* () {
mm(process.env, 'EGG_SERVER_ENV', 'prod');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
app.loader.loadConfig();
assert(loader.config.from === 'prod');
});

it('should return with scope when env = default', function* () {
mm(process.env, 'EGG_SERVER_ENV', 'default');
mm(process.env, 'EGG_SERVER_SCOPE', 'en');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
app.loader.loadConfig();
assert(loader.config.from === 'en');
});

it('should return with scope when env = prod', function* () {
mm(process.env, 'EGG_SERVER_ENV', 'prod');
mm(process.env, 'EGG_SERVER_SCOPE', 'en');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
app.loader.loadConfig();
assert(loader.config.from === 'en_prod');
});
});
});
31 changes: 31 additions & 0 deletions test/loader/mixin/load_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,35 @@ describe('test/load_plugin.test.js', function() {
]);
});

it('should load plugin from scope', () => {
mm(process.env, 'EGG_SERVER_SCOPE', 'en');
app = utils.createApp('scope');
const loader = app.loader;
loader.loadPlugin();
assert(loader.allPlugins.a.enable === false);
});

it('should load plugin from scope and default env', () => {
mm(process.env, 'EGG_SERVER_ENV', 'default');
mm(process.env, 'EGG_SERVER_SCOPE', 'en');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
assert(loader.allPlugins.a.enable === false);
assert(loader.allPlugins.b.enable === true);
assert(!loader.allPlugins.c);
assert(!loader.allPlugins.d);
});

it('should load plugin from scope and prod env', () => {
mm(process.env, 'EGG_SERVER_ENV', 'prod');
mm(process.env, 'EGG_SERVER_SCOPE', 'en');
app = utils.createApp('scope-env');
const loader = app.loader;
loader.loadPlugin();
assert(loader.allPlugins.a.enable === false);
assert(loader.allPlugins.b.enable === false);
assert(loader.allPlugins.c.enable === false);
assert(loader.allPlugins.d.enable === true);
});
});

0 comments on commit eedfd3d

Please sign in to comment.