Skip to content

Commit

Permalink
fix: fix requestContext load configService
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 4, 2020
1 parent 753cfb4 commit f2c874f
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 12 deletions.
24 changes: 15 additions & 9 deletions packages/midway-core/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export class MidwayContainer extends Container implements IMidwayContainer {

init(): void {
this.handlerMap = new Map();
this.environmentService = new MidwayEnvironmentService();
this.configService = new MidwayConfigService(this);
this.initService();
super.init();

this.registerEachCreatedHook();
Expand All @@ -68,6 +67,11 @@ export class MidwayContainer extends Container implements IMidwayContainer {
this.registerObject('ctx', this.ctx);
}

initService() {
this.environmentService = new MidwayEnvironmentService();
this.configService = new MidwayConfigService(this);
}

/**
* load directory and traverse file to find bind class
* @param opts
Expand Down Expand Up @@ -353,13 +357,15 @@ export class MidwayContainer extends Container implements IMidwayContainer {

async ready() {
super.ready();
// register handler for container
this.registerDataHandler(MidwayHandlerKey.CONFIG, (key: string) => {
return this.configService.getConfiguration(key);
});
// 加载配置
await this.configService.load();
if (this.configService) {
// register handler for container
this.registerDataHandler(MidwayHandlerKey.CONFIG, (key: string) => {
return this.configService.getConfiguration(key);
});
// 加载配置
await this.configService.load();
}
}
}

// TODO 测试的 ctx,看看行不行
// TODO configuration 依赖去重
4 changes: 4 additions & 0 deletions packages/midway-core/src/requestContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ export class MidwayRequestContainer extends MidwayContainer {
return this.parent.getAsync<T>(identifier, args);
}
}

initService() {
// do nothing
}
}
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,9 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
imports: [
'../../midway-plugin-mock/src',
'../../midway-plugin-ok/src'
],
})
export class AutoConfiguraion {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Provide, Inject } from '@midwayjs/decorator';

@Provide()
export class BaseService {

@Inject()
userManager;

@Inject('midway-plugin-mock:articleManager')
articleManager;

@Inject('ok:articleManager')
newArticleManager;

async getInformation() {
const result1 = await this.userManager.getUser();
const result2 = await this.articleManager.getOne();
const result3 = await this.newArticleManager.getOne();
return result1 + ',' + result2 + ',' + result3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Provide } from '@midwayjs/decorator';

@Provide()
export class UserManager {
async getUser() {
return 'harry';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "midway-plugin-mock",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Provide } from '@midwayjs/decorator';

@Provide()
export class ArticleManager {
async getOne() {
return 'one article';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({})
export class AutoConfiguraion {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Provide } from '@midwayjs/decorator';

@Provide()
export class ReplaceManager {
async getOne() {
return 'one article';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "midway-plugin-mock",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Provide } from '@midwayjs/decorator';

@Provide()
export class ArticleManager {
async getOne() {
return 'two article';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export = {
ok: {
text: 'ok'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export = {
ok: {
text: 'ok1'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: 'ok',
importConfigs: [
'./config/config.default',
'./config/config.local'
]
})
export class AutoConfiguraion {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Config, Provide } from '@midwayjs/decorator';

@Provide()
export class ReplaceManager {

@Config('ok.text')
config;

async getOne() {
return this.config;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({})
@Configuration({
namespace: ''
})
export class AutoConfiguraion {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: 'ok',
namespace: '',
importConfigs: [
'./config/config.default',
'./config/config.local'
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-mock/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('test/index.test.ts', () => {
assert(service1.getData() !== ('mock_test' + ts));
});

it('should use bootstrap to get app', () => {
it.only('should use bootstrap to get app', () => {
return app.httpRequest()
.get('/api/index')
.expect(200);
Expand Down

0 comments on commit f2c874f

Please sign in to comment.