ee-core 1.1.10 → 1.2.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/config/config.default.js +20 -2
- package/core/lib/ee.js +0 -8
- package/lib/application.js +2 -1
- package/lib/eeApp.js +35 -17
- package/lib/socket/httpServer.js +105 -0
- package/lib/socket/socketServer.js +14 -8
- package/lib/socket/start.js +4 -0
- package/package.json +7 -4
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
|
@@ -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,7 +56,12 @@ 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
67
|
if (self.electron.mainWindow) {
|
|
@@ -73,6 +87,10 @@ class EeApp extends BaseApp {
|
|
|
73
87
|
self.appQuit();
|
|
74
88
|
}
|
|
75
89
|
})
|
|
90
|
+
|
|
91
|
+
app.on('before-quit', () => {
|
|
92
|
+
self.electron.extra.closeWindow = true;
|
|
93
|
+
})
|
|
76
94
|
}
|
|
77
95
|
|
|
78
96
|
/**
|
|
@@ -190,14 +208,15 @@ class EeApp extends BaseApp {
|
|
|
190
208
|
}
|
|
191
209
|
|
|
192
210
|
const koaApp = new Koa();
|
|
193
|
-
koaApp.use(koaServe(staticDir))
|
|
194
|
-
|
|
195
|
-
|
|
211
|
+
koaApp.use(koaServe(staticDir));
|
|
212
|
+
|
|
213
|
+
const mainServer = this.config.mainServer;
|
|
214
|
+
let url = mainServer.protocol + mainServer.host + ':' + mainServer.port;
|
|
196
215
|
if (mode == 'html') {
|
|
197
216
|
url += '/' + hostInfo.indexPage;
|
|
198
217
|
}
|
|
199
218
|
|
|
200
|
-
koaApp.listen(port, () => {
|
|
219
|
+
koaApp.listen(mainServer.port, () => {
|
|
201
220
|
self.loadMainUrl(mode, url);
|
|
202
221
|
});
|
|
203
222
|
}
|
|
@@ -208,8 +227,7 @@ class EeApp extends BaseApp {
|
|
|
208
227
|
loadMainUrl (type, url) {
|
|
209
228
|
this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
|
|
210
229
|
this.electron.mainWindow.loadURL(url);
|
|
211
|
-
}
|
|
212
|
-
|
|
230
|
+
}
|
|
213
231
|
|
|
214
232
|
/**
|
|
215
233
|
* 限制一个窗口
|
|
@@ -228,7 +246,7 @@ class EeApp extends BaseApp {
|
|
|
228
246
|
await this.beforeClose();
|
|
229
247
|
|
|
230
248
|
// 窗口销毁
|
|
231
|
-
this.electron.mainWindow.
|
|
249
|
+
this.electron.mainWindow.close();
|
|
232
250
|
|
|
233
251
|
//console.log('Exit now!');
|
|
234
252
|
// 托盘销毁
|
|
@@ -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.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "ee core",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"agentkeepalive": "^4.2.0",
|
|
16
16
|
"co": "^4.6.0",
|
|
17
|
+
"debug": "^4.3.3",
|
|
18
|
+
"depd": "^2.0.0",
|
|
17
19
|
"egg-errors": "^2.3.0",
|
|
18
20
|
"egg-logger": "^2.7.1",
|
|
19
21
|
"electron-is": "^3.0.0",
|
|
@@ -25,10 +27,13 @@
|
|
|
25
27
|
"humanize-ms": "^1.2.1",
|
|
26
28
|
"is-type-of": "^1.2.1",
|
|
27
29
|
"koa": "^2.13.4",
|
|
30
|
+
"koa-bodyparser": "^4.3.0",
|
|
28
31
|
"koa-convert": "^2.0.0",
|
|
29
32
|
"koa-static": "^5.0.0",
|
|
33
|
+
"koa2-cors": "^2.0.6",
|
|
30
34
|
"lodash": "^4.17.21",
|
|
31
35
|
"lowdb": "^1.0.0",
|
|
36
|
+
"path-to-regexp": "^6.2.0",
|
|
32
37
|
"socket.io": "^4.4.1",
|
|
33
38
|
"socket.io-client": "^4.4.1",
|
|
34
39
|
"uglify-js": "^3.14.5",
|
|
@@ -36,7 +41,5 @@
|
|
|
36
41
|
"urllib": "^2.38.0",
|
|
37
42
|
"utility": "^1.17.0"
|
|
38
43
|
},
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"debug": "^4.3.3"
|
|
41
|
-
}
|
|
44
|
+
"devDependencies": {}
|
|
42
45
|
}
|