Skip to content

Commit

Permalink
fix: faas missing config in framework (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Dec 9, 2021
1 parent bfafbdf commit 7ab16a2
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 18 deletions.
8 changes: 5 additions & 3 deletions packages-serverless/egg-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ module.exports = engine => {
try {
const bootstrap = require(join(baseDir, 'bootstrap'));
eggApp = await bootstrap({
globalConfig: {
default: require('./framework/config/config.default'),
},
globalConfig: [
{
default: require('./framework/config/config.default'),
},
],
});
} catch (e) {
console.error(e);
Expand Down
3 changes: 2 additions & 1 deletion packages-serverless/serverless-worker-starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@midwayjs/decorator": "^3.0.0-beta.8",
"@midwayjs/jest-environment-service-worker": "^0.0.1-alpha.5",
"@midwayjs/jsdom-service-worker": "^0.0.1-alpha.5",
"@midwayjs/runtime-mock": "^3.0.0-beta.8"
"@midwayjs/runtime-mock": "^3.0.0-beta.8",
"@midwayjs/logger": "^3.0.0-beta.8"
},
"engines": {
"node": ">=12"
Expand Down
5 changes: 5 additions & 0 deletions packages-serverless/serverless-worker-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { ServerlessLightRuntime } from '@midwayjs/runtime-engine';
import { Application, HTTPResponse } from '@midwayjs/serverless-http-parser';
import { types } from 'util';
import { HTTPRequest } from './http-request';
import { loggers } from '@midwayjs/logger';

loggers.addLogger('coreLogger', console);
loggers.addLogger('appLogger', console);
loggers.addLogger('logger', console);

const { isAnyArrayBuffer, isArrayBufferView } = types;

Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/common/webRouterCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { joinURLPath } from '../util';
import { MidwayContainer } from '../context/container';
import { DirectoryFileDetector } from './fileDetector';
import * as util from 'util';
import { MidwayCommonError } from '../error';

const debug = util.debuglog('midway:debug');

Expand Down Expand Up @@ -200,6 +201,12 @@ export class WebRouterCollector {
prefix = ignorePrefix;
}

if (/\*/.test(prefix)) {
throw new MidwayCommonError(
`Router prefix ${prefix} can't set string with *`
);
}

// set prefix
if (!this.routes.has(prefix)) {
this.routes.set(prefix, []);
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/context/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ class ContainerConfiguration {
});
}

addImportConfigs(importConfigs: string[]) {
if (importConfigs && importConfigs.length) {
this.container.get(MidwayConfigService).add(importConfigs);
addImportConfigs(
importConfigs:
| Array<{ [environmentName: string]: Record<string, any> }>
| Record<string, any>
) {
if (importConfigs) {
if (Array.isArray(importConfigs)) {
this.container.get(MidwayConfigService).add(importConfigs);
} else {
this.container.get(MidwayConfigService).addObject(importConfigs);
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,7 @@ export interface IMidwayBootstrapOptions {
moduleDetector?: 'file' | IFileDetector | false;
logger?: boolean | ILogger;
ignore?: string[];
globalConfig?: {
[environmentName: string]: Record<string, any>;
}
globalConfig?: Array<{[environmentName: string]: Record<string, any>;}> | Record<string, any>;
}

export interface IConfigurationOptions {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export async function initializeGlobalApplicationContext(
await applicationContext.ready();

if (globalOptions.globalConfig) {
configService.add([globalOptions.globalConfig]);
if (Array.isArray(globalOptions.globalConfig)) {
configService.add(globalOptions.globalConfig);
} else {
configService.addObject(globalOptions.globalConfig);
}
}

// merge config
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,15 @@ describe('/test/baseFramework.test.ts', () => {
expect(await composeMiddleware({})).toEqual('hello world gogogo, zhangting');
});

it('load object config in configuration', async () => {
mm(process.env, 'MIDWAY_SERVER_ENV', '');
const framework = await createLightFramework(path.join(
__dirname,
'./fixtures/app-with-configuration-config-object/src'
));

expect(framework.getConfiguration('bbb')).toEqual(222);
expect(framework.getConfiguration('ccc')).toEqual(333);
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
importConfigs: {
bbb: 222,
ccc: 333,
},
})
class AutoConfiguration {
}

module.exports = AutoConfiguration;
78 changes: 78 additions & 0 deletions packages/core/test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,84 @@ describe('/test/setup.test.ts', () => {
await destroyGlobalApplicationContext(container);
});

it('should test setup global config', async () => {
const baseDir = join(
__dirname,
'./fixtures/base-app-config/src'
);
const container = await initializeGlobalApplicationContext({
baseDir,
configurationModule: [require(join(baseDir, 'configuration'))],
globalConfig: {
ccc: 222
}
});

const configService = await container.getAsync(MidwayConfigService);
const config = configService.getConfiguration();
expect(config).toHaveProperty('hello',
{
'a': 1,
'b': 4,
'c': 3,
'd': [
1,
2,
3
]
});

expect(config).toHaveProperty('plugins',
{
'bucLogin': false
});

expect(config).toHaveProperty('ccc', 222);

await destroyGlobalApplicationContext(container);
});

it('should test setup global config with env', async () => {
const baseDir = join(
__dirname,
'./fixtures/base-app-config/src'
);
const container = await initializeGlobalApplicationContext({
baseDir,
configurationModule: [require(join(baseDir, 'configuration'))],
globalConfig: [
{
default: {
ccc: 333
}
}
]
});

const configService = await container.getAsync(MidwayConfigService);
const config = configService.getConfiguration();
expect(config).toHaveProperty('hello',
{
'a': 1,
'b': 4,
'c': 3,
'd': [
1,
2,
3
]
});

expect(config).toHaveProperty('plugins',
{
'bucLogin': false
});

expect(config).toHaveProperty('ccc', 333);

await destroyGlobalApplicationContext(container);
});

it('should test setup a framework and get from container', async () => {
/**
* 一个全量的空框架
Expand Down
4 changes: 3 additions & 1 deletion packages/decorator/src/decorator/common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface ResolveFilter {
export interface InjectionConfigurationOptions {
imports?: Array<string | IComponentInfo | { Configuration: any }>;
importObjects?: Record<string, unknown>;
importConfigs?: any[];
importConfigs?:
| Array<{ [environmentName: string]: Record<string, any> }>
| Record<string, any>;
namespace?: string;
directoryResolveFilter?: ResolveFilter[];
conflictCheck?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion packages/faas/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class FaaSConfiguration {
async onReady(container) {}

async onServerReady() {
await this.framework.run();
if (!this.framework.isEnable()) {
await this.framework.run();
}
}
}
11 changes: 6 additions & 5 deletions packages/faas/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class MidwayFaaSFramework extends BaseFramework<
public app: IMidwayFaaSApplication;
private isReplaceLogger =
process.env['MIDWAY_SERVERLESS_REPLACE_LOGGER'] === 'true';
private developmentRun = false;

@Inject()
environmentService: MidwayEnvironmentService;
Expand All @@ -48,16 +49,16 @@ export class MidwayFaaSFramework extends BaseFramework<
middlewareService: MidwayMiddlewareService<FaaSContext, any>;

configure(options: IFaaSConfigurationOptions) {
const faasConfig = this.configService.getConfiguration('faas');
if (faasConfig) {
this.configurationOptions = faasConfig;
} else {
if (options) {
this.developmentRun = true;
this.configurationOptions = options;
} else {
return this.configService.getConfiguration('faas');
}
}

isEnable(): boolean {
return false;
return !this.developmentRun;
}

async applicationInitialize(options: IMidwayBootstrapOptions) {
Expand Down

0 comments on commit 7ab16a2

Please sign in to comment.