ee-core 1.5.1-beta.1 → 1.5.1
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/LICENSE +21 -21
- package/README.md +2 -2
- package/addon/window/index.js +91 -91
- package/bin/tools.js +17 -17
- package/config/config.default.js +280 -280
- package/core/index.js +12 -12
- package/core/lib/ee.js +218 -218
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +457 -457
- package/core/lib/loader/file_loader.js +325 -325
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +135 -135
- package/core/lib/loader/mixin/controller.js +124 -124
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/index.js +127 -127
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/index.js +49 -49
- package/lib/appLoader.js +53 -53
- package/lib/application.js +84 -84
- package/lib/baseApp.js +131 -131
- package/lib/constant.js +9 -9
- package/lib/eeApp.js +359 -359
- package/lib/httpclient.js +136 -136
- package/lib/logger.js +46 -46
- package/lib/socket/httpServer.js +142 -142
- package/lib/socket/io.js +23 -23
- package/lib/socket/ipcServer.js +108 -108
- package/lib/socket/socketClient.js +50 -50
- package/lib/socket/socketServer.js +76 -76
- package/lib/socket/start.js +22 -22
- package/lib/storage/index.js +33 -33
- package/lib/storage/lowdbStorage.js +98 -98
- package/lib/storage/sqliteStorage.js +127 -127
- package/package.json +45 -45
- package/tools/encrypt.js +274 -274
- package/tools/replaceDist.js +61 -61
- package/utils/common.js +90 -90
- package/utils/index.js +246 -246
- package/utils/wrap.js +37 -37
package/lib/httpclient.js
CHANGED
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const Agent = require('agentkeepalive');
|
|
4
|
-
const HttpsAgent = require('agentkeepalive').HttpsAgent;
|
|
5
|
-
const urllib = require('urllib');
|
|
6
|
-
const ms = require('humanize-ms');
|
|
7
|
-
const { FrameworkBaseError } = require('egg-errors');
|
|
8
|
-
|
|
9
|
-
class HttpClientError extends FrameworkBaseError {
|
|
10
|
-
get module() {
|
|
11
|
-
return 'httpclient';
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class HttpClient extends urllib.HttpClient2 {
|
|
16
|
-
constructor(app) {
|
|
17
|
-
normalizeConfig(app);
|
|
18
|
-
const config = app.config.httpclient;
|
|
19
|
-
super({
|
|
20
|
-
app,
|
|
21
|
-
defaultArgs: config.request,
|
|
22
|
-
agent: new Agent(config.httpAgent),
|
|
23
|
-
httpsAgent: new HttpsAgent(config.httpsAgent),
|
|
24
|
-
});
|
|
25
|
-
this.app = app;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
request(url, args, callback) {
|
|
29
|
-
if (typeof args === 'function') {
|
|
30
|
-
callback = args;
|
|
31
|
-
args = null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
args = args || {};
|
|
35
|
-
|
|
36
|
-
// the callback style
|
|
37
|
-
if (callback) {
|
|
38
|
-
//this.app.deprecate('[httpclient] We now support async for this function, so callback isn\'t recommended.');
|
|
39
|
-
super.request(url, args)
|
|
40
|
-
.then(result => process.nextTick(() => callback(null, result.data, result.res)))
|
|
41
|
-
.catch(err => process.nextTick(() => callback(err)));
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// the Promise style
|
|
46
|
-
return super.request(url, args)
|
|
47
|
-
.catch(err => {
|
|
48
|
-
if (err.code === 'ENETUNREACH') {
|
|
49
|
-
throw HttpClientError.create(err.message, err.code);
|
|
50
|
-
}
|
|
51
|
-
throw err;
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
curl(url, args, callback) {
|
|
56
|
-
return this.request(url, args, callback);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
requestThunk(url, args) {
|
|
60
|
-
//this.app.deprecate('[httpclient] Please use `request()` instead of `requestThunk()`');
|
|
61
|
-
return callback => {
|
|
62
|
-
this.request(url, args, (err, data, res) => {
|
|
63
|
-
if (err) {
|
|
64
|
-
return callback(err);
|
|
65
|
-
}
|
|
66
|
-
callback(null, {
|
|
67
|
-
data,
|
|
68
|
-
status: res.status,
|
|
69
|
-
headers: res.headers,
|
|
70
|
-
res,
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function normalizeConfig(app) {
|
|
78
|
-
const config = app.config.httpclient;
|
|
79
|
-
|
|
80
|
-
// compatibility
|
|
81
|
-
if (typeof config.keepAlive === 'boolean') {
|
|
82
|
-
config.httpAgent.keepAlive = config.keepAlive;
|
|
83
|
-
config.httpsAgent.keepAlive = config.keepAlive;
|
|
84
|
-
}
|
|
85
|
-
if (config.timeout) {
|
|
86
|
-
config.timeout = ms(config.timeout);
|
|
87
|
-
config.httpAgent.timeout = config.timeout;
|
|
88
|
-
config.httpsAgent.timeout = config.timeout;
|
|
89
|
-
}
|
|
90
|
-
// compatibility httpclient.freeSocketKeepAliveTimeout => httpclient.freeSocketTimeout
|
|
91
|
-
if (config.freeSocketKeepAliveTimeout && !config.freeSocketTimeout) {
|
|
92
|
-
config.freeSocketTimeout = config.freeSocketKeepAliveTimeout;
|
|
93
|
-
delete config.freeSocketKeepAliveTimeout;
|
|
94
|
-
}
|
|
95
|
-
if (config.freeSocketTimeout) {
|
|
96
|
-
config.freeSocketTimeout = ms(config.freeSocketTimeout);
|
|
97
|
-
config.httpAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
98
|
-
config.httpsAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
99
|
-
} else {
|
|
100
|
-
// compatibility agent.freeSocketKeepAliveTimeout
|
|
101
|
-
if (config.httpAgent.freeSocketKeepAliveTimeout && !config.httpAgent.freeSocketTimeout) {
|
|
102
|
-
config.httpAgent.freeSocketTimeout = config.httpAgent.freeSocketKeepAliveTimeout;
|
|
103
|
-
delete config.httpAgent.freeSocketKeepAliveTimeout;
|
|
104
|
-
}
|
|
105
|
-
if (config.httpsAgent.freeSocketKeepAliveTimeout && !config.httpsAgent.freeSocketTimeout) {
|
|
106
|
-
config.httpsAgent.freeSocketTimeout = config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
107
|
-
delete config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (typeof config.maxSockets === 'number') {
|
|
112
|
-
config.httpAgent.maxSockets = config.maxSockets;
|
|
113
|
-
config.httpsAgent.maxSockets = config.maxSockets;
|
|
114
|
-
}
|
|
115
|
-
if (typeof config.maxFreeSockets === 'number') {
|
|
116
|
-
config.httpAgent.maxFreeSockets = config.maxFreeSockets;
|
|
117
|
-
config.httpsAgent.maxFreeSockets = config.maxFreeSockets;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (config.httpAgent.timeout < 30000) {
|
|
121
|
-
app.coreLogger.warn('[ee:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
122
|
-
config.httpAgent.timeout);
|
|
123
|
-
config.httpAgent.timeout = 30000;
|
|
124
|
-
}
|
|
125
|
-
if (config.httpsAgent.timeout < 30000) {
|
|
126
|
-
app.coreLogger.warn('[ee:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
127
|
-
config.httpsAgent.timeout);
|
|
128
|
-
config.httpsAgent.timeout = 30000;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (typeof config.request.timeout === 'string') {
|
|
132
|
-
config.request.timeout = ms(config.request.timeout);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
module.exports = HttpClient;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Agent = require('agentkeepalive');
|
|
4
|
+
const HttpsAgent = require('agentkeepalive').HttpsAgent;
|
|
5
|
+
const urllib = require('urllib');
|
|
6
|
+
const ms = require('humanize-ms');
|
|
7
|
+
const { FrameworkBaseError } = require('egg-errors');
|
|
8
|
+
|
|
9
|
+
class HttpClientError extends FrameworkBaseError {
|
|
10
|
+
get module() {
|
|
11
|
+
return 'httpclient';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class HttpClient extends urllib.HttpClient2 {
|
|
16
|
+
constructor(app) {
|
|
17
|
+
normalizeConfig(app);
|
|
18
|
+
const config = app.config.httpclient;
|
|
19
|
+
super({
|
|
20
|
+
app,
|
|
21
|
+
defaultArgs: config.request,
|
|
22
|
+
agent: new Agent(config.httpAgent),
|
|
23
|
+
httpsAgent: new HttpsAgent(config.httpsAgent),
|
|
24
|
+
});
|
|
25
|
+
this.app = app;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
request(url, args, callback) {
|
|
29
|
+
if (typeof args === 'function') {
|
|
30
|
+
callback = args;
|
|
31
|
+
args = null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
args = args || {};
|
|
35
|
+
|
|
36
|
+
// the callback style
|
|
37
|
+
if (callback) {
|
|
38
|
+
//this.app.deprecate('[httpclient] We now support async for this function, so callback isn\'t recommended.');
|
|
39
|
+
super.request(url, args)
|
|
40
|
+
.then(result => process.nextTick(() => callback(null, result.data, result.res)))
|
|
41
|
+
.catch(err => process.nextTick(() => callback(err)));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// the Promise style
|
|
46
|
+
return super.request(url, args)
|
|
47
|
+
.catch(err => {
|
|
48
|
+
if (err.code === 'ENETUNREACH') {
|
|
49
|
+
throw HttpClientError.create(err.message, err.code);
|
|
50
|
+
}
|
|
51
|
+
throw err;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
curl(url, args, callback) {
|
|
56
|
+
return this.request(url, args, callback);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
requestThunk(url, args) {
|
|
60
|
+
//this.app.deprecate('[httpclient] Please use `request()` instead of `requestThunk()`');
|
|
61
|
+
return callback => {
|
|
62
|
+
this.request(url, args, (err, data, res) => {
|
|
63
|
+
if (err) {
|
|
64
|
+
return callback(err);
|
|
65
|
+
}
|
|
66
|
+
callback(null, {
|
|
67
|
+
data,
|
|
68
|
+
status: res.status,
|
|
69
|
+
headers: res.headers,
|
|
70
|
+
res,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeConfig(app) {
|
|
78
|
+
const config = app.config.httpclient;
|
|
79
|
+
|
|
80
|
+
// compatibility
|
|
81
|
+
if (typeof config.keepAlive === 'boolean') {
|
|
82
|
+
config.httpAgent.keepAlive = config.keepAlive;
|
|
83
|
+
config.httpsAgent.keepAlive = config.keepAlive;
|
|
84
|
+
}
|
|
85
|
+
if (config.timeout) {
|
|
86
|
+
config.timeout = ms(config.timeout);
|
|
87
|
+
config.httpAgent.timeout = config.timeout;
|
|
88
|
+
config.httpsAgent.timeout = config.timeout;
|
|
89
|
+
}
|
|
90
|
+
// compatibility httpclient.freeSocketKeepAliveTimeout => httpclient.freeSocketTimeout
|
|
91
|
+
if (config.freeSocketKeepAliveTimeout && !config.freeSocketTimeout) {
|
|
92
|
+
config.freeSocketTimeout = config.freeSocketKeepAliveTimeout;
|
|
93
|
+
delete config.freeSocketKeepAliveTimeout;
|
|
94
|
+
}
|
|
95
|
+
if (config.freeSocketTimeout) {
|
|
96
|
+
config.freeSocketTimeout = ms(config.freeSocketTimeout);
|
|
97
|
+
config.httpAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
98
|
+
config.httpsAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
99
|
+
} else {
|
|
100
|
+
// compatibility agent.freeSocketKeepAliveTimeout
|
|
101
|
+
if (config.httpAgent.freeSocketKeepAliveTimeout && !config.httpAgent.freeSocketTimeout) {
|
|
102
|
+
config.httpAgent.freeSocketTimeout = config.httpAgent.freeSocketKeepAliveTimeout;
|
|
103
|
+
delete config.httpAgent.freeSocketKeepAliveTimeout;
|
|
104
|
+
}
|
|
105
|
+
if (config.httpsAgent.freeSocketKeepAliveTimeout && !config.httpsAgent.freeSocketTimeout) {
|
|
106
|
+
config.httpsAgent.freeSocketTimeout = config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
107
|
+
delete config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (typeof config.maxSockets === 'number') {
|
|
112
|
+
config.httpAgent.maxSockets = config.maxSockets;
|
|
113
|
+
config.httpsAgent.maxSockets = config.maxSockets;
|
|
114
|
+
}
|
|
115
|
+
if (typeof config.maxFreeSockets === 'number') {
|
|
116
|
+
config.httpAgent.maxFreeSockets = config.maxFreeSockets;
|
|
117
|
+
config.httpsAgent.maxFreeSockets = config.maxFreeSockets;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (config.httpAgent.timeout < 30000) {
|
|
121
|
+
app.coreLogger.warn('[ee:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
122
|
+
config.httpAgent.timeout);
|
|
123
|
+
config.httpAgent.timeout = 30000;
|
|
124
|
+
}
|
|
125
|
+
if (config.httpsAgent.timeout < 30000) {
|
|
126
|
+
app.coreLogger.warn('[ee:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
127
|
+
config.httpsAgent.timeout);
|
|
128
|
+
config.httpsAgent.timeout = 30000;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (typeof config.request.timeout === 'string') {
|
|
132
|
+
config.request.timeout = ms(config.request.timeout);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = HttpClient;
|
package/lib/logger.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')('ee-core:logger');
|
|
4
|
-
const Loggers = require('egg-logger').EggLoggers;
|
|
5
|
-
const assert = require('assert');
|
|
6
|
-
|
|
7
|
-
class Logger {
|
|
8
|
-
constructor (config) {
|
|
9
|
-
debug('Loaded logger');
|
|
10
|
-
assert(Object.keys(config).length != 0, `logger config is null`);
|
|
11
|
-
this.eggLogger = this.init(config);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 单例
|
|
16
|
-
*/
|
|
17
|
-
static getInstance (config = {}) {
|
|
18
|
-
if (typeof this.instance === 'object') {
|
|
19
|
-
return this.instance.eggLogger;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
this.instance = new Logger(config);
|
|
23
|
-
|
|
24
|
-
// 返回egg-logger实例
|
|
25
|
-
return this.instance.eggLogger;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 初始化模块
|
|
30
|
-
*/
|
|
31
|
-
init(config) {
|
|
32
|
-
const loggerConfig = config.logger;
|
|
33
|
-
loggerConfig.type = 'application'; // application、agent
|
|
34
|
-
|
|
35
|
-
if (config.env === 'prod' && loggerConfig.level === 'DEBUG' && !loggerConfig.allowDebugAtProd) {
|
|
36
|
-
loggerConfig.level = 'INFO';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const loggers = new Loggers(config);
|
|
40
|
-
|
|
41
|
-
loggers.coreLogger.info('[ee-core:logger] init all loggers with options: %j', loggerConfig);
|
|
42
|
-
|
|
43
|
-
return loggers;
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')('ee-core:logger');
|
|
4
|
+
const Loggers = require('egg-logger').EggLoggers;
|
|
5
|
+
const assert = require('assert');
|
|
6
|
+
|
|
7
|
+
class Logger {
|
|
8
|
+
constructor (config) {
|
|
9
|
+
debug('Loaded logger');
|
|
10
|
+
assert(Object.keys(config).length != 0, `logger config is null`);
|
|
11
|
+
this.eggLogger = this.init(config);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 单例
|
|
16
|
+
*/
|
|
17
|
+
static getInstance (config = {}) {
|
|
18
|
+
if (typeof this.instance === 'object') {
|
|
19
|
+
return this.instance.eggLogger;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
this.instance = new Logger(config);
|
|
23
|
+
|
|
24
|
+
// 返回egg-logger实例
|
|
25
|
+
return this.instance.eggLogger;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 初始化模块
|
|
30
|
+
*/
|
|
31
|
+
init(config) {
|
|
32
|
+
const loggerConfig = config.logger;
|
|
33
|
+
loggerConfig.type = 'application'; // application、agent
|
|
34
|
+
|
|
35
|
+
if (config.env === 'prod' && loggerConfig.level === 'DEBUG' && !loggerConfig.allowDebugAtProd) {
|
|
36
|
+
loggerConfig.level = 'INFO';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const loggers = new Loggers(config);
|
|
40
|
+
|
|
41
|
+
loggers.coreLogger.info('[ee-core:logger] init all loggers with options: %j', loggerConfig);
|
|
42
|
+
|
|
43
|
+
return loggers;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
47
|
module.exports = Logger;
|
package/lib/socket/httpServer.js
CHANGED
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const Koa = require('koa');
|
|
6
|
-
const cors = require('koa2-cors');
|
|
7
|
-
const koaBody = require('koa-body');
|
|
8
|
-
const https = require('https');
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
const _ = require('lodash');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* http server
|
|
15
|
-
*/
|
|
16
|
-
class HttpServer {
|
|
17
|
-
constructor (app) {
|
|
18
|
-
this.app = app;
|
|
19
|
-
this.options = this.app.config.httpServer;
|
|
20
|
-
|
|
21
|
-
if (!this.options.enable) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
let port = process.env.EE_HTTP_PORT ? parseInt(process.env.EE_HTTP_PORT) : parseInt(this.getHttpPort());
|
|
26
|
-
assert(typeof port === 'number', 'http port required, and must be a number');
|
|
27
|
-
|
|
28
|
-
this.create();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 创建服务
|
|
33
|
-
*/
|
|
34
|
-
create () {
|
|
35
|
-
const self = this;
|
|
36
|
-
const httpServer = this.options;
|
|
37
|
-
const isHttps = httpServer?.https?.enable ?? false;
|
|
38
|
-
let sslOptions = {};
|
|
39
|
-
|
|
40
|
-
if (isHttps === true) {
|
|
41
|
-
httpServer.protocol = 'https://';
|
|
42
|
-
const keyFile = path.join(this.app.config.homeDir, httpServer.https.key);
|
|
43
|
-
const certFile = path.join(this.app.config.homeDir, httpServer.https.cert);
|
|
44
|
-
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
45
|
-
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
46
|
-
|
|
47
|
-
sslOptions.key = fs.readFileSync(keyFile);
|
|
48
|
-
sslOptions.cert = fs.readFileSync(certFile);
|
|
49
|
-
}
|
|
50
|
-
const url = httpServer.protocol + httpServer.host + ':' + httpServer.port;
|
|
51
|
-
const corsOptions = httpServer.cors;
|
|
52
|
-
|
|
53
|
-
const koaApp = new Koa();
|
|
54
|
-
koaApp
|
|
55
|
-
.use(cors(corsOptions))
|
|
56
|
-
.use(koaBody(httpServer.body))
|
|
57
|
-
.use(async (ctx, next) => {
|
|
58
|
-
ctx.eeApp = self.app;
|
|
59
|
-
await next();
|
|
60
|
-
})
|
|
61
|
-
.use(this.dispatch);
|
|
62
|
-
|
|
63
|
-
let msg = '[ee-core:http:server] server is: ' + url;
|
|
64
|
-
if (isHttps) {
|
|
65
|
-
https.createServer(sslOptions, koaApp.callback()).listen(httpServer.port, (err) => {
|
|
66
|
-
msg = err ? err : msg;
|
|
67
|
-
self.app.coreLogger.info(msg);
|
|
68
|
-
});
|
|
69
|
-
} else {
|
|
70
|
-
koaApp.listen(httpServer.port, (e) => {
|
|
71
|
-
msg = e ? e : msg;
|
|
72
|
-
self.app.coreLogger.info(msg);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 路由分发
|
|
79
|
-
*/
|
|
80
|
-
async dispatch (ctx, next) {
|
|
81
|
-
const config = ctx.eeApp.config.httpServer;
|
|
82
|
-
let uriPath = ctx.request.path;
|
|
83
|
-
const method = ctx.request.method;
|
|
84
|
-
let params = ctx.request.query;
|
|
85
|
-
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
86
|
-
const body = ctx.request.body;
|
|
87
|
-
|
|
88
|
-
// 默认
|
|
89
|
-
ctx.response.status = 200;
|
|
90
|
-
|
|
91
|
-
// 添加到全局属性
|
|
92
|
-
ctx.eeApp.request = ctx.request;
|
|
93
|
-
ctx.eeApp.response = ctx.response;
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
// 找函数
|
|
97
|
-
// 去除开头的 '/'
|
|
98
|
-
if (uriPath.indexOf('/') == 0) {
|
|
99
|
-
uriPath = uriPath.substring(1);
|
|
100
|
-
}
|
|
101
|
-
// 过滤
|
|
102
|
-
if (_.includes(config.filterRequest.uris, uriPath)) {
|
|
103
|
-
ctx.response.body = config.filterRequest.returnData;
|
|
104
|
-
await next();
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
if (uriPath.slice(0, 10) != 'controller') {
|
|
108
|
-
uriPath = 'controller/' + uriPath;
|
|
109
|
-
}
|
|
110
|
-
const cmd = uriPath.split('/').join('.');
|
|
111
|
-
const args = (method == 'POST') ? body : params;
|
|
112
|
-
let fn = null;
|
|
113
|
-
if (is.string(cmd)) {
|
|
114
|
-
const actions = cmd.split('.');
|
|
115
|
-
let obj = ctx.eeApp;
|
|
116
|
-
actions.forEach(key => {
|
|
117
|
-
obj = obj[key];
|
|
118
|
-
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
119
|
-
});
|
|
120
|
-
fn = obj;
|
|
121
|
-
}
|
|
122
|
-
if (!fn) throw new Error('function not exists');
|
|
123
|
-
|
|
124
|
-
const result = await fn.call(ctx.eeApp, args);
|
|
125
|
-
ctx.response.body = result;
|
|
126
|
-
} catch (err) {
|
|
127
|
-
ctx.eeApp.console.error('[ee-core:http:server] throw error:', err);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
await next();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* 获取http端口
|
|
135
|
-
*/
|
|
136
|
-
getHttpPort () {
|
|
137
|
-
const cdb = this.getCoreDB();
|
|
138
|
-
const port = cdb.getItem('config').httpServer.port;
|
|
139
|
-
return parseInt(port);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const Koa = require('koa');
|
|
6
|
+
const cors = require('koa2-cors');
|
|
7
|
+
const koaBody = require('koa-body');
|
|
8
|
+
const https = require('https');
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const _ = require('lodash');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* http server
|
|
15
|
+
*/
|
|
16
|
+
class HttpServer {
|
|
17
|
+
constructor (app) {
|
|
18
|
+
this.app = app;
|
|
19
|
+
this.options = this.app.config.httpServer;
|
|
20
|
+
|
|
21
|
+
if (!this.options.enable) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let port = process.env.EE_HTTP_PORT ? parseInt(process.env.EE_HTTP_PORT) : parseInt(this.getHttpPort());
|
|
26
|
+
assert(typeof port === 'number', 'http port required, and must be a number');
|
|
27
|
+
|
|
28
|
+
this.create();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 创建服务
|
|
33
|
+
*/
|
|
34
|
+
create () {
|
|
35
|
+
const self = this;
|
|
36
|
+
const httpServer = this.options;
|
|
37
|
+
const isHttps = httpServer?.https?.enable ?? false;
|
|
38
|
+
let sslOptions = {};
|
|
39
|
+
|
|
40
|
+
if (isHttps === true) {
|
|
41
|
+
httpServer.protocol = 'https://';
|
|
42
|
+
const keyFile = path.join(this.app.config.homeDir, httpServer.https.key);
|
|
43
|
+
const certFile = path.join(this.app.config.homeDir, httpServer.https.cert);
|
|
44
|
+
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
45
|
+
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
46
|
+
|
|
47
|
+
sslOptions.key = fs.readFileSync(keyFile);
|
|
48
|
+
sslOptions.cert = fs.readFileSync(certFile);
|
|
49
|
+
}
|
|
50
|
+
const url = httpServer.protocol + httpServer.host + ':' + httpServer.port;
|
|
51
|
+
const corsOptions = httpServer.cors;
|
|
52
|
+
|
|
53
|
+
const koaApp = new Koa();
|
|
54
|
+
koaApp
|
|
55
|
+
.use(cors(corsOptions))
|
|
56
|
+
.use(koaBody(httpServer.body))
|
|
57
|
+
.use(async (ctx, next) => {
|
|
58
|
+
ctx.eeApp = self.app;
|
|
59
|
+
await next();
|
|
60
|
+
})
|
|
61
|
+
.use(this.dispatch);
|
|
62
|
+
|
|
63
|
+
let msg = '[ee-core:http:server] server is: ' + url;
|
|
64
|
+
if (isHttps) {
|
|
65
|
+
https.createServer(sslOptions, koaApp.callback()).listen(httpServer.port, (err) => {
|
|
66
|
+
msg = err ? err : msg;
|
|
67
|
+
self.app.coreLogger.info(msg);
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
koaApp.listen(httpServer.port, (e) => {
|
|
71
|
+
msg = e ? e : msg;
|
|
72
|
+
self.app.coreLogger.info(msg);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 路由分发
|
|
79
|
+
*/
|
|
80
|
+
async dispatch (ctx, next) {
|
|
81
|
+
const config = ctx.eeApp.config.httpServer;
|
|
82
|
+
let uriPath = ctx.request.path;
|
|
83
|
+
const method = ctx.request.method;
|
|
84
|
+
let params = ctx.request.query;
|
|
85
|
+
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
86
|
+
const body = ctx.request.body;
|
|
87
|
+
|
|
88
|
+
// 默认
|
|
89
|
+
ctx.response.status = 200;
|
|
90
|
+
|
|
91
|
+
// 添加到全局属性
|
|
92
|
+
ctx.eeApp.request = ctx.request;
|
|
93
|
+
ctx.eeApp.response = ctx.response;
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
// 找函数
|
|
97
|
+
// 去除开头的 '/'
|
|
98
|
+
if (uriPath.indexOf('/') == 0) {
|
|
99
|
+
uriPath = uriPath.substring(1);
|
|
100
|
+
}
|
|
101
|
+
// 过滤
|
|
102
|
+
if (_.includes(config.filterRequest.uris, uriPath)) {
|
|
103
|
+
ctx.response.body = config.filterRequest.returnData;
|
|
104
|
+
await next();
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
if (uriPath.slice(0, 10) != 'controller') {
|
|
108
|
+
uriPath = 'controller/' + uriPath;
|
|
109
|
+
}
|
|
110
|
+
const cmd = uriPath.split('/').join('.');
|
|
111
|
+
const args = (method == 'POST') ? body : params;
|
|
112
|
+
let fn = null;
|
|
113
|
+
if (is.string(cmd)) {
|
|
114
|
+
const actions = cmd.split('.');
|
|
115
|
+
let obj = ctx.eeApp;
|
|
116
|
+
actions.forEach(key => {
|
|
117
|
+
obj = obj[key];
|
|
118
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
119
|
+
});
|
|
120
|
+
fn = obj;
|
|
121
|
+
}
|
|
122
|
+
if (!fn) throw new Error('function not exists');
|
|
123
|
+
|
|
124
|
+
const result = await fn.call(ctx.eeApp, args);
|
|
125
|
+
ctx.response.body = result;
|
|
126
|
+
} catch (err) {
|
|
127
|
+
ctx.eeApp.console.error('[ee-core:http:server] throw error:', err);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
await next();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 获取http端口
|
|
135
|
+
*/
|
|
136
|
+
getHttpPort () {
|
|
137
|
+
const cdb = this.getCoreDB();
|
|
138
|
+
const port = cdb.getItem('config').httpServer.port;
|
|
139
|
+
return parseInt(port);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
143
|
module.exports = HttpServer;
|