ee-core 1.1.11 → 1.2.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/config/config.default.js +20 -2
- package/core/lib/ee.js +0 -8
- package/lib/application.js +2 -1
- package/lib/eeApp.js +20 -14
- package/lib/socket/httpServer.js +105 -0
- package/lib/socket/socketServer.js +14 -8
- package/lib/socket/start.js +4 -0
- package/package.json +4 -1
package/config/config.default.js
CHANGED
|
@@ -193,8 +193,8 @@ module.exports = appInfo => {
|
|
|
193
193
|
|
|
194
194
|
/* 内置socket服务 */
|
|
195
195
|
config.socketServer = {
|
|
196
|
-
|
|
197
|
-
|
|
196
|
+
enable: false, // 是否启用
|
|
197
|
+
port: 7070, // 默认端口(如果端口被使用,则随机获取一个)
|
|
198
198
|
path: "/socket.io/", // 路径名称
|
|
199
199
|
connectTimeout: 45000, // 客户端连接超时时间
|
|
200
200
|
pingTimeout: 30000, // 心跳检测超时时间
|
|
@@ -204,7 +204,25 @@ module.exports = appInfo => {
|
|
|
204
204
|
cors: {
|
|
205
205
|
origin: true, // http协议时,要设置跨域 类型 Boolean String RegExp Array Function
|
|
206
206
|
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/* 内置http服务 */
|
|
210
|
+
config.httpServer = {
|
|
211
|
+
enable: false, // 是否启用
|
|
212
|
+
protocol: 'http://',
|
|
213
|
+
host: '127.0.0.1',
|
|
214
|
+
port: 7071, // 默认端口(如果端口被使用,则随机获取一个)
|
|
215
|
+
cors: {
|
|
216
|
+
origin: "*"
|
|
217
|
+
}
|
|
207
218
|
};
|
|
208
219
|
|
|
220
|
+
/* 主进程加载的地址 */
|
|
221
|
+
config.mainServer = {
|
|
222
|
+
protocol: 'http://',
|
|
223
|
+
host: '127.0.0.1',
|
|
224
|
+
port: 7072, // 默认端口(如果端口被使用,则随机获取一个)
|
|
225
|
+
};
|
|
226
|
+
|
|
209
227
|
return config;
|
|
210
228
|
};
|
package/core/lib/ee.js
CHANGED
|
@@ -206,12 +206,4 @@ class EeCore extends KoaApplication {
|
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
// delegate all router method to application
|
|
210
|
-
// utils.methods.concat([ 'all', 'resources', 'register', 'redirect' ]).forEach(method => {
|
|
211
|
-
// EeCore.prototype[method] = function(...args) {
|
|
212
|
-
// this.router[method](...args);
|
|
213
|
-
// return this;
|
|
214
|
-
// };
|
|
215
|
-
// });
|
|
216
|
-
|
|
217
209
|
module.exports = EeCore;
|
package/lib/application.js
CHANGED
|
@@ -49,8 +49,9 @@ class Appliaction extends EeApp {
|
|
|
49
49
|
env.EE_USER_HOME = options.userHome;
|
|
50
50
|
env.EE_APP_DATA = options.appData;
|
|
51
51
|
env.EE_APP_USER_DATA = options.appUserData;
|
|
52
|
-
env.
|
|
52
|
+
env.EE_MAIN_PORT = null;
|
|
53
53
|
env.EE_SOCKET_PORT = null;
|
|
54
|
+
env.EE_HTTP_PORT = null;
|
|
54
55
|
env.EGG_SERVER_ENV = options.env;
|
|
55
56
|
debug('options:%j', options)
|
|
56
57
|
|
package/lib/eeApp.js
CHANGED
|
@@ -20,17 +20,23 @@ class EeApp extends BaseApp {
|
|
|
20
20
|
* 生成端口
|
|
21
21
|
*/
|
|
22
22
|
async createPorts () {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
const mainPort = await getPort({port: this.config.mainServer.port});
|
|
24
|
+
process.env.EE_MAIN_PORT = mainPort;
|
|
25
|
+
this.config.mainServer.port = mainPort;
|
|
26
|
+
|
|
27
|
+
if (this.config.socketServer.enable) {
|
|
28
|
+
const socketPort = await getPort({port: this.config.socketServer.port});
|
|
29
|
+
process.env.EE_SOCKET_PORT = socketPort;
|
|
30
|
+
this.config.socketServer.port = socketPort;
|
|
31
|
+
}
|
|
26
32
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
if (this.config.httpServer.enable) {
|
|
34
|
+
const httpPort = await getPort({port: this.config.httpServer.port});
|
|
35
|
+
process.env.EE_HTTP_PORT = httpPort;
|
|
36
|
+
this.config.httpServer.port = httpPort;
|
|
37
|
+
}
|
|
31
38
|
|
|
32
39
|
// 更新db配置
|
|
33
|
-
this.config.socketServer.port = socketPort;
|
|
34
40
|
this.getCoreDB().setItem('config', this.config);
|
|
35
41
|
}
|
|
36
42
|
|
|
@@ -190,14 +196,15 @@ class EeApp extends BaseApp {
|
|
|
190
196
|
}
|
|
191
197
|
|
|
192
198
|
const koaApp = new Koa();
|
|
193
|
-
koaApp.use(koaServe(staticDir))
|
|
194
|
-
|
|
195
|
-
|
|
199
|
+
koaApp.use(koaServe(staticDir));
|
|
200
|
+
|
|
201
|
+
const mainServer = this.config.mainServer;
|
|
202
|
+
let url = mainServer.protocol + mainServer.host + ':' + mainServer.port;
|
|
196
203
|
if (mode == 'html') {
|
|
197
204
|
url += '/' + hostInfo.indexPage;
|
|
198
205
|
}
|
|
199
206
|
|
|
200
|
-
koaApp.listen(port, () => {
|
|
207
|
+
koaApp.listen(mainServer.port, () => {
|
|
201
208
|
self.loadMainUrl(mode, url);
|
|
202
209
|
});
|
|
203
210
|
}
|
|
@@ -208,8 +215,7 @@ class EeApp extends BaseApp {
|
|
|
208
215
|
loadMainUrl (type, url) {
|
|
209
216
|
this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
|
|
210
217
|
this.electron.mainWindow.loadURL(url);
|
|
211
|
-
}
|
|
212
|
-
|
|
218
|
+
}
|
|
213
219
|
|
|
214
220
|
/**
|
|
215
221
|
* 限制一个窗口
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const Koa = require('koa');
|
|
6
|
+
const BodyParser = require('koa-bodyparser');
|
|
7
|
+
const cors = require('koa2-cors');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* http server
|
|
11
|
+
*/
|
|
12
|
+
class HttpServer {
|
|
13
|
+
constructor (app) {
|
|
14
|
+
this.app = app;
|
|
15
|
+
const options = this.app.config.httpServer;
|
|
16
|
+
|
|
17
|
+
if (!options.enable) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let port = process.env.EE_HTTP_PORT ? parseInt(process.env.EE_HTTP_PORT) : parseInt(this.getHttpPort());
|
|
22
|
+
assert(typeof port === 'number', 'http port required, and must be a number');
|
|
23
|
+
|
|
24
|
+
this.create();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 创建服务
|
|
29
|
+
*/
|
|
30
|
+
create () {
|
|
31
|
+
const self = this;
|
|
32
|
+
const httpServer = this.app.config.httpServer;
|
|
33
|
+
const url = httpServer.protocol + httpServer.host + ':' + httpServer.port;
|
|
34
|
+
const corsOptions = httpServer.cors;
|
|
35
|
+
|
|
36
|
+
const koaApp = new Koa();
|
|
37
|
+
const bodyparser= new BodyParser();
|
|
38
|
+
|
|
39
|
+
koaApp
|
|
40
|
+
.use(cors(corsOptions))
|
|
41
|
+
.use(bodyparser)
|
|
42
|
+
.use(async (ctx, next) => {
|
|
43
|
+
ctx.eeApp = self.app;
|
|
44
|
+
await next();
|
|
45
|
+
})
|
|
46
|
+
.use(this.dispatch);
|
|
47
|
+
|
|
48
|
+
koaApp.listen(httpServer.port, () => {
|
|
49
|
+
self.app.coreLogger.info('[ee-core:http:server] http server is:', url);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 路由分发
|
|
55
|
+
*/
|
|
56
|
+
async dispatch (ctx, next) {
|
|
57
|
+
let uriPath = ctx.request.path;
|
|
58
|
+
const method = ctx.request.method;
|
|
59
|
+
let params = ctx.request.query;
|
|
60
|
+
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
61
|
+
const body = ctx.request.body;
|
|
62
|
+
// 添加到全局属性
|
|
63
|
+
ctx.eeApp.request = ctx.request;
|
|
64
|
+
ctx.eeApp.response = ctx.response;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
// 找函数
|
|
68
|
+
if (uriPath.indexOf('/') !== -1) {
|
|
69
|
+
uriPath = uriPath.substring(1);
|
|
70
|
+
}
|
|
71
|
+
const cmd = uriPath.split('/').join('.');
|
|
72
|
+
const args = (method == 'POST') ? body : params;
|
|
73
|
+
let fn = null;
|
|
74
|
+
if (is.string(cmd)) {
|
|
75
|
+
const actions = cmd.split('.');
|
|
76
|
+
let obj = ctx.eeApp;
|
|
77
|
+
actions.forEach(key => {
|
|
78
|
+
obj = obj[key];
|
|
79
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
80
|
+
});
|
|
81
|
+
fn = obj;
|
|
82
|
+
}
|
|
83
|
+
if (!fn) throw new Error('function not exists');
|
|
84
|
+
|
|
85
|
+
const result = await fn.call(ctx.eeApp, args);
|
|
86
|
+
ctx.response.status = 200;
|
|
87
|
+
ctx.response.body = result;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
ctx.eeApp.console.error('[ee-core:http:server] throw error:', err);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await next();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 获取http端口
|
|
97
|
+
*/
|
|
98
|
+
getHttpPort () {
|
|
99
|
+
const cdb = this.getCoreDB();
|
|
100
|
+
const port = cdb.getItem('config').httpServer.port;
|
|
101
|
+
return parseInt(port);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = HttpServer;
|
|
@@ -11,14 +11,20 @@ const is = require('is-type-of');
|
|
|
11
11
|
*/
|
|
12
12
|
class SocketServer {
|
|
13
13
|
constructor (app) {
|
|
14
|
-
let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketcPort());
|
|
15
|
-
|
|
16
|
-
assert(typeof port === 'number', 'port required, and must be a number');
|
|
17
|
-
|
|
18
14
|
this.app = app;
|
|
19
|
-
this.consoleLogger = new EggConsoleLogger();
|
|
20
|
-
this.consoleLogger.info('[ee-core:socket:server] start server, socket port is:', port);
|
|
21
15
|
const options = this.app.config.socketServer;
|
|
16
|
+
this.consoleLogger = new EggConsoleLogger();
|
|
17
|
+
|
|
18
|
+
if (!options.enable) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketPort());
|
|
23
|
+
assert(typeof port === 'number', 'socekt port required, and must be a number');
|
|
24
|
+
this.consoleLogger.info('[ee-core:socket:server] socket server port is:', port);
|
|
25
|
+
|
|
26
|
+
// let opt = Object.assign({}, options);
|
|
27
|
+
// delete opt.enable;
|
|
22
28
|
this.io = new Server(port, options);
|
|
23
29
|
this.connec();
|
|
24
30
|
}
|
|
@@ -50,7 +56,7 @@ class SocketServer {
|
|
|
50
56
|
const result = await fn.call(self.app, args);
|
|
51
57
|
callback(result);
|
|
52
58
|
} catch (err) {
|
|
53
|
-
self.app.
|
|
59
|
+
self.app.console.error('[ee-core:socket:server] throw error:', err);
|
|
54
60
|
}
|
|
55
61
|
});
|
|
56
62
|
});
|
|
@@ -61,7 +67,7 @@ class SocketServer {
|
|
|
61
67
|
return coreDB;
|
|
62
68
|
}
|
|
63
69
|
|
|
64
|
-
|
|
70
|
+
getSocketPort () {
|
|
65
71
|
const cdb = this.getCoreDB();
|
|
66
72
|
const port = cdb.getItem('config').socketServer.port;
|
|
67
73
|
return port;
|
package/lib/socket/start.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const socketServer = require('./socketServer');
|
|
4
4
|
const ipcServer = require('./ipcServer');
|
|
5
|
+
const httpServer = require('./httpServer');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* server
|
|
@@ -11,6 +12,9 @@ const ipcServer = require('./ipcServer');
|
|
|
11
12
|
// 启动 socket server
|
|
12
13
|
new socketServer(app);
|
|
13
14
|
|
|
15
|
+
// 启动 http server
|
|
16
|
+
new httpServer(app);
|
|
17
|
+
|
|
14
18
|
// 启动 electron ipc server
|
|
15
19
|
new ipcServer(app);
|
|
16
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ee-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "ee core",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,10 +27,13 @@
|
|
|
27
27
|
"humanize-ms": "^1.2.1",
|
|
28
28
|
"is-type-of": "^1.2.1",
|
|
29
29
|
"koa": "^2.13.4",
|
|
30
|
+
"koa-bodyparser": "^4.3.0",
|
|
30
31
|
"koa-convert": "^2.0.0",
|
|
31
32
|
"koa-static": "^5.0.0",
|
|
33
|
+
"koa2-cors": "^2.0.6",
|
|
32
34
|
"lodash": "^4.17.21",
|
|
33
35
|
"lowdb": "^1.0.0",
|
|
36
|
+
"path-to-regexp": "^6.2.0",
|
|
34
37
|
"socket.io": "^4.4.1",
|
|
35
38
|
"socket.io-client": "^4.4.1",
|
|
36
39
|
"uglify-js": "^3.14.5",
|