mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-06-10 03:07:32 +08:00
Merge branch 'mini'
# Conflicts: # electron/api.js # package.json
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const request = require('superagent');
|
||||
const Service = require('egg').Service;
|
||||
|
||||
class BaseService extends Service {
|
||||
|
||||
/*
|
||||
* ipc call
|
||||
*/
|
||||
@@ -17,14 +17,8 @@ class BaseService extends Service {
|
||||
return result;
|
||||
}
|
||||
|
||||
const port = this.service.storage.getElectronIPCPort();
|
||||
const url = 'localhost:' + port + '/send';
|
||||
try {
|
||||
const response = await request.post(url)
|
||||
.send({ cmd: method, params: params })
|
||||
.set('accept', 'json');
|
||||
|
||||
result = JSON.parse(response.text);
|
||||
result = await this.service.socket.call(method, params);
|
||||
} catch (err) {
|
||||
this.app.logger.error('[base] [ipcCall] request error:', err);
|
||||
result.err = 'request err';
|
||||
@@ -33,6 +27,36 @@ class BaseService extends Service {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* ipc call
|
||||
*/
|
||||
// async ipcCall(method = '', ...params) {
|
||||
// let result = {
|
||||
// err: null,
|
||||
// data: null
|
||||
// };
|
||||
// if (!method) {
|
||||
// result.err = 'Method does not exist';
|
||||
// return result;
|
||||
// }
|
||||
|
||||
// const port = this.service.storage.getElectronIPCPort();
|
||||
// const url = 'http://localhost:' + port + '/send';
|
||||
// try {
|
||||
// const response = await request.post(url)
|
||||
// .send({ cmd: method, params: params })
|
||||
// .set('accept', 'json');
|
||||
|
||||
// result = JSON.parse(response.text);
|
||||
// } catch (err) {
|
||||
// this.app.logger.error('[base] [ipcCall] request error:', err);
|
||||
// result.err = 'request err';
|
||||
// }
|
||||
// this.app.logger.info('[base] [ipcCall] result:', result);
|
||||
|
||||
// return result;
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = BaseService;
|
||||
|
||||
30
app/service/socket.js
Normal file
30
app/service/socket.js
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
const BaseService = require('./base');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
this.instance = null;
|
||||
|
||||
class SocketService extends BaseService {
|
||||
|
||||
instance() {
|
||||
if (!SocketService.instance) {
|
||||
const port = this.service.storage.getElectronIPCPort();
|
||||
const url = 'http://localhost:' + port;
|
||||
const instance = io(url);
|
||||
SocketService.instance = instance;
|
||||
}
|
||||
return SocketService.instance;
|
||||
}
|
||||
|
||||
call (method = '', params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.instance().emit('ipc', { cmd: method, params: params }, (response) => {
|
||||
resolve(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = SocketService;
|
||||
195
electron/api.js
195
electron/api.js
@@ -1,90 +1,105 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
const storage = require('./storage');
|
||||
|
||||
const apis = {};
|
||||
|
||||
exports.setup = async function () {
|
||||
setApi();
|
||||
|
||||
// use api server
|
||||
let port = await storage.setIpcDynamicPort();
|
||||
ELog.info('[api] [setup] dynamic port:', port);
|
||||
const listen = 'localhost';
|
||||
port = port ? port : 7069;
|
||||
|
||||
const server = http.createServer(function(req, res) {
|
||||
ELog.info('[ api ] [setup] command received', { method: req.method, url: req.url });
|
||||
if ((req.method === 'POST' && req.url === '/send')) {
|
||||
let body = '';
|
||||
req.setEncoding('utf8');
|
||||
req
|
||||
.on('data', function(data) {
|
||||
body += data;
|
||||
})
|
||||
.on('end', function() {
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(body);
|
||||
} catch (e) {
|
||||
res.statusCode = 400;
|
||||
return res.end('request body parse failure.');
|
||||
}
|
||||
if (!apis[message.cmd]) {
|
||||
ELog.info('[ api ] [setup] invalid command called:', message.cmd);
|
||||
res.statusCode = 404;
|
||||
return res.end('NG');
|
||||
}
|
||||
|
||||
ELog.info('[ api ] [setup] command received message:', message);
|
||||
const start = Date.now();
|
||||
const data = apis[message.cmd](...message.params);
|
||||
const elapsed = Date.now() - start;
|
||||
ELog.info(`[api] [setup] [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
res.statusCode = 200;
|
||||
const result = {
|
||||
err: null,
|
||||
data: data,
|
||||
};
|
||||
res.end(JSON.stringify(result));
|
||||
});
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
res.end('NOT FOUND');
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(port, listen, function() {
|
||||
ELog.info('[ api ] [setup] server is listening on', `${listen}:${port}`);
|
||||
});
|
||||
};
|
||||
|
||||
function setApi() {
|
||||
const apiDir = path.normalize(__dirname + '/apis');
|
||||
// console.log('apiDir:', apiDir);
|
||||
fs.readdirSync(apiDir).forEach(function(filename) {
|
||||
if (path.extname(filename) === '.js' && filename !== 'index.js') {
|
||||
const name = path.basename(filename, '.js');
|
||||
const fileObj = require(`./apis/${filename}`);
|
||||
_.map(fileObj, function(fn, method) {
|
||||
let methodName = getApiName(name, method);
|
||||
apis[methodName] = fn;
|
||||
// ELog.info('[ Monitor ]', methodName);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* get api method name
|
||||
* ex.) jsname='user' method='get' => 'user.get'
|
||||
* @param {String} jsname
|
||||
* @param {String} method
|
||||
*/
|
||||
function getApiName (jsname, method) {
|
||||
return jsname + '.' + method;
|
||||
//return jsname + method.charAt(0).toUpperCase() + method.slice(1);
|
||||
};
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
const storage = require('./storage');
|
||||
const socketIo = require('socket.io');
|
||||
|
||||
const apis = {};
|
||||
|
||||
exports.setup = async function () {
|
||||
setApi();
|
||||
|
||||
// use api server
|
||||
let port = await storage.setIpcDynamicPort();
|
||||
ELog.info('[api] [setup] dynamic port:', port);
|
||||
const listen = 'localhost';
|
||||
port = port ? port : 7069;
|
||||
|
||||
const server = http.createServer(function(req, res) {
|
||||
ELog.info('[ api ] [setup] command received', { method: req.method, url: req.url });
|
||||
if ((req.method === 'POST' && req.url === '/send')) {
|
||||
let body = '';
|
||||
req.setEncoding('utf8');
|
||||
req
|
||||
.on('data', function(data) {
|
||||
body += data;
|
||||
})
|
||||
.on('end', function() {
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(body);
|
||||
} catch (e) {
|
||||
res.statusCode = 400;
|
||||
return res.end('request body parse failure.');
|
||||
}
|
||||
if (!apis[message.cmd]) {
|
||||
ELog.info('[ api ] [setup] invalid command called:', message.cmd);
|
||||
res.statusCode = 404;
|
||||
return res.end('NG');
|
||||
}
|
||||
|
||||
ELog.info('[ api ] [setup] command received message:', message);
|
||||
const start = Date.now();
|
||||
const data = apis[message.cmd](...message.params);
|
||||
const elapsed = Date.now() - start;
|
||||
ELog.info(`[api] [setup] [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
res.statusCode = 200;
|
||||
const result = {
|
||||
err: null,
|
||||
data: data,
|
||||
};
|
||||
res.end(JSON.stringify(result));
|
||||
});
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
res.end('NOT FOUND');
|
||||
}
|
||||
});
|
||||
|
||||
// socket io
|
||||
const io = socketIo(server);
|
||||
io.on('connection', (socket) => {
|
||||
socket.on('ipc', (message, callback) => {
|
||||
ELog.info('[ api ] [setup] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
||||
const data = apis[message.cmd](...message.params);
|
||||
const result = {
|
||||
err: null,
|
||||
data: data,
|
||||
};
|
||||
callback(result);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, listen, function() {
|
||||
ELog.info('[ api ] [setup] server is listening on', `${listen}:${port}`);
|
||||
});
|
||||
};
|
||||
|
||||
function setApi() {
|
||||
const apiDir = path.normalize(__dirname + '/apis');
|
||||
// console.log('apiDir:', apiDir);
|
||||
fs.readdirSync(apiDir).forEach(function(filename) {
|
||||
if (path.extname(filename) === '.js' && filename !== 'index.js') {
|
||||
const name = path.basename(filename, '.js');
|
||||
const fileObj = require(`./apis/${filename}`);
|
||||
_.map(fileObj, function(fn, method) {
|
||||
let methodName = getApiName(name, method);
|
||||
apis[methodName] = fn;
|
||||
// ELog.info('[ Monitor ]', methodName);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* get api method name
|
||||
* ex.) jsname='user' method='get' => 'user.get'
|
||||
* @param {String} jsname
|
||||
* @param {String} method
|
||||
*/
|
||||
function getApiName (jsname, method) {
|
||||
return jsname + '.' + method;
|
||||
//return jsname + method.charAt(0).toUpperCase() + method.slice(1);
|
||||
};
|
||||
|
||||
@@ -21,7 +21,6 @@ class AutoLaunch {
|
||||
|
||||
isEnabled () {
|
||||
const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin;
|
||||
console.log('AutoLaunch isEnabled:', enabled);
|
||||
return enabled;
|
||||
}
|
||||
}
|
||||
|
||||
241
package.json
241
package.json
@@ -1,120 +1,121 @@
|
||||
{
|
||||
"name": "electron-egg",
|
||||
"version": "1.5.0",
|
||||
"description": "A fast, desktop software development framework",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"dev": "electron . --env=local",
|
||||
"build-w": "electron-builder -w",
|
||||
"build-m": "electron-builder -m",
|
||||
"build-l": "electron-builder -l",
|
||||
"web-start": "egg-scripts start --daemon --title=electron-egg --ignore-stderr --env=prod --workers=1",
|
||||
"web-stop": "egg-scripts stop --title=electron-egg",
|
||||
"web-dev": "egg-bin dev"
|
||||
},
|
||||
"build": {
|
||||
"productName": "electron-egg",
|
||||
"appId": "com.electron.egg",
|
||||
"copyright": "wallace5303",
|
||||
"directories": {
|
||||
"output": "out"
|
||||
},
|
||||
"asar": true,
|
||||
"files": [
|
||||
"**/*"
|
||||
],
|
||||
"electronDownload": {
|
||||
"mirror": "https://npm.taobao.org/mirrors/electron/"
|
||||
},
|
||||
"nsis": {
|
||||
"oneClick": false,
|
||||
"allowElevation": true,
|
||||
"allowToChangeInstallationDirectory": true,
|
||||
"installerIcon": "./build/icons/icon.ico",
|
||||
"uninstallerIcon": "./build/icons/icon.ico",
|
||||
"installerHeaderIcon": "./build/icons/icon.ico",
|
||||
"createDesktopShortcut": true,
|
||||
"createStartMenuShortcut": true,
|
||||
"shortcutName": "demo"
|
||||
},
|
||||
"publish": [
|
||||
{
|
||||
"provider": "generic",
|
||||
"url": "https://github.com/wallace5303/electron-egg"
|
||||
}
|
||||
],
|
||||
"dmg": {
|
||||
"contents": [
|
||||
{
|
||||
"x": 410,
|
||||
"y": 150,
|
||||
"type": "link",
|
||||
"path": "/Applications"
|
||||
},
|
||||
{
|
||||
"x": 130,
|
||||
"y": 150,
|
||||
"type": "file"
|
||||
}
|
||||
]
|
||||
},
|
||||
"mac": {
|
||||
"icon": "build/icons/icon.icns"
|
||||
},
|
||||
"win": {
|
||||
"icon": "build/icons/icon.ico",
|
||||
"artifactName": "${productName}_windows_${version}.${ext}",
|
||||
"target": [
|
||||
{
|
||||
"target": "nsis",
|
||||
"arch": [
|
||||
"ia32"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"linux": {
|
||||
"icon": "build/icons"
|
||||
}
|
||||
},
|
||||
"repository": "https://github.com/wallace5303/electron-egg.git",
|
||||
"keywords": [
|
||||
"Electron",
|
||||
"Egg"
|
||||
],
|
||||
"author": "wallace5303",
|
||||
"license": "Apache",
|
||||
"devDependencies": {
|
||||
"devtron": "^1.4.0",
|
||||
"electron": "^8.4.1",
|
||||
"electron-builder": "^22.7.0",
|
||||
"autod": "^3.0.1",
|
||||
"autod-egg": "^1.1.0",
|
||||
"egg-bin": "^4.12.3",
|
||||
"egg-ci": "^1.11.0",
|
||||
"egg-mock": "^3.21.0",
|
||||
"eslint": "^5.13.0",
|
||||
"eslint-config-egg": "^7.1.0",
|
||||
"eslint-plugin-prettier": "^3.0.1",
|
||||
"prettier": "^1.16.4",
|
||||
"webstorm-disable-index": "^1.2.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"dayjs": "^1.9.5",
|
||||
"egg": "^2.15.1",
|
||||
"egg-cors": "^2.2.0",
|
||||
"egg-jwt": "^3.1.6",
|
||||
"egg-scripts": "^2.13.0",
|
||||
"egg-view-ejs": "^2.0.0",
|
||||
"electron-is": "^3.0.0",
|
||||
"electron-log": "^4.2.2",
|
||||
"electron-updater": "^4.3.5",
|
||||
"get-port": "^5.1.1",
|
||||
"glob": "^7.1.6",
|
||||
"lodash": "^4.17.11",
|
||||
"lowdb": "^1.0.0",
|
||||
"semver": "^5.4.1",
|
||||
"superagent": "^6.1.0"
|
||||
}
|
||||
}
|
||||
{
|
||||
"name": "electron-egg",
|
||||
"version": "1.5.0",
|
||||
"description": "A fast, desktop software development framework",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"dev": "electron . --env=local",
|
||||
"build-w": "electron-builder -w",
|
||||
"build-m": "electron-builder -m",
|
||||
"build-l": "electron-builder -l",
|
||||
"web-start": "egg-scripts start --daemon --title=electron-egg --ignore-stderr --env=prod --workers=1",
|
||||
"web-stop": "egg-scripts stop --title=electron-egg",
|
||||
"web-dev": "egg-bin dev"
|
||||
},
|
||||
"build": {
|
||||
"productName": "electron-egg",
|
||||
"appId": "com.electron.egg",
|
||||
"copyright": "wallace5303",
|
||||
"directories": {
|
||||
"output": "out"
|
||||
},
|
||||
"asar": true,
|
||||
"files": [
|
||||
"**/*"
|
||||
],
|
||||
"electronDownload": {
|
||||
"mirror": "https://npm.taobao.org/mirrors/electron/"
|
||||
},
|
||||
"nsis": {
|
||||
"oneClick": false,
|
||||
"allowElevation": true,
|
||||
"allowToChangeInstallationDirectory": true,
|
||||
"installerIcon": "./build/icons/icon.ico",
|
||||
"uninstallerIcon": "./build/icons/icon.ico",
|
||||
"installerHeaderIcon": "./build/icons/icon.ico",
|
||||
"createDesktopShortcut": true,
|
||||
"createStartMenuShortcut": true,
|
||||
"shortcutName": "demo"
|
||||
},
|
||||
"publish": [
|
||||
{
|
||||
"provider": "generic",
|
||||
"url": "https://github.com/wallace5303/electron-egg"
|
||||
}
|
||||
],
|
||||
"dmg": {
|
||||
"contents": [
|
||||
{
|
||||
"x": 410,
|
||||
"y": 150,
|
||||
"type": "link",
|
||||
"path": "/Applications"
|
||||
},
|
||||
{
|
||||
"x": 130,
|
||||
"y": 150,
|
||||
"type": "file"
|
||||
}
|
||||
]
|
||||
},
|
||||
"mac": {
|
||||
"icon": "build/icons/icon.icns"
|
||||
},
|
||||
"win": {
|
||||
"icon": "build/icons/icon.ico",
|
||||
"artifactName": "${productName}_windows_${version}.${ext}",
|
||||
"target": [
|
||||
{
|
||||
"target": "nsis",
|
||||
"arch": [
|
||||
"ia32"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"linux": {
|
||||
"icon": "build/icons"
|
||||
}
|
||||
},
|
||||
"repository": "https://github.com/wallace5303/electron-egg.git",
|
||||
"keywords": [
|
||||
"Electron",
|
||||
"Egg"
|
||||
],
|
||||
"author": "wallace5303",
|
||||
"license": "Apache",
|
||||
"devDependencies": {
|
||||
"devtron": "^1.4.0",
|
||||
"electron": "^8.4.1",
|
||||
"electron-builder": "^22.7.0",
|
||||
"autod": "^3.0.1",
|
||||
"autod-egg": "^1.1.0",
|
||||
"egg-bin": "^4.12.3",
|
||||
"egg-ci": "^1.11.0",
|
||||
"egg-mock": "^3.21.0",
|
||||
"eslint": "^5.13.0",
|
||||
"eslint-config-egg": "^7.1.0",
|
||||
"eslint-plugin-prettier": "^3.0.1",
|
||||
"prettier": "^1.16.4",
|
||||
"webstorm-disable-index": "^1.2.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"dayjs": "^1.9.5",
|
||||
"egg": "^2.15.1",
|
||||
"egg-cors": "^2.2.0",
|
||||
"egg-jwt": "^3.1.6",
|
||||
"egg-scripts": "^2.13.0",
|
||||
"egg-view-ejs": "^2.0.0",
|
||||
"electron-is": "^3.0.0",
|
||||
"electron-log": "^4.2.2",
|
||||
"electron-updater": "^4.3.5",
|
||||
"get-port": "^5.1.1",
|
||||
"glob": "^7.1.6",
|
||||
"lodash": "^4.17.11",
|
||||
"lowdb": "^1.0.0",
|
||||
"semver": "^5.4.1",
|
||||
"socket.io": "^3.0.5",
|
||||
"socket.io-client": "^3.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user