@midwayjs/web 3.0.13 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/extend/context.js +1 -1
- package/dist/base.js +44 -36
- package/dist/cluster.d.ts +2 -0
- package/dist/cluster.js +36 -0
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +67 -0
- package/dist/config/config.local.d.ts +9 -0
- package/dist/config/config.local.js +12 -0
- package/dist/config/config.unittest.d.ts +9 -0
- package/dist/config/config.unittest.js +12 -0
- package/dist/configuration.js +2 -59
- package/dist/framework/lifecycle.d.ts +14 -0
- package/dist/framework/lifecycle.js +137 -0
- package/dist/framework/web.d.ts +1 -0
- package/dist/framework/web.js +26 -17
- package/dist/index.d.ts +1 -1
- package/dist/index.js +14 -3
- package/dist/interface.d.ts +1 -1
- package/dist/logger.d.ts +9 -3
- package/dist/logger.js +79 -60
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +7 -67
- package/package.json +5 -4
- package/config/config.default.js +0 -54
package/app/extend/context.js
CHANGED
package/dist/base.js
CHANGED
|
@@ -8,6 +8,8 @@ const fs_1 = require("fs");
|
|
|
8
8
|
const logger_1 = require("./logger");
|
|
9
9
|
const router_1 = require("@eggjs/router");
|
|
10
10
|
const util_1 = require("util");
|
|
11
|
+
const lifecycle_1 = require("./framework/lifecycle");
|
|
12
|
+
const web_1 = require("./framework/web");
|
|
11
13
|
const ROUTER = Symbol('EggCore#router');
|
|
12
14
|
const EGG_LOADER = Symbol.for('egg#loader');
|
|
13
15
|
const EGG_PATH = Symbol.for('egg#eggPath');
|
|
@@ -38,9 +40,6 @@ const createAppWorkerLoader = () => {
|
|
|
38
40
|
this.useEggSocketIO = false;
|
|
39
41
|
}
|
|
40
42
|
getEggPaths() {
|
|
41
|
-
if ((0, core_1.getCurrentApplicationContext)()) {
|
|
42
|
-
this.singleProcessMode = true;
|
|
43
|
-
}
|
|
44
43
|
if (!this.appDir) {
|
|
45
44
|
// 这里的逻辑是为了兼容老 cluster 模式
|
|
46
45
|
if (this.app.options.typescript || this.app.options.isTsMode) {
|
|
@@ -104,19 +103,38 @@ const createAppWorkerLoader = () => {
|
|
|
104
103
|
return serverEnv;
|
|
105
104
|
}
|
|
106
105
|
load() {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
106
|
+
/**
|
|
107
|
+
* 由于使用了新的 hook 方式,这个时候 midway 已经初始化了一部分
|
|
108
|
+
* 但是我们把初始化分为了两个部分,framework 相关的留到这里初始化
|
|
109
|
+
* 避免 app 不存在,也能尽可能和单进程模式执行一样的逻辑
|
|
110
|
+
*/
|
|
111
|
+
// lazy initialize framework
|
|
112
|
+
if (process.env['EGG_CLUSTER_MODE'] === 'true') {
|
|
113
|
+
this.app.beforeStart(async () => {
|
|
114
|
+
debug('[egg]: start "initialize framework service with lazy in app.load"');
|
|
115
|
+
const applicationContext = (0, core_1.getCurrentApplicationContext)();
|
|
116
|
+
applicationContext.bind(lifecycle_1.MidwayWebLifeCycleService);
|
|
117
|
+
/**
|
|
118
|
+
* 这里 logger service 已经被 get loggers() 初始化过了,就不需要在这里初始化了
|
|
119
|
+
*/
|
|
120
|
+
// framework/config/plugin/logger/app decorator support
|
|
121
|
+
await applicationContext.getAsync(core_1.MidwayFrameworkService, [
|
|
122
|
+
applicationContext,
|
|
123
|
+
{
|
|
124
|
+
application: this.app,
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
this.app.once('server', async (server) => {
|
|
128
|
+
this.framework.setServer(server);
|
|
129
|
+
// register httpServer to applicationContext
|
|
130
|
+
applicationContext.registerObject(core_1.HTTP_SERVER_KEY, server);
|
|
131
|
+
await this.lifecycleService.afterInit();
|
|
132
|
+
});
|
|
133
|
+
// 这里生命周期走到 onReady
|
|
134
|
+
this.lifecycleService = await applicationContext.getAsync(lifecycle_1.MidwayWebLifeCycleService, [applicationContext]);
|
|
135
|
+
// 执行加载路由
|
|
136
|
+
this.framework = await applicationContext.getAsync(web_1.MidwayWebFramework);
|
|
137
|
+
await this.framework.loadMidwayController();
|
|
120
138
|
});
|
|
121
139
|
}
|
|
122
140
|
}
|
|
@@ -126,15 +144,13 @@ const createAppWorkerLoader = () => {
|
|
|
126
144
|
}
|
|
127
145
|
loadConfig() {
|
|
128
146
|
super.loadConfig();
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
});
|
|
137
|
-
}
|
|
147
|
+
const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService);
|
|
148
|
+
configService.addObject(this.config, true);
|
|
149
|
+
Object.defineProperty(this, 'config', {
|
|
150
|
+
get() {
|
|
151
|
+
return configService.getConfiguration();
|
|
152
|
+
},
|
|
153
|
+
});
|
|
138
154
|
}
|
|
139
155
|
loadMiddleware() {
|
|
140
156
|
super.loadMiddleware();
|
|
@@ -155,9 +171,6 @@ const createAgentWorkerLoader = () => {
|
|
|
155
171
|
require('egg').AgentWorkerLoader;
|
|
156
172
|
class EggAgentWorkerLoader extends AppWorkerLoader {
|
|
157
173
|
getEggPaths() {
|
|
158
|
-
if ((0, core_1.getCurrentApplicationContext)()) {
|
|
159
|
-
this.singleProcessMode = true;
|
|
160
|
-
}
|
|
161
174
|
if (!this.appDir) {
|
|
162
175
|
if (this.app.options.typescript || this.app.options.isTsMode) {
|
|
163
176
|
process.env.EGG_TYPESCRIPT = 'true';
|
|
@@ -222,21 +235,16 @@ const createAgentWorkerLoader = () => {
|
|
|
222
235
|
load() {
|
|
223
236
|
this.app.beforeStart(async () => {
|
|
224
237
|
debug('[egg]: start "initializeAgentApplicationContext"');
|
|
225
|
-
await (0, utils_1.initializeAgentApplicationContext)(this.app
|
|
226
|
-
...this.globalOptions,
|
|
227
|
-
appDir: this.appDir,
|
|
228
|
-
baseDir: this.baseDir,
|
|
229
|
-
ignore: ['**/app/extend/**'],
|
|
230
|
-
});
|
|
238
|
+
await (0, utils_1.initializeAgentApplicationContext)(this.app);
|
|
231
239
|
super.load();
|
|
232
240
|
debug('[egg]: agent load run complete');
|
|
233
241
|
});
|
|
234
242
|
}
|
|
235
243
|
loadConfig() {
|
|
236
244
|
super.loadConfig();
|
|
237
|
-
if (
|
|
245
|
+
if ((0, core_1.getCurrentApplicationContext)()) {
|
|
238
246
|
const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService);
|
|
239
|
-
configService.addObject(this.config);
|
|
247
|
+
configService.addObject(this.config, true);
|
|
240
248
|
Object.defineProperty(this, 'config', {
|
|
241
249
|
get() {
|
|
242
250
|
return configService.getConfiguration();
|
package/dist/cluster.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@midwayjs/core");
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const util_1 = require("util");
|
|
7
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
8
|
+
function runInAgent() {
|
|
9
|
+
return (process.title.indexOf('agent') !== -1 ||
|
|
10
|
+
(process.argv[1] && process.argv[1].indexOf('agent_worker') !== -1));
|
|
11
|
+
}
|
|
12
|
+
// 多进程模式,从 egg-scripts 启动的
|
|
13
|
+
process.env['EGG_CLUSTER_MODE'] = 'true';
|
|
14
|
+
const isAgent = runInAgent();
|
|
15
|
+
debug('[egg]: run with egg-scripts in worker and init midway container in cluster mode');
|
|
16
|
+
const appDir = process.cwd();
|
|
17
|
+
let baseDir;
|
|
18
|
+
if ((0, utils_1.isTypeScriptEnvironment)()) {
|
|
19
|
+
baseDir = (0, path_1.join)(appDir, 'src');
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
baseDir = (0, path_1.join)(appDir, 'dist');
|
|
23
|
+
}
|
|
24
|
+
(0, core_1.initializeGlobalApplicationContext)({
|
|
25
|
+
appDir,
|
|
26
|
+
baseDir,
|
|
27
|
+
ignore: ['**/app/extend/**'],
|
|
28
|
+
lazyInitializeFramework: true,
|
|
29
|
+
});
|
|
30
|
+
if (!isAgent) {
|
|
31
|
+
debug('[egg]: run with egg-scripts in worker and init midway container complete');
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
debug('[egg]: run with egg-scripts in agent and init midway container complete');
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=cluster.js.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const mkdirp = require("mkdirp");
|
|
5
|
+
const os = require("os");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
exports.default = appInfo => {
|
|
8
|
+
const exports = {};
|
|
9
|
+
exports.rundir = path.join(appInfo.appDir, 'run');
|
|
10
|
+
// 修改默认的日志名
|
|
11
|
+
exports.midwayLogger = {
|
|
12
|
+
clients: {
|
|
13
|
+
appLogger: {
|
|
14
|
+
fileLogName: 'midway-web.log',
|
|
15
|
+
aliasName: 'logger',
|
|
16
|
+
},
|
|
17
|
+
agentLogger: {
|
|
18
|
+
fileLogName: 'midway-agent.log',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
exports.egg = {
|
|
23
|
+
dumpConfig: true,
|
|
24
|
+
contextLoggerFormat: info => {
|
|
25
|
+
const ctx = info.ctx;
|
|
26
|
+
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
27
|
+
const userId = ctx.userId || '-';
|
|
28
|
+
const traceId = (ctx.tracer && ctx.tracer.traceId) || '-';
|
|
29
|
+
const use = Date.now() - ctx.startTime;
|
|
30
|
+
const label = userId +
|
|
31
|
+
'/' +
|
|
32
|
+
ctx.ip +
|
|
33
|
+
'/' +
|
|
34
|
+
traceId +
|
|
35
|
+
'/' +
|
|
36
|
+
use +
|
|
37
|
+
'ms ' +
|
|
38
|
+
ctx.method +
|
|
39
|
+
' ' +
|
|
40
|
+
ctx.url;
|
|
41
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
exports.pluginOverwrite = false;
|
|
45
|
+
exports.security = {
|
|
46
|
+
csrf: {
|
|
47
|
+
ignoreJSON: false,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
// alinode runtime 写入的日志策略是: 如果 NODE_LOG_DIR 有设置,写入 NODE_LOG_DIR 设置的目录;否则为 /tmp
|
|
51
|
+
let alinodeLogdir = fs.existsSync('/tmp') ? '/tmp' : os.tmpdir();
|
|
52
|
+
// try to use NODE_LOG_DIR first
|
|
53
|
+
if (process.env.NODE_LOG_DIR) {
|
|
54
|
+
alinodeLogdir = process.env.NODE_LOG_DIR;
|
|
55
|
+
}
|
|
56
|
+
mkdirp.sync(alinodeLogdir);
|
|
57
|
+
exports.alinode = {
|
|
58
|
+
logdir: alinodeLogdir,
|
|
59
|
+
error_log: [
|
|
60
|
+
path.join(appInfo.root, `logs/${appInfo.pkg.name}/common-error.log`),
|
|
61
|
+
path.join(appInfo.root, 'logs/stderr.log'),
|
|
62
|
+
],
|
|
63
|
+
packages: [path.join(appInfo.appDir, 'package.json')],
|
|
64
|
+
};
|
|
65
|
+
return exports;
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=config.default.js.map
|
package/dist/configuration.js
CHANGED
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.EggConfiguration = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
|
+
const path_1 = require("path");
|
|
15
16
|
let EggConfiguration = class EggConfiguration {
|
|
16
17
|
init() {
|
|
17
18
|
this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
@@ -59,65 +60,7 @@ __decorate([
|
|
|
59
60
|
EggConfiguration = __decorate([
|
|
60
61
|
(0, decorator_1.Configuration)({
|
|
61
62
|
namespace: 'egg',
|
|
62
|
-
importConfigs: [
|
|
63
|
-
{
|
|
64
|
-
default: {
|
|
65
|
-
midwayLogger: {
|
|
66
|
-
clients: {
|
|
67
|
-
appLogger: {
|
|
68
|
-
fileLogName: 'midway-web.log',
|
|
69
|
-
aliasName: 'logger',
|
|
70
|
-
},
|
|
71
|
-
agentLogger: {
|
|
72
|
-
fileLogName: 'midway-agent.log',
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
egg: {
|
|
77
|
-
dumpConfig: true,
|
|
78
|
-
contextLoggerFormat: info => {
|
|
79
|
-
const ctx = info.ctx;
|
|
80
|
-
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
81
|
-
const userId = ctx.userId || '-';
|
|
82
|
-
const traceId = (ctx.tracer && ctx.tracer.traceId) || '-';
|
|
83
|
-
const use = Date.now() - ctx.startTime;
|
|
84
|
-
const label = userId +
|
|
85
|
-
'/' +
|
|
86
|
-
ctx.ip +
|
|
87
|
-
'/' +
|
|
88
|
-
traceId +
|
|
89
|
-
'/' +
|
|
90
|
-
use +
|
|
91
|
-
'ms ' +
|
|
92
|
-
ctx.method +
|
|
93
|
-
' ' +
|
|
94
|
-
ctx.url;
|
|
95
|
-
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
test: {
|
|
100
|
-
egg: {
|
|
101
|
-
plugins: {
|
|
102
|
-
'egg-mock': {
|
|
103
|
-
enable: true,
|
|
104
|
-
package: 'egg-mock',
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
unittest: {
|
|
110
|
-
egg: {
|
|
111
|
-
plugins: {
|
|
112
|
-
'egg-mock': {
|
|
113
|
-
enable: true,
|
|
114
|
-
package: 'egg-mock',
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
],
|
|
63
|
+
importConfigs: [(0, path_1.join)(__dirname, 'config')],
|
|
121
64
|
})
|
|
122
65
|
], EggConfiguration);
|
|
123
66
|
exports.EggConfiguration = EggConfiguration;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IMidwayContainer, MidwayConfigService, MidwayFrameworkService } from '@midwayjs/core';
|
|
2
|
+
export declare class MidwayWebLifeCycleService {
|
|
3
|
+
readonly applicationContext: IMidwayContainer;
|
|
4
|
+
protected frameworkService: MidwayFrameworkService;
|
|
5
|
+
protected configService: MidwayConfigService;
|
|
6
|
+
private lifecycleInstanceList;
|
|
7
|
+
constructor(applicationContext: IMidwayContainer);
|
|
8
|
+
protected init(): Promise<void>;
|
|
9
|
+
afterInit(): Promise<void>;
|
|
10
|
+
stop(): Promise<void>;
|
|
11
|
+
private runContainerLifeCycle;
|
|
12
|
+
private runObjectLifeCycle;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MidwayWebLifeCycleService = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
15
|
+
const configuration_1 = require("@midwayjs/core/dist/functional/configuration");
|
|
16
|
+
const util_1 = require("util");
|
|
17
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
18
|
+
let MidwayWebLifeCycleService = class MidwayWebLifeCycleService {
|
|
19
|
+
constructor(applicationContext) {
|
|
20
|
+
this.applicationContext = applicationContext;
|
|
21
|
+
this.lifecycleInstanceList = [];
|
|
22
|
+
}
|
|
23
|
+
async init() {
|
|
24
|
+
// run lifecycle
|
|
25
|
+
const cycles = (0, decorator_1.listModule)(decorator_1.CONFIGURATION_KEY);
|
|
26
|
+
debug(`[core]: Found Configuration length = ${cycles.length}`);
|
|
27
|
+
for (const cycle of cycles) {
|
|
28
|
+
if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
|
|
29
|
+
// 函数式写法
|
|
30
|
+
cycle.instance = cycle.target;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// 普通类写法
|
|
34
|
+
debug(`[core]: Lifecycle run ${cycle.target.name} init`);
|
|
35
|
+
cycle.instance = await this.applicationContext.getAsync(cycle.target);
|
|
36
|
+
}
|
|
37
|
+
if (cycle.instance) {
|
|
38
|
+
this.lifecycleInstanceList.push(cycle);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// bind object lifecycle
|
|
42
|
+
await Promise.all([
|
|
43
|
+
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectCreated'),
|
|
44
|
+
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectCreated'),
|
|
45
|
+
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectInit'),
|
|
46
|
+
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectDestroy'),
|
|
47
|
+
]);
|
|
48
|
+
// bind framework lifecycle
|
|
49
|
+
// onAppError
|
|
50
|
+
// exec onConfigLoad()
|
|
51
|
+
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onConfigLoad', configData => {
|
|
52
|
+
if (configData) {
|
|
53
|
+
this.configService.addObject(configData);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
// exec onReady()
|
|
57
|
+
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onReady');
|
|
58
|
+
}
|
|
59
|
+
async afterInit() {
|
|
60
|
+
// exec framework.run()
|
|
61
|
+
await this.frameworkService.runFramework();
|
|
62
|
+
// exec onServerReady()
|
|
63
|
+
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onServerReady');
|
|
64
|
+
// clear config merge cache
|
|
65
|
+
if (!this.configService.getConfiguration('debug.recordConfigMergeOrder')) {
|
|
66
|
+
this.configService.clearConfigMergeOrder();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async stop() {
|
|
70
|
+
// stop lifecycle
|
|
71
|
+
const cycles = (0, decorator_1.listModule)(decorator_1.CONFIGURATION_KEY);
|
|
72
|
+
for (const cycle of cycles) {
|
|
73
|
+
let inst;
|
|
74
|
+
if (cycle.target instanceof configuration_1.FunctionalConfiguration) {
|
|
75
|
+
// 函数式写法
|
|
76
|
+
inst = cycle.target;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
inst = await this.applicationContext.getAsync(cycle.target);
|
|
80
|
+
}
|
|
81
|
+
await this.runContainerLifeCycle(inst, 'onStop');
|
|
82
|
+
}
|
|
83
|
+
// stop framework
|
|
84
|
+
await this.frameworkService.stopFramework();
|
|
85
|
+
}
|
|
86
|
+
async runContainerLifeCycle(lifecycleInstanceOrList, lifecycle, resultHandler) {
|
|
87
|
+
if (Array.isArray(lifecycleInstanceOrList)) {
|
|
88
|
+
for (const cycle of lifecycleInstanceOrList) {
|
|
89
|
+
if (typeof cycle.instance[lifecycle] === 'function') {
|
|
90
|
+
debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
|
|
91
|
+
const result = await cycle.instance[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
|
|
92
|
+
if (resultHandler) {
|
|
93
|
+
resultHandler(result);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
if (typeof lifecycleInstanceOrList[lifecycle] === 'function') {
|
|
100
|
+
debug(`[core]: Lifecycle run ${lifecycleInstanceOrList.constructor.name} ${lifecycle}`);
|
|
101
|
+
const result = await lifecycleInstanceOrList[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
|
|
102
|
+
if (resultHandler) {
|
|
103
|
+
resultHandler(result);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async runObjectLifeCycle(lifecycleInstanceList, lifecycle) {
|
|
109
|
+
for (const cycle of lifecycleInstanceList) {
|
|
110
|
+
if (typeof cycle.instance[lifecycle] === 'function') {
|
|
111
|
+
debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
|
|
112
|
+
return this.applicationContext[lifecycle](cycle.instance[lifecycle].bind(cycle.instance));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
__decorate([
|
|
118
|
+
(0, decorator_1.Inject)(),
|
|
119
|
+
__metadata("design:type", core_1.MidwayFrameworkService)
|
|
120
|
+
], MidwayWebLifeCycleService.prototype, "frameworkService", void 0);
|
|
121
|
+
__decorate([
|
|
122
|
+
(0, decorator_1.Inject)(),
|
|
123
|
+
__metadata("design:type", core_1.MidwayConfigService)
|
|
124
|
+
], MidwayWebLifeCycleService.prototype, "configService", void 0);
|
|
125
|
+
__decorate([
|
|
126
|
+
(0, decorator_1.Init)(),
|
|
127
|
+
__metadata("design:type", Function),
|
|
128
|
+
__metadata("design:paramtypes", []),
|
|
129
|
+
__metadata("design:returntype", Promise)
|
|
130
|
+
], MidwayWebLifeCycleService.prototype, "init", null);
|
|
131
|
+
MidwayWebLifeCycleService = __decorate([
|
|
132
|
+
(0, decorator_1.Provide)(),
|
|
133
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
|
|
134
|
+
__metadata("design:paramtypes", [Object])
|
|
135
|
+
], MidwayWebLifeCycleService);
|
|
136
|
+
exports.MidwayWebLifeCycleService = MidwayWebLifeCycleService;
|
|
137
|
+
//# sourceMappingURL=lifecycle.js.map
|
package/dist/framework/web.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export declare class MidwayWebFramework extends BaseFramework<Application, Conte
|
|
|
29
29
|
setContextLoggerClass(): void;
|
|
30
30
|
generateMiddleware(middlewareId: any): Promise<any>;
|
|
31
31
|
beforeStop(): Promise<void>;
|
|
32
|
+
setServer(server: any): void;
|
|
32
33
|
}
|
|
33
34
|
export {};
|
|
34
35
|
//# sourceMappingURL=web.d.ts.map
|
package/dist/framework/web.js
CHANGED
|
@@ -87,17 +87,19 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
87
87
|
this.app.use(midwayRootMiddleware);
|
|
88
88
|
this.generator = new EggControllerGenerator(this.app);
|
|
89
89
|
this.overwriteApplication('app');
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
this.app.loader.loadOrigin();
|
|
91
|
+
// 这里拦截 app.use 方法,让他可以加到 midway 的 middlewareManager 中
|
|
92
|
+
this.app.originUse = this.app.use;
|
|
93
|
+
this.app.use = this.app.useMiddleware;
|
|
94
|
+
if (!this.isClusterMode) {
|
|
95
|
+
await new Promise(resolve => {
|
|
96
|
+
this.app.once('application-ready', () => {
|
|
97
|
+
debug('[egg]: web framework: init egg end');
|
|
98
|
+
resolve();
|
|
99
|
+
});
|
|
100
|
+
this.app.ready();
|
|
94
101
|
});
|
|
95
|
-
|
|
96
|
-
// 这里拦截 app.use 方法,让他可以加到 midway 的 middlewareManager 中
|
|
97
|
-
this.app.originUse = this.app.use;
|
|
98
|
-
this.app.use = this.app.useMiddleware;
|
|
99
|
-
this.app.ready();
|
|
100
|
-
});
|
|
102
|
+
}
|
|
101
103
|
}
|
|
102
104
|
overwriteApplication(processType) {
|
|
103
105
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
@@ -136,32 +138,36 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
136
138
|
return core_1.MidwayProcessTypeEnum.AGENT;
|
|
137
139
|
}
|
|
138
140
|
},
|
|
141
|
+
createContextLogger: (ctx, name) => {
|
|
142
|
+
return this.createContextLogger(ctx, name);
|
|
143
|
+
},
|
|
139
144
|
}, ['createAnonymousContext']);
|
|
140
145
|
// if use midway logger will be use midway custom context logger
|
|
141
146
|
debug(`[egg]: overwrite BaseContextLoggerClass to "${processType}"`);
|
|
142
147
|
this.setContextLoggerClass();
|
|
143
148
|
}
|
|
144
149
|
async loadMidwayController() {
|
|
150
|
+
// move egg router to last
|
|
151
|
+
this.app.getMiddleware().findAndInsertLast('eggRouterMiddleware');
|
|
145
152
|
await this.generator.loadMidwayController(this.configurationOptions.globalPrefix, newRouter => {
|
|
146
153
|
var _a;
|
|
147
154
|
const dispatchFn = newRouter.middleware();
|
|
148
155
|
dispatchFn._name = `midwayController(${((_a = newRouter === null || newRouter === void 0 ? void 0 : newRouter.opts) === null || _a === void 0 ? void 0 : _a.prefix) || '/'})`;
|
|
149
156
|
this.app.useMiddleware(dispatchFn);
|
|
150
157
|
});
|
|
158
|
+
// restore use method
|
|
159
|
+
this.app.use = this.app.originUse;
|
|
160
|
+
debug(`[egg]: current middleware = ${this.middlewareManager.getNames()}`);
|
|
151
161
|
}
|
|
152
162
|
getFrameworkType() {
|
|
153
163
|
return decorator_1.MidwayFrameworkType.WEB;
|
|
154
164
|
}
|
|
155
165
|
async run() {
|
|
156
166
|
var _a;
|
|
157
|
-
//
|
|
158
|
-
this.app.getMiddleware().findAndInsertLast('eggRouterMiddleware');
|
|
159
|
-
// load controller
|
|
160
|
-
await this.loadMidwayController();
|
|
161
|
-
// restore use method
|
|
162
|
-
this.app.use = this.app.originUse;
|
|
163
|
-
debug(`[egg]: current middleware = ${this.middlewareManager.getNames()}`);
|
|
167
|
+
// cluster 模式加载路由需在 run 之前,因为 run 需要在拿到 server 之后执行
|
|
164
168
|
if (!this.isClusterMode) {
|
|
169
|
+
// load controller
|
|
170
|
+
await this.loadMidwayController();
|
|
165
171
|
// https config
|
|
166
172
|
if (this.configurationOptions.key && this.configurationOptions.cert) {
|
|
167
173
|
this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
|
|
@@ -238,6 +244,9 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
238
244
|
await this.agent.close();
|
|
239
245
|
}
|
|
240
246
|
}
|
|
247
|
+
setServer(server) {
|
|
248
|
+
this.server = server;
|
|
249
|
+
}
|
|
241
250
|
};
|
|
242
251
|
__decorate([
|
|
243
252
|
(0, decorator_1.Inject)(),
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export * from './interface';
|
|
|
2
2
|
export { MidwayWebFramework as Framework } from './framework/web';
|
|
3
3
|
export { createEggApplication, createEggAgent, createAppWorkerLoader, createAgentWorkerLoader, } from './base';
|
|
4
4
|
export { Application, Agent } from './application';
|
|
5
|
-
export { startCluster } from 'egg';
|
|
6
5
|
export { EggConfiguration as Configuration } from './configuration';
|
|
6
|
+
export declare function startCluster(serverConfig: any, callback?: any): any;
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.
|
|
13
|
+
exports.startCluster = exports.Configuration = exports.Agent = exports.Application = exports.createAgentWorkerLoader = exports.createAppWorkerLoader = exports.createEggAgent = exports.createEggApplication = exports.Framework = void 0;
|
|
14
|
+
const egg_cluster_1 = require("egg-cluster");
|
|
15
|
+
const path_1 = require("path");
|
|
14
16
|
__exportStar(require("./interface"), exports);
|
|
15
17
|
var web_1 = require("./framework/web");
|
|
16
18
|
Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return web_1.MidwayWebFramework; } });
|
|
@@ -22,8 +24,17 @@ Object.defineProperty(exports, "createAgentWorkerLoader", { enumerable: true, ge
|
|
|
22
24
|
var application_1 = require("./application");
|
|
23
25
|
Object.defineProperty(exports, "Application", { enumerable: true, get: function () { return application_1.Application; } });
|
|
24
26
|
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return application_1.Agent; } });
|
|
25
|
-
var egg_1 = require("egg");
|
|
26
|
-
Object.defineProperty(exports, "startCluster", { enumerable: true, get: function () { return egg_1.startCluster; } });
|
|
27
27
|
var configuration_1 = require("./configuration");
|
|
28
28
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.EggConfiguration; } });
|
|
29
|
+
function startCluster(serverConfig, callback) {
|
|
30
|
+
if (!serverConfig['require']) {
|
|
31
|
+
serverConfig['require'] = [];
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(serverConfig['require'])) {
|
|
34
|
+
serverConfig['require'] = [serverConfig['require']];
|
|
35
|
+
}
|
|
36
|
+
serverConfig['require'].push((0, path_1.join)(__dirname, 'cluster'));
|
|
37
|
+
return (0, egg_cluster_1.startCluster)(serverConfig, callback);
|
|
38
|
+
}
|
|
39
|
+
exports.startCluster = startCluster;
|
|
29
40
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -86,7 +86,7 @@ export interface IWebMiddleware {
|
|
|
86
86
|
resolve(): MidwayWebMiddleware;
|
|
87
87
|
}
|
|
88
88
|
declare module '@midwayjs/core/dist/interface' {
|
|
89
|
-
interface MidwayConfig extends EggAppConfig {
|
|
89
|
+
interface MidwayConfig extends Partial<EggAppConfig> {
|
|
90
90
|
egg?: IMidwayWebConfigurationOptions;
|
|
91
91
|
}
|
|
92
92
|
}
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ILogger } from '@midwayjs/logger';
|
|
2
2
|
import { Application } from 'egg';
|
|
3
|
+
import { MidwayLoggerService } from '@midwayjs/core';
|
|
3
4
|
declare class MidwayLoggers extends Map<string, ILogger> {
|
|
4
|
-
app: Application;
|
|
5
|
+
protected app: Application;
|
|
6
|
+
protected loggerService: MidwayLoggerService;
|
|
5
7
|
/**
|
|
6
8
|
* @constructor
|
|
7
9
|
* - logger
|
|
@@ -20,13 +22,17 @@ declare class MidwayLoggers extends Map<string, ILogger> {
|
|
|
20
22
|
* - {String} eol - end of line char
|
|
21
23
|
* - {String} [concentrateError = duplicate] - whether write error logger to common-error.log, `duplicate` / `redirect` / `ignore`
|
|
22
24
|
* - customLogger
|
|
23
|
-
* @param options
|
|
24
25
|
* @param app
|
|
25
26
|
*/
|
|
26
|
-
constructor(
|
|
27
|
+
constructor(app: Application);
|
|
27
28
|
createLogger(options: any, loggerKey: string): ILogger;
|
|
28
29
|
disableConsole(): void;
|
|
29
30
|
reload(): void;
|
|
31
|
+
transformEggLogger(eggCustomLogger: any): {
|
|
32
|
+
midwayLogger: {
|
|
33
|
+
clients: {};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
30
36
|
}
|
|
31
37
|
export declare const createLoggers: (app: Application, processType: 'agent' | 'app') => MidwayLoggers;
|
|
32
38
|
export {};
|
package/dist/logger.js
CHANGED
|
@@ -16,6 +16,34 @@ function isEmptyFile(p) {
|
|
|
16
16
|
});
|
|
17
17
|
return content === null || content === undefined || content === '';
|
|
18
18
|
}
|
|
19
|
+
const levelTransform = level => {
|
|
20
|
+
if (!level) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
switch (level) {
|
|
24
|
+
case 'NONE':
|
|
25
|
+
case Infinity: // egg logger 的 none 是这个等级
|
|
26
|
+
return 'none';
|
|
27
|
+
case 0:
|
|
28
|
+
case 'DEBUG':
|
|
29
|
+
case 'debug':
|
|
30
|
+
return 'debug';
|
|
31
|
+
case 1:
|
|
32
|
+
case 'INFO':
|
|
33
|
+
case 'info':
|
|
34
|
+
return 'info';
|
|
35
|
+
case 2:
|
|
36
|
+
case 'WARN':
|
|
37
|
+
case 'warn':
|
|
38
|
+
return 'warn';
|
|
39
|
+
case 3:
|
|
40
|
+
case 'ERROR':
|
|
41
|
+
case 'error':
|
|
42
|
+
return 'error';
|
|
43
|
+
default:
|
|
44
|
+
return 'silly';
|
|
45
|
+
}
|
|
46
|
+
};
|
|
19
47
|
function checkEggLoggerExistsAndBackup(dir, fileName) {
|
|
20
48
|
const file = (0, path_1.isAbsolute)(fileName) ? fileName : (0, path_1.join)(dir, fileName);
|
|
21
49
|
if ((0, fs_1.existsSync)(file) && !(0, fs_1.lstatSync)(file).isSymbolicLink()) {
|
|
@@ -32,6 +60,13 @@ function checkEggLoggerExistsAndBackup(dir, fileName) {
|
|
|
32
60
|
}
|
|
33
61
|
}
|
|
34
62
|
}
|
|
63
|
+
function cleanUndefinedProperty(obj) {
|
|
64
|
+
Object.keys(obj).forEach(key => {
|
|
65
|
+
if (obj[key] === undefined) {
|
|
66
|
+
delete obj[key];
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
35
70
|
class MidwayLoggers extends Map {
|
|
36
71
|
/**
|
|
37
72
|
* @constructor
|
|
@@ -51,59 +86,30 @@ class MidwayLoggers extends Map {
|
|
|
51
86
|
* - {String} eol - end of line char
|
|
52
87
|
* - {String} [concentrateError = duplicate] - whether write error logger to common-error.log, `duplicate` / `redirect` / `ignore`
|
|
53
88
|
* - customLogger
|
|
54
|
-
* @param options
|
|
55
89
|
* @param app
|
|
56
90
|
*/
|
|
57
|
-
constructor(
|
|
58
|
-
var _a, _b;
|
|
91
|
+
constructor(app) {
|
|
59
92
|
super();
|
|
60
93
|
/**
|
|
61
|
-
*
|
|
62
|
-
* 1、单进程,使用 egg logger
|
|
63
|
-
* 2、单进程,使用 midway logger
|
|
64
|
-
* 3、egg-scripts 多进程,使用 egg-logger
|
|
65
|
-
* 4、egg-scripts 多进程,使用 midway logger
|
|
94
|
+
* 在 egg 这里创建 loggers,并初始化 loggerService
|
|
66
95
|
*/
|
|
67
96
|
// 这么改是为了防止 egg 日志切割时遍历属性,导致报错
|
|
68
97
|
Object.defineProperty(this, 'app', {
|
|
69
98
|
value: app,
|
|
70
99
|
enumerable: false,
|
|
71
100
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
options.logger.agentLogName,
|
|
79
|
-
options.logger.errorLogName,
|
|
80
|
-
]) {
|
|
81
|
-
checkEggLoggerExistsAndBackup(options.logger.dir, name);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* 走到这里,有几种情况
|
|
85
|
-
* 1、egg-scripts 启动,并使用了 midway logger,egg 先启动了
|
|
86
|
-
* 2、单进程启动,但是没有 configuration,midway 没读到配置,日志服务没初始化
|
|
87
|
-
* 3、再走一遍日志创建,把 egg 插件配置的日志再初始化一遍
|
|
88
|
-
*/
|
|
89
|
-
const loggerService = new core_1.MidwayLoggerService(null);
|
|
90
|
-
loggerService.configService = {
|
|
91
|
-
getConfiguration(configKey) {
|
|
92
|
-
if (configKey) {
|
|
93
|
-
return (0, core_1.safelyGet)(configKey, options);
|
|
94
|
-
}
|
|
95
|
-
return this.configuration;
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
const midwayLoggerConfig = loggerService.transformEggLogger(options);
|
|
99
|
-
(0, core_1.extend)(true, options, midwayLoggerConfig);
|
|
100
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.midwayLogger) === null || _a === void 0 ? void 0 : _a.clients) {
|
|
101
|
-
// 从 egg 过来,这里有可能没有 dir
|
|
102
|
-
if (!((_b = options.midwayLogger['default']) === null || _b === void 0 ? void 0 : _b.dir)) {
|
|
103
|
-
options.midwayLogger.default['dir'] = options.logger.dir;
|
|
101
|
+
const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService);
|
|
102
|
+
// 先把 egg 的日志配置转为 midway logger 配置
|
|
103
|
+
if (configService.getConfiguration('customLogger')) {
|
|
104
|
+
const eggLoggerConfig = this.transformEggLogger(configService.getConfiguration('customLogger'));
|
|
105
|
+
if (eggLoggerConfig) {
|
|
106
|
+
configService.addObject(eggLoggerConfig);
|
|
104
107
|
}
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
}
|
|
109
|
+
const loggerConfig = configService.getConfiguration('midwayLogger');
|
|
110
|
+
if (loggerConfig) {
|
|
111
|
+
for (const id of Object.keys(loggerConfig.clients)) {
|
|
112
|
+
const config = Object.assign({}, loggerConfig['default'], loggerConfig.clients[id]);
|
|
107
113
|
this.createLogger(config, id);
|
|
108
114
|
}
|
|
109
115
|
}
|
|
@@ -111,16 +117,18 @@ class MidwayLoggers extends Map {
|
|
|
111
117
|
this['logger'] = logger_1.loggers.getLogger('appLogger');
|
|
112
118
|
this.set('logger', logger_1.loggers.getLogger('appLogger'));
|
|
113
119
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
console.log('-----请在 logger 里配置 clients');
|
|
121
|
-
}
|
|
120
|
+
// 初始化日志服务
|
|
121
|
+
this.loggerService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayLoggerService, [(0, core_1.getCurrentApplicationContext)()]);
|
|
122
|
+
// 防止循环枚举报错
|
|
123
|
+
Object.defineProperty(this, 'loggerService', {
|
|
124
|
+
enumerable: false,
|
|
125
|
+
});
|
|
122
126
|
}
|
|
123
127
|
createLogger(options, loggerKey) {
|
|
128
|
+
/**
|
|
129
|
+
* 提前备份 egg 日志
|
|
130
|
+
*/
|
|
131
|
+
checkEggLoggerExistsAndBackup(options.dir, options.fileLogName);
|
|
124
132
|
const logger = logger_1.loggers.createLogger(loggerKey, options);
|
|
125
133
|
// overwrite values for pandora collect
|
|
126
134
|
logger.values = () => {
|
|
@@ -149,25 +157,36 @@ class MidwayLoggers extends Map {
|
|
|
149
157
|
}
|
|
150
158
|
}
|
|
151
159
|
}
|
|
160
|
+
transformEggLogger(eggCustomLogger) {
|
|
161
|
+
var _a, _b, _c;
|
|
162
|
+
const transformLoggerConfig = {
|
|
163
|
+
midwayLogger: {
|
|
164
|
+
clients: {},
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
for (const name in eggCustomLogger) {
|
|
168
|
+
transformLoggerConfig.midwayLogger.clients[name] = {
|
|
169
|
+
fileLogName: (_a = eggCustomLogger[name]) === null || _a === void 0 ? void 0 : _a.file,
|
|
170
|
+
level: levelTransform((_b = eggCustomLogger[name]) === null || _b === void 0 ? void 0 : _b.level),
|
|
171
|
+
consoleLevel: levelTransform((_c = eggCustomLogger[name]) === null || _c === void 0 ? void 0 : _c.consoleLevel),
|
|
172
|
+
};
|
|
173
|
+
cleanUndefinedProperty(transformLoggerConfig.midwayLogger.clients[name]);
|
|
174
|
+
}
|
|
175
|
+
return transformLoggerConfig;
|
|
176
|
+
}
|
|
152
177
|
}
|
|
153
178
|
const createLoggers = (app, processType) => {
|
|
154
|
-
const loggerConfig = app.config.logger;
|
|
155
|
-
loggerConfig.type = app.type;
|
|
156
|
-
if (app.config.env === 'prod' &&
|
|
157
|
-
loggerConfig.level === 'DEBUG' &&
|
|
158
|
-
!loggerConfig.allowDebugAtProd) {
|
|
159
|
-
loggerConfig.level = 'INFO';
|
|
160
|
-
}
|
|
161
179
|
// 现在只走 midway logger
|
|
162
|
-
const loggers = new MidwayLoggers(app
|
|
180
|
+
const loggers = new MidwayLoggers(app);
|
|
163
181
|
// won't print to console after started, except for local and unittest
|
|
164
182
|
app.ready(() => {
|
|
165
|
-
|
|
183
|
+
var _a;
|
|
184
|
+
if ((_a = app.config.logger) === null || _a === void 0 ? void 0 : _a.disableConsoleAfterReady) {
|
|
166
185
|
loggers.disableConsole();
|
|
167
186
|
}
|
|
168
187
|
});
|
|
169
188
|
debug(`[egg]: create loggers in ${processType}`);
|
|
170
|
-
loggers['coreLogger'].info('[egg:logger] init all loggers with options: %j',
|
|
189
|
+
loggers['coreLogger'].info('[egg:logger] init all loggers with options: %j', app.config.midwayLogger);
|
|
171
190
|
return loggers;
|
|
172
191
|
};
|
|
173
192
|
exports.createLoggers = createLoggers;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { IMidwayBootstrapOptions } from '@midwayjs/core';
|
|
2
1
|
export declare function isTypeScriptEnvironment(): boolean;
|
|
3
2
|
export declare const parseNormalDir: (baseDir: string, isTypescript?: boolean) => {
|
|
4
3
|
baseDir: string;
|
|
@@ -6,5 +5,5 @@ export declare const parseNormalDir: (baseDir: string, isTypescript?: boolean) =
|
|
|
6
5
|
};
|
|
7
6
|
export declare const findLernaRoot: (findRoot?: string) => string;
|
|
8
7
|
export declare const getCurrentDateString: (timestamp?: number) => string;
|
|
9
|
-
export declare function initializeAgentApplicationContext(agent: any
|
|
8
|
+
export declare function initializeAgentApplicationContext(agent: any): Promise<import("@midwayjs/core").IMidwayContainer>;
|
|
10
9
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
CHANGED
|
@@ -66,75 +66,12 @@ const getCurrentDateString = (timestamp = Date.now()) => {
|
|
|
66
66
|
.padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
|
|
67
67
|
};
|
|
68
68
|
exports.getCurrentDateString = getCurrentDateString;
|
|
69
|
-
async function initializeAgentApplicationContext(agent
|
|
70
|
-
|
|
71
|
-
if (!(0, core_1.getCurrentApplicationContext)()) {
|
|
72
|
-
debug('[egg]: start "initializeGlobalApplicationContext"');
|
|
73
|
-
const appDir = (_a = globalOptions.appDir) !== null && _a !== void 0 ? _a : '';
|
|
74
|
-
const baseDir = (_b = globalOptions.baseDir) !== null && _b !== void 0 ? _b : '';
|
|
75
|
-
// new container
|
|
76
|
-
const applicationContext = new core_1.MidwayContainer();
|
|
77
|
-
debug('[egg]: delegate module map from decoratorManager');
|
|
78
|
-
(0, decorator_1.bindContainer)(applicationContext);
|
|
79
|
-
global['MIDWAY_APPLICATION_CONTEXT'] = applicationContext;
|
|
80
|
-
// register baseDir and appDir
|
|
81
|
-
applicationContext.registerObject('baseDir', baseDir);
|
|
82
|
-
applicationContext.registerObject('appDir', appDir);
|
|
83
|
-
if (globalOptions.moduleDirector !== false) {
|
|
84
|
-
if (globalOptions.moduleDetector === undefined ||
|
|
85
|
-
globalOptions.moduleDetector === 'file') {
|
|
86
|
-
applicationContext.setFileDetector(new core_1.DirectoryFileDetector({
|
|
87
|
-
loadDir: baseDir,
|
|
88
|
-
ignore: (_c = globalOptions.ignore) !== null && _c !== void 0 ? _c : [],
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
else if (globalOptions.moduleDetector) {
|
|
92
|
-
applicationContext.setFileDetector(globalOptions.moduleDetector);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// bind inner service
|
|
96
|
-
applicationContext.bindClass(core_1.MidwayEnvironmentService);
|
|
97
|
-
applicationContext.bindClass(core_1.MidwayInformationService);
|
|
98
|
-
applicationContext.bindClass(core_1.MidwayDecoratorService);
|
|
99
|
-
applicationContext.bindClass(core_1.MidwayConfigService);
|
|
100
|
-
applicationContext.bindClass(core_1.MidwayAspectService);
|
|
101
|
-
applicationContext.bindClass(core_1.MidwayLoggerService);
|
|
102
|
-
// bind preload module
|
|
103
|
-
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
104
|
-
for (const preloadModule of globalOptions.preloadModules) {
|
|
105
|
-
applicationContext.bindClass(preloadModule);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
// init default config
|
|
109
|
-
const configService = await applicationContext.getAsync(core_1.MidwayConfigService);
|
|
110
|
-
// add egg config here, it will be ignore midway and component config
|
|
111
|
-
configService.add([
|
|
112
|
-
{
|
|
113
|
-
default: agent.config,
|
|
114
|
-
},
|
|
115
|
-
]);
|
|
69
|
+
async function initializeAgentApplicationContext(agent) {
|
|
70
|
+
if (process.env['EGG_CLUSTER_MODE'] === 'true') {
|
|
116
71
|
// init aop support
|
|
117
|
-
const aspectService =
|
|
72
|
+
const aspectService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayAspectService);
|
|
118
73
|
// init decorator service
|
|
119
|
-
const decoratorService =
|
|
120
|
-
if (!globalOptions.imports) {
|
|
121
|
-
globalOptions.imports = [
|
|
122
|
-
(0, core_1.safeRequire)((0, path_1.join)(globalOptions.baseDir, 'configuration')),
|
|
123
|
-
];
|
|
124
|
-
}
|
|
125
|
-
for (const configurationModule of [].concat(globalOptions.imports)) {
|
|
126
|
-
// load configuration and component
|
|
127
|
-
applicationContext.load(configurationModule);
|
|
128
|
-
}
|
|
129
|
-
// bind user code module
|
|
130
|
-
await applicationContext.ready();
|
|
131
|
-
// merge config
|
|
132
|
-
await configService.load();
|
|
133
|
-
debug('[core]: Current config = %j', configService.getConfiguration());
|
|
134
|
-
// init logger
|
|
135
|
-
await applicationContext.getAsync(core_1.MidwayLoggerService, [
|
|
136
|
-
applicationContext,
|
|
137
|
-
]);
|
|
74
|
+
const decoratorService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayDecoratorService);
|
|
138
75
|
// framework/config/plugin/logger/app decorator support
|
|
139
76
|
// register base config hook
|
|
140
77
|
decoratorService.registerPropertyHandler(decorator_1.CONFIG_KEY, (propertyName, meta) => {
|
|
@@ -164,12 +101,15 @@ async function initializeAgentApplicationContext(agent, globalOptions) {
|
|
|
164
101
|
});
|
|
165
102
|
// init aspect module
|
|
166
103
|
await aspectService.loadAspect();
|
|
104
|
+
debug('[egg]: added extra for "initializeAgentApplicationContext" in cluster mode');
|
|
167
105
|
}
|
|
168
106
|
else {
|
|
169
107
|
debug('[egg]: "initializeAgentApplicationContext" ignore re-init in single process');
|
|
170
108
|
}
|
|
171
109
|
const applicationContext = (0, core_1.getCurrentApplicationContext)();
|
|
172
110
|
const agentFramework = new web_1.MidwayWebFramework(applicationContext);
|
|
111
|
+
agentFramework['logger'] = agent.logger;
|
|
112
|
+
agentFramework['appLogger'] = agent.coreLogger;
|
|
173
113
|
agentFramework.app = agent;
|
|
174
114
|
agentFramework.configService = applicationContext.get(core_1.MidwayConfigService);
|
|
175
115
|
agentFramework.overwriteApplication('agent');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/web",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Midway Web Framework for Egg.js",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@midwayjs/decorator": "^3.0.10",
|
|
31
31
|
"@midwayjs/logger": "^2.15.0",
|
|
32
|
-
"@midwayjs/mock": "^3.0
|
|
32
|
+
"@midwayjs/mock": "^3.1.0",
|
|
33
33
|
"axios": "0.26.0",
|
|
34
34
|
"dayjs": "1.10.8",
|
|
35
35
|
"egg-logger": "2.7.1",
|
|
@@ -46,8 +46,9 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@eggjs/router": "^2.0.0",
|
|
49
|
-
"@midwayjs/core": "^3.0
|
|
49
|
+
"@midwayjs/core": "^3.1.0",
|
|
50
50
|
"egg": "^2.28.0",
|
|
51
|
+
"egg-cluster": "^1.27.1",
|
|
51
52
|
"find-up": "5.0.0",
|
|
52
53
|
"mkdirp": "^1.0.4"
|
|
53
54
|
},
|
|
@@ -59,5 +60,5 @@
|
|
|
59
60
|
"engines": {
|
|
60
61
|
"node": ">=12"
|
|
61
62
|
},
|
|
62
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "2074c838e454a9673a044dbe065631dd9f944005"
|
|
63
64
|
}
|
package/config/config.default.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const mkdirp = require('mkdirp');
|
|
5
|
-
const os = require('os');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
|
|
8
|
-
module.exports = appInfo => {
|
|
9
|
-
const exports = {};
|
|
10
|
-
|
|
11
|
-
exports.rundir = path.join(appInfo.appDir, 'run');
|
|
12
|
-
// 修改默认的日志名
|
|
13
|
-
exports.midwayLogger = {
|
|
14
|
-
clients: {
|
|
15
|
-
coreLogger: {
|
|
16
|
-
fileLogName: 'midway-core.log',
|
|
17
|
-
},
|
|
18
|
-
appLogger: {
|
|
19
|
-
fileLogName: 'midway-web.log',
|
|
20
|
-
aliasName: 'logger',
|
|
21
|
-
},
|
|
22
|
-
agentLogger: {
|
|
23
|
-
fileLogName: 'midway-agent.log',
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
exports.pluginOverwrite = false;
|
|
29
|
-
|
|
30
|
-
exports.security = {
|
|
31
|
-
csrf: {
|
|
32
|
-
ignoreJSON: false,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// alinode runtime 写入的日志策略是: 如果 NODE_LOG_DIR 有设置,写入 NODE_LOG_DIR 设置的目录;否则为 /tmp
|
|
37
|
-
let alinodeLogdir = fs.existsSync('/tmp') ? '/tmp' : os.tmpdir();
|
|
38
|
-
// try to use NODE_LOG_DIR first
|
|
39
|
-
if (process.env.NODE_LOG_DIR) {
|
|
40
|
-
alinodeLogdir = process.env.NODE_LOG_DIR;
|
|
41
|
-
}
|
|
42
|
-
mkdirp.sync(alinodeLogdir);
|
|
43
|
-
|
|
44
|
-
exports.alinode = {
|
|
45
|
-
logdir: alinodeLogdir,
|
|
46
|
-
error_log: [
|
|
47
|
-
path.join(appInfo.root, `logs/${appInfo.pkg.name}/common-error.log`),
|
|
48
|
-
path.join(appInfo.root, 'logs/stderr.log'),
|
|
49
|
-
],
|
|
50
|
-
packages: [path.join(appInfo.appDir, 'package.json')],
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return exports;
|
|
54
|
-
};
|