mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
180
electron/api.js
Normal file → Executable file
180
electron/api.js
Normal file → Executable file
@@ -1,90 +1,90 @@
|
||||
'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 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);
|
||||
};
|
||||
|
||||
240
package.json
Normal file → Executable file
240
package.json
Normal file → Executable file
@@ -1,120 +1,120 @@
|
||||
{
|
||||
"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",
|
||||
"superagent": "^6.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user