ee-core 1.1.11 → 1.2.2
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/baseApp.js +0 -24
- package/lib/eeApp.js +57 -35
- 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/tools/codeCompress.js +3 -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/baseApp.js
CHANGED
|
@@ -108,30 +108,6 @@ class BaseApp extends EeAppCore {
|
|
|
108
108
|
return this[HTTPCLIENT];
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
/**
|
|
112
|
-
* 调用 egg api
|
|
113
|
-
*/
|
|
114
|
-
// async curlEgg (method, uri, params, timeout = 15000) {
|
|
115
|
-
// let result = null;
|
|
116
|
-
// try {
|
|
117
|
-
// const port = this.config.egg.port;
|
|
118
|
-
// const url = "http://127.0.0.1:" + port + uri;
|
|
119
|
-
// // console.log('[ee:baseApp] [curlEgg] url:', url);
|
|
120
|
-
// const response = await this.curl(url, {
|
|
121
|
-
// method: method,
|
|
122
|
-
// contentType: 'application/json',
|
|
123
|
-
// data: params,
|
|
124
|
-
// dataType: 'json',
|
|
125
|
-
// timeout: timeout,
|
|
126
|
-
// });
|
|
127
|
-
// result = response.data;
|
|
128
|
-
// } catch (err) {
|
|
129
|
-
// this.logger.error('[ee:baseApp] [curlEgg] throw error:', err);
|
|
130
|
-
// }
|
|
131
|
-
|
|
132
|
-
// return result;
|
|
133
|
-
// }
|
|
134
|
-
|
|
135
111
|
/**
|
|
136
112
|
* core app have been loaded
|
|
137
113
|
*/
|
package/lib/eeApp.js
CHANGED
|
@@ -12,7 +12,10 @@ class EeApp extends BaseApp {
|
|
|
12
12
|
|
|
13
13
|
this.electron = {
|
|
14
14
|
mainWindow: null,
|
|
15
|
-
tray: null
|
|
15
|
+
tray: null,
|
|
16
|
+
extra: {
|
|
17
|
+
closeWindow: false,
|
|
18
|
+
}
|
|
16
19
|
};
|
|
17
20
|
}
|
|
18
21
|
|
|
@@ -20,17 +23,23 @@ class EeApp extends BaseApp {
|
|
|
20
23
|
* 生成端口
|
|
21
24
|
*/
|
|
22
25
|
async createPorts () {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
const mainPort = await getPort({port: this.config.mainServer.port});
|
|
27
|
+
process.env.EE_MAIN_PORT = mainPort;
|
|
28
|
+
this.config.mainServer.port = mainPort;
|
|
29
|
+
|
|
30
|
+
if (this.config.socketServer.enable) {
|
|
31
|
+
const socketPort = await getPort({port: this.config.socketServer.port});
|
|
32
|
+
process.env.EE_SOCKET_PORT = socketPort;
|
|
33
|
+
this.config.socketServer.port = socketPort;
|
|
34
|
+
}
|
|
26
35
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
if (this.config.httpServer.enable) {
|
|
37
|
+
const httpPort = await getPort({port: this.config.httpServer.port});
|
|
38
|
+
process.env.EE_HTTP_PORT = httpPort;
|
|
39
|
+
this.config.httpServer.port = httpPort;
|
|
40
|
+
}
|
|
31
41
|
|
|
32
42
|
// 更新db配置
|
|
33
|
-
this.config.socketServer.port = socketPort;
|
|
34
43
|
this.getCoreDB().setItem('config', this.config);
|
|
35
44
|
}
|
|
36
45
|
|
|
@@ -47,23 +56,21 @@ class EeApp extends BaseApp {
|
|
|
47
56
|
*/
|
|
48
57
|
async createElectronApp () {
|
|
49
58
|
const self = this;
|
|
50
|
-
|
|
59
|
+
|
|
60
|
+
const gotTheLock = app.requestSingleInstanceLock();
|
|
61
|
+
if (!gotTheLock) {
|
|
62
|
+
await this.appQuit();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
51
65
|
|
|
52
66
|
app.on('second-instance', (event) => {
|
|
53
|
-
|
|
54
|
-
if (self.electron.mainWindow.isMinimized()) {
|
|
55
|
-
self.electron.mainWindow.restore();
|
|
56
|
-
}
|
|
57
|
-
self.electron.mainWindow.focus()
|
|
58
|
-
}
|
|
67
|
+
self.restoreMainWindow();
|
|
59
68
|
})
|
|
60
69
|
|
|
61
70
|
app.whenReady().then(() => {
|
|
62
71
|
self.createWindow();
|
|
63
72
|
app.on('activate', function () {
|
|
64
|
-
|
|
65
|
-
self.createWindow();
|
|
66
|
-
}
|
|
73
|
+
self.restoreMainWindow();
|
|
67
74
|
})
|
|
68
75
|
})
|
|
69
76
|
|
|
@@ -73,15 +80,18 @@ class EeApp extends BaseApp {
|
|
|
73
80
|
self.appQuit();
|
|
74
81
|
}
|
|
75
82
|
})
|
|
83
|
+
|
|
84
|
+
app.on('before-quit', () => {
|
|
85
|
+
self.electron.extra.closeWindow = true;
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
await this.electronAppReady();
|
|
76
89
|
}
|
|
77
90
|
|
|
78
91
|
/**
|
|
79
92
|
* 创建应用主窗口
|
|
80
93
|
*/
|
|
81
94
|
async createWindow () {
|
|
82
|
-
|
|
83
|
-
await this.electronAppReady();
|
|
84
|
-
|
|
85
95
|
const winOptions = this.config.windowsOption;
|
|
86
96
|
this.electron.mainWindow = new BrowserWindow(winOptions);
|
|
87
97
|
|
|
@@ -104,6 +114,18 @@ class EeApp extends BaseApp {
|
|
|
104
114
|
}
|
|
105
115
|
}
|
|
106
116
|
|
|
117
|
+
/**
|
|
118
|
+
* 还原窗口
|
|
119
|
+
*/
|
|
120
|
+
restoreMainWindow () {
|
|
121
|
+
if (this.electron.mainWindow) {
|
|
122
|
+
if (this.electron.mainWindow.isMinimized()) {
|
|
123
|
+
this.electron.mainWindow.restore();
|
|
124
|
+
}
|
|
125
|
+
this.electron.mainWindow.show()
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
107
129
|
/**
|
|
108
130
|
* 加载已经实现的功能
|
|
109
131
|
*/
|
|
@@ -190,14 +212,15 @@ class EeApp extends BaseApp {
|
|
|
190
212
|
}
|
|
191
213
|
|
|
192
214
|
const koaApp = new Koa();
|
|
193
|
-
koaApp.use(koaServe(staticDir))
|
|
194
|
-
|
|
195
|
-
|
|
215
|
+
koaApp.use(koaServe(staticDir));
|
|
216
|
+
|
|
217
|
+
const mainServer = this.config.mainServer;
|
|
218
|
+
let url = mainServer.protocol + mainServer.host + ':' + mainServer.port;
|
|
196
219
|
if (mode == 'html') {
|
|
197
220
|
url += '/' + hostInfo.indexPage;
|
|
198
221
|
}
|
|
199
222
|
|
|
200
|
-
koaApp.listen(port, () => {
|
|
223
|
+
koaApp.listen(mainServer.port, () => {
|
|
201
224
|
self.loadMainUrl(mode, url);
|
|
202
225
|
});
|
|
203
226
|
}
|
|
@@ -208,18 +231,17 @@ class EeApp extends BaseApp {
|
|
|
208
231
|
loadMainUrl (type, url) {
|
|
209
232
|
this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
|
|
210
233
|
this.electron.mainWindow.loadURL(url);
|
|
211
|
-
}
|
|
212
|
-
|
|
234
|
+
}
|
|
213
235
|
|
|
214
236
|
/**
|
|
215
237
|
* 限制一个窗口
|
|
216
238
|
*/
|
|
217
|
-
async limitOneWindow () {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
239
|
+
// async limitOneWindow () {
|
|
240
|
+
// const gotTheLock = app.requestSingleInstanceLock();
|
|
241
|
+
// if (!gotTheLock) {
|
|
242
|
+
// await this.appQuit();
|
|
243
|
+
// }
|
|
244
|
+
// }
|
|
223
245
|
|
|
224
246
|
/**
|
|
225
247
|
* electron app退出
|
|
@@ -228,7 +250,7 @@ class EeApp extends BaseApp {
|
|
|
228
250
|
await this.beforeClose();
|
|
229
251
|
|
|
230
252
|
// 窗口销毁
|
|
231
|
-
this.electron.mainWindow.
|
|
253
|
+
//this.electron.mainWindow.close();
|
|
232
254
|
|
|
233
255
|
//console.log('Exit now!');
|
|
234
256
|
// 托盘销毁
|
|
@@ -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.2",
|
|
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",
|
package/tools/codeCompress.js
CHANGED
|
@@ -128,7 +128,9 @@ class CodeCompress {
|
|
|
128
128
|
* 移除备份
|
|
129
129
|
*/
|
|
130
130
|
rmBackup () {
|
|
131
|
-
fs.
|
|
131
|
+
if (fs.existsSync(this.backupCodeDir)) {
|
|
132
|
+
fs.rmSync(this.backupCodeDir, {recursive: true, force: true});
|
|
133
|
+
}
|
|
132
134
|
return;
|
|
133
135
|
}
|
|
134
136
|
|