-
Notifications
You must be signed in to change notification settings - Fork 568
/
frameworkService.ts
284 lines (252 loc) · 8.45 KB
/
frameworkService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import {
ALL,
APPLICATION_KEY,
CONFIG_KEY,
FRAMEWORK_KEY,
getProviderUUId,
Init,
Inject,
listModule,
LOGGER_KEY,
PIPELINE_IDENTIFIER,
PLUGIN_KEY,
Provide,
Scope,
FACTORY_SERVICE_CLIENT_KEY,
} from '../decorator';
import {
IMidwayContainer,
IMidwayFramework,
IServiceFactory,
ScopeEnum,
MidwayFrameworkType,
} from '../interface';
import { MidwayConfigService } from './configService';
import { MidwayLoggerService } from './loggerService';
import { BaseFramework } from '../baseFramework';
import { MidwayPipelineService } from './pipelineService';
import { MidwayDecoratorService } from './decoratorService';
import { MidwayAspectService } from './aspectService';
import { MidwayApplicationManager } from '../common/applicationManager';
import * as util from 'util';
import { MidwayCommonError, MidwayParameterError } from '../error';
import { REQUEST_OBJ_CTX_KEY } from '../constants';
const debug = util.debuglog('midway:debug');
@Provide()
@Scope(ScopeEnum.Singleton)
export class MidwayFrameworkService {
@Inject()
configService: MidwayConfigService;
@Inject()
loggerService: MidwayLoggerService;
@Inject()
aspectService: MidwayAspectService;
@Inject()
decoratorService: MidwayDecoratorService;
@Inject()
applicationManager: MidwayApplicationManager;
constructor(
readonly applicationContext: IMidwayContainer,
readonly globalOptions
) {}
private mainFramework: IMidwayFramework<any, any, any>;
private globalFrameworkList: Array<IMidwayFramework<any, any, any>> = [];
@Init()
protected async init() {
// register base config hook
this.decoratorService.registerPropertyHandler(
CONFIG_KEY,
(propertyName, meta) => {
if (meta.identifier === ALL) {
return this.configService.getConfiguration();
} else {
return this.configService.getConfiguration(
meta.identifier ?? propertyName
);
}
}
);
// register @Logger decorator handler
this.decoratorService.registerPropertyHandler(
LOGGER_KEY,
(propertyName, meta) => {
return this.loggerService.getLogger(meta.identifier ?? propertyName);
}
);
this.decoratorService.registerPropertyHandler(
PIPELINE_IDENTIFIER,
(key, meta, instance) => {
return new MidwayPipelineService(
instance[REQUEST_OBJ_CTX_KEY]?.requestContext ??
this.applicationContext,
meta.valves
);
}
);
// register @App decorator handler
this.decoratorService.registerPropertyHandler(
APPLICATION_KEY,
(propertyName, meta) => {
if (meta.type) {
const framework = this.applicationManager.getApplication(meta.type);
if (!framework) {
throw new MidwayCommonError(`Framework ${meta.type} not Found`);
}
return framework;
} else {
return this.getMainApp();
}
}
);
this.decoratorService.registerPropertyHandler(
PLUGIN_KEY,
(propertyName, meta) => {
return this.getMainApp()[meta.identifier ?? propertyName];
}
);
this.decoratorService.registerPropertyHandler(
FACTORY_SERVICE_CLIENT_KEY,
(
propertyName,
meta: {
serviceFactoryClz: new (...args) => IServiceFactory<unknown>;
clientName: string;
}
) => {
const factory = this.applicationContext.get(meta.serviceFactoryClz);
const clientName = meta.clientName || factory.getDefaultClientName();
if (clientName && factory.has(clientName)) {
return factory.get(clientName);
} else {
if (!clientName) {
throw new MidwayParameterError(
`Please set clientName or options.defaultClientName for ${meta.serviceFactoryClz.name}).`
);
} else {
throw new MidwayParameterError(
`ClientName(${clientName} not found in ${meta.serviceFactoryClz.name}).`
);
}
}
}
);
let frameworks: Array<new (...args) => any> = listModule(FRAMEWORK_KEY);
// filter proto
frameworks = filterProtoFramework(frameworks);
debug(`[core]: Found Framework length = ${frameworks.length}`);
if (frameworks.length) {
for (const frameworkClz of frameworks) {
if (
!this.applicationContext.hasDefinition(getProviderUUId(frameworkClz))
) {
debug(
`[core]: Found Framework "${frameworkClz.name}" but missing definition, skip initialize.`
);
continue;
}
const frameworkInstance = await this.applicationContext.getAsync<
IMidwayFramework<any, any, any>
>(frameworkClz, [this.applicationContext]);
// if enable, just init framework
if (frameworkInstance.isEnable()) {
// app init
await frameworkInstance.initialize({
applicationContext: this.applicationContext,
namespace: frameworkInstance.getNamespace(),
...this.globalOptions,
});
debug(
`[core]: Found Framework "${frameworkInstance.getFrameworkName()}" and initialize.`
);
} else {
debug(
`[core]: Found Framework "${frameworkInstance.getFrameworkName()}" and delay initialize.`
);
}
// app init
const definition = this.applicationContext.registry.getDefinition(
getProviderUUId(frameworkClz)
);
// set framework namespace here
frameworkInstance.setNamespace(definition?.namespace);
// link framework to application manager
this.applicationManager.addFramework(
definition?.namespace ?? frameworkInstance.getFrameworkName(),
frameworkInstance
);
this.globalFrameworkList.push(frameworkInstance);
}
let mainNs;
/**
* 这里处理引入组件的顺序,在主框架之前是否包含其他的 framework
* 1、装饰器的顺序和 import 的写的顺序有关
* 2、主框架和 configuration 中的配置加载顺序有关
* 3、两者不符合的话,App 装饰器获取的 app 会不一致,导致中间件等无法正常使用
*/
const namespaceList = this.applicationContext.getNamespaceList();
for (const namespace of namespaceList) {
const framework = this.applicationManager.getApplication(namespace);
if (framework) {
mainNs = namespace;
break;
}
}
global['MIDWAY_MAIN_FRAMEWORK'] = this.mainFramework =
this.applicationManager.getFramework(mainNs) ??
this.globalFrameworkList[0];
debug(`[core]: Current main Framework is "${mainNs}".`);
}
// init aspect module
await this.aspectService.loadAspect();
}
public getMainApp() {
return this.mainFramework?.getApplication();
}
public getMainFramework() {
return this.mainFramework;
}
public getFramework(namespaceOrFrameworkType: string | MidwayFrameworkType) {
return this.applicationManager.getFramework(namespaceOrFrameworkType);
}
public async runFramework() {
const namespaceList = this.applicationContext.getNamespaceList();
// globalFrameworkList 需要基于 namespaceList 进行排序,不然会出现顺序问题
this.globalFrameworkList = this.globalFrameworkList.sort((a, b) => {
return (
namespaceList.indexOf(a.getNamespace()) -
namespaceList.indexOf(b.getNamespace())
);
});
for (const frameworkInstance of this.globalFrameworkList) {
// if enable, just init framework
if (frameworkInstance.isEnable()) {
// app init
await frameworkInstance.run();
debug(
`[core]: Found Framework "${frameworkInstance.getFrameworkName()}" and run.`
);
}
}
}
public async stopFramework() {
await Promise.all(
Array.from(this.globalFrameworkList).map(frameworkInstance => {
return frameworkInstance.stop();
})
);
}
}
function filterProtoFramework(frameworks) {
const frameworkProtoArr = [];
// 这里把继承的框架父类都找出来,然后排除掉,只取第一层
for (const framework of frameworks) {
let proto = Object.getPrototypeOf(framework);
while (proto.name && proto.name !== BaseFramework.name) {
frameworkProtoArr.push(proto);
proto = Object.getPrototypeOf(proto);
}
}
return frameworks.filter(framework => {
return !frameworkProtoArr.includes(framework);
});
}