mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 11:52:07 +08:00
electron egg api
This commit is contained in:
@@ -3,23 +3,24 @@ const fs = require('fs');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
const storage = require('./storage');
|
||||
|
||||
|
||||
const apis = {};
|
||||
|
||||
exports.setup = function () {
|
||||
exports.setup = async function () {
|
||||
ELog.info('[api] [setup]');
|
||||
setApi();
|
||||
|
||||
// use monitor server
|
||||
const mConfig = {};
|
||||
const listen = mConfig.listen || '0.0.0.0';
|
||||
const port = 7069;
|
||||
const timeout = 10000;
|
||||
// use api server
|
||||
let port = await storage.setApiDynamicPort();
|
||||
console.log('api port:', port);
|
||||
const listen = 'localhost';
|
||||
port = port ? port : 7069;
|
||||
|
||||
const server = http.createServer(function(req, res) {
|
||||
ELog.info('[ Monitor ] command received', { method: req.method, url: req.url });
|
||||
ELog.info('[ api ] command received', { method: req.method, url: req.url });
|
||||
if ((req.method === 'POST' && req.url === '/send')) {
|
||||
|
||||
let body = '';
|
||||
req.setEncoding('utf8');
|
||||
req
|
||||
@@ -35,7 +36,7 @@ exports.setup = function () {
|
||||
return res.end('request body parse failure.');
|
||||
}
|
||||
if (!apis[message.cmd]) {
|
||||
ELog.info('[ Monitor ] invalid command called:', message.cmd);
|
||||
ELog.info('[ api ] invalid command called:', message.cmd);
|
||||
res.statusCode = 404;
|
||||
return res.end('NG');
|
||||
}
|
||||
@@ -43,7 +44,7 @@ exports.setup = function () {
|
||||
const start = Date.now();
|
||||
const data = apis[message.cmd]();
|
||||
const elapsed = Date.now() - start;
|
||||
ELog.info(`monitor api [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
ELog.info(`api [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
res.statusCode = 200;
|
||||
const result = {
|
||||
code: 0,
|
||||
@@ -58,21 +59,21 @@ exports.setup = function () {
|
||||
});
|
||||
|
||||
server.listen(port, listen, function() {
|
||||
ELog.info('[ Monitor ] server is listening on', `${listen}:${port}`);
|
||||
ELog.info('[ api ] server is listening on', `${listen}:${port}`);
|
||||
});
|
||||
};
|
||||
|
||||
function setApi() {
|
||||
const apiDir = path.normalize(__dirname + '/apis');
|
||||
console.log('apiDir:', apiDir);
|
||||
// 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 file = apiDir + filename;
|
||||
_.map(require(`./apis/${filename}`), function(fn, method) {
|
||||
const fileObj = require(`./apis/${filename}`);
|
||||
_.map(fileObj, function(fn, method) {
|
||||
let methodName = getApiName(name, method);
|
||||
apis[methodName] = fn;
|
||||
ELog.info('[ Monitor ]', methodName);
|
||||
// ELog.info('[ Monitor ]', methodName);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -4,3 +4,16 @@ exports.info = function() {
|
||||
console.log('test api info');
|
||||
return true;
|
||||
};
|
||||
|
||||
// class Test {
|
||||
// constructor() {
|
||||
// this.name = 'test';
|
||||
// }
|
||||
|
||||
// info () {
|
||||
// console.log('test api info');
|
||||
// return true;
|
||||
// };
|
||||
// }
|
||||
|
||||
// module.exports = Test;
|
||||
@@ -39,6 +39,10 @@ const config = {
|
||||
provider: 'generic', // or github, s3, bintray
|
||||
url: 'https://raw.githubusercontent.com/wallace5303/electron-egg/master/' // resource dir
|
||||
}
|
||||
},
|
||||
electronApi: {
|
||||
port: 7069,
|
||||
hostname: '0.0.0.0',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,4 +61,13 @@ exports.setDynamicPort = async function () {
|
||||
return res;
|
||||
};
|
||||
|
||||
exports.setApiDynamicPort = async function () {
|
||||
const dynamicPort = await getPort();
|
||||
this.instance()
|
||||
.set('electron_api.port', dynamicPort)
|
||||
.write();
|
||||
|
||||
return dynamicPort;
|
||||
};
|
||||
|
||||
exports = module.exports;
|
||||
Reference in New Issue
Block a user