Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

296 righe
13 KiB

1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
  1. const http = require('http');
  2. const querystring = require('querystring');
  3. const url = require('url');
  4. const fs = require('fs');
  5. const path=require('path');
  6. const {app, dialog} = require('electron');
  7. const XLSX = require('xlsx');
  8. const multer = require('multer');
  9. function travel(dir,callback){
  10. fs.readdirSync(dir).forEach((file)=>{
  11. const pathname=path.join(dir,file)
  12. if(fs.statSync(pathname).isDirectory()){
  13. travel(pathname,callback)
  14. }else{
  15. callback(pathname)
  16. }
  17. })
  18. }
  19. function compare(p){ //这是比较函数
  20. return function(m,n){
  21. var a = m[p];
  22. var b = n[p];
  23. return b - a; //降序
  24. }
  25. }
  26. function getDir(){
  27. if(__dirname.indexOf("app") >= 0 && __dirname.indexOf("sources") >= 0){
  28. if(process.platform == "darwin"){
  29. return app.getPath("userData");
  30. } else {
  31. return path.join(__dirname,"../../..");
  32. }
  33. } else {
  34. return __dirname;
  35. }
  36. }
  37. function getEasySpiderLocation(){
  38. if(__dirname.indexOf("app") >= 0 && __dirname.indexOf("sources") >= 0){
  39. if(process.platform == "darwin"){
  40. return path.join(__dirname,"../../../");
  41. } else {
  42. return path.join(__dirname,"../../../");
  43. }
  44. } else {
  45. return __dirname;
  46. }
  47. }
  48. if(!fs.existsSync(path.join(getDir(), "tasks"))){
  49. fs.mkdirSync(path.join(getDir(), "tasks"));
  50. }
  51. if(!fs.existsSync(path.join(getDir(), "execution_instances"))){
  52. fs.mkdirSync(path.join(getDir(), "execution_instances"));
  53. }
  54. if(!fs.existsSync(path.join(getDir(), "config.json"))){
  55. fs.writeFileSync(path.join(getDir(), "config.json"), JSON.stringify({"webserver_address":"http://localhost","webserver_port":8074,"user_data_folder":"./user_data","absolute_user_data_folder":""}));
  56. }
  57. exports.getDir = getDir;
  58. exports.getEasySpiderLocation = getEasySpiderLocation;
  59. FileMimes = JSON.parse(fs.readFileSync(path.join(__dirname,'mime.json')).toString());
  60. exports.start = function(port = 8074) {
  61. http.createServer(function(req, res) {
  62. let body = "";
  63. res.setHeader("Access-Control-Allow-Origin", "*"); // 设置可访问的源
  64. // 解析参数
  65. const pathName = url.parse(req.url).pathname;
  66. if(pathName.indexOf(".") < 0) { //如果没有后缀名, 则为后台请求
  67. res.writeHead(200, { 'Content-Type': 'application/json' });
  68. }
  69. // else if(pathName.indexOf("index.html") >= 0) {
  70. // fs.readFile(path.join(__dirname,"src", pathName), async (err, data) => {
  71. // if (err) {
  72. // res.writeHead(404, { 'Content-Type': 'text/html;charset="utf-8"' })
  73. // res.end(err.message)
  74. // return;
  75. // }
  76. // if (!err) {
  77. // // 3. 针对不同的文件返回不同的内容头
  78. // let extname = path.extname(pathName);
  79. // let mime = FileMimes[extname]
  80. // res.writeHead(200, { 'Content-Type': mime + ';charset="utf-8"' })
  81. // res.end(data);
  82. // return;
  83. // }
  84. // })
  85. // }
  86. else { //如果有后缀名, 则为前端请求
  87. // console.log(path.join(__dirname,"src/taskGrid", pathName));
  88. fs.readFile(path.join(__dirname,"src", pathName), async (err, data) => {
  89. if (err) {
  90. res.writeHead(404, { 'Content-Type': 'text/html;charset="utf-8"' })
  91. res.end(err.message)
  92. return;
  93. }
  94. if (!err) {
  95. // 3. 针对不同的文件返回不同的内容头
  96. let extname = path.extname(pathName);
  97. let mime = FileMimes[extname]
  98. res.writeHead(200, { 'Content-Type': mime + ';charset="utf-8"' })
  99. res.end(data);
  100. return;
  101. }
  102. })
  103. }
  104. req.on('data', function(chunk) {
  105. body += chunk;
  106. });
  107. req.on('end', function() {
  108. // 设置响应头部信息及编码
  109. if (pathName == "/queryTasks") { //查询所有服务信息,只包括id和服务名称
  110. output = [];
  111. travel(path.join(getDir(), "tasks"),function(pathname){
  112. const data = fs.readFileSync(pathname, 'utf8');
  113. let stat = fs.statSync(pathname, 'utf8');
  114. // parse JSON string to JSON object
  115. const task = JSON.parse(data);
  116. let item = {
  117. "id": task.id,
  118. "name": task.name,
  119. "url": task.url,
  120. "mtime": stat.mtime,
  121. }
  122. if(item.id!= -2) {
  123. output.push(item);
  124. }
  125. });
  126. output.sort(compare("mtime"));
  127. res.write(JSON.stringify(output));
  128. res.end();
  129. } else if(pathName == "/queryOSVersion") {
  130. res.write(JSON.stringify({"version":process.platform, "bit":process.arch}));
  131. res.end();
  132. }
  133. else if (pathName == "/queryExecutionInstances") { //查询所有服务信息,只包括id和服务名称
  134. output = [];
  135. travel(path.join(getDir(), "execution_instances"),function(pathname){
  136. const data = fs.readFileSync(pathname, 'utf8');
  137. // parse JSON string to JSON object
  138. const task = JSON.parse(data);
  139. let item = {
  140. "id": task.id,
  141. "name": task.name,
  142. "url": task.url,
  143. }
  144. if(item.id!= -2) {
  145. output.push(item);
  146. }
  147. });
  148. res.write(JSON.stringify(output));
  149. res.end();
  150. } else if (pathName == "/queryTask") {
  151. var params = url.parse(req.url, true).query;
  152. try {
  153. var tid = parseInt(params.id);
  154. const data = fs.readFileSync(path.join(getDir(), `tasks/${tid}.json`), 'utf8');
  155. // parse JSON string to JSON object
  156. res.write(data);
  157. res.end();
  158. } catch (error) {
  159. res.write(JSON.stringify({ "error": "Cannot find task based on specified task ID." }));
  160. res.end();
  161. }
  162. } else if (pathName == "/queryExecutionInstance") {
  163. var params = url.parse(req.url, true).query;
  164. try {
  165. var tid = parseInt(params.id);
  166. const data = fs.readFileSync(path.join(getDir(), `execution_instances/${tid}.json`), 'utf8');
  167. // parse JSON string to JSON object
  168. res.write(data);
  169. res.end();
  170. } catch (error) {
  171. res.write(JSON.stringify({ "error": "Cannot find execution instance based on specified execution ID." }));
  172. res.end();
  173. }
  174. } else if(pathName == "/"){
  175. res.write("Hello World!", 'utf8');
  176. res.end();
  177. } else if(pathName == "/deleteTask"){
  178. var params = url.parse(req.url, true).query;
  179. try {
  180. let tid = parseInt(params.id);
  181. let data = fs.readFileSync(path.join(getDir(), `tasks/${tid}.json`), 'utf8');
  182. data = JSON.parse(data);
  183. data.id = -2;
  184. data = JSON.stringify(data);
  185. // write JSON string to a file
  186. fs.writeFile(path.join(getDir(), `tasks/${tid}.json`), data, (err) => {
  187. if (err) {
  188. throw err;
  189. }
  190. });
  191. res.write(JSON.stringify({ "success": "Task has been deleted successfully." }));
  192. res.end();
  193. } catch (error) {
  194. res.write(JSON.stringify({ "error": "Cannot find task based on specified task ID." }));
  195. res.end();
  196. }
  197. } else if(pathName == "/manageTask"){
  198. body = querystring.parse(body);
  199. data = JSON.parse(body.paras);
  200. let id = data["id"];
  201. if (data["id"] == -1) {
  202. file_names = [];
  203. fs.readdirSync(path.join(getDir(), "tasks")).forEach((file)=>{
  204. try{
  205. if(file.split(".")[1] == "json"){
  206. file_names.push(parseInt(file.split(".")[0]));
  207. }
  208. } catch (error) {
  209. }
  210. })
  211. if(file_names.length == 0){
  212. id = 0;
  213. } else {
  214. id = Math.max(...file_names) + 1;
  215. }
  216. data["id"] = id;
  217. // write JSON string to a fil
  218. }
  219. data = JSON.stringify(data);
  220. // write JSON string to a file
  221. fs.writeFile(path.join(getDir(), `tasks/${id}.json`), data, (err) => {});
  222. res.write(id.toString(), 'utf8');
  223. res.end();
  224. } else if(pathName == "/invokeTask"){
  225. body = querystring.parse(body);
  226. let data = JSON.parse(body.paras);
  227. let id = body.id;
  228. let task = fs.readFileSync(path.join(getDir(), `tasks/${id}.json`), 'utf8');
  229. task = JSON.parse(task);
  230. try{
  231. task["links"] = data["urlList_0"];
  232. }catch(error){
  233. console.log(error);
  234. }
  235. for (const [key, value] of Object.entries(data)) {
  236. for (let i = 0; i < task["inputParameters"].length; i++) {
  237. if (key === task["inputParameters"][i]["name"]) { // 能调用
  238. const nodeId = parseInt(task["inputParameters"][i]["nodeId"]);
  239. const node = task["graph"][nodeId];
  240. if (node["option"] === 1) {
  241. node["parameters"]["links"] = value;
  242. } else if (node["option"] === 4) {
  243. node["parameters"]["value"] = value;
  244. } else if (node["option"] === 8 && node["parameters"]["loopType"] === 0) {
  245. node["parameters"]["exitCount"] = parseInt(value);
  246. } else if (node["option"] === 8) {
  247. node["parameters"]["textList"] = value;
  248. }
  249. break;
  250. }
  251. }
  252. }
  253. let file_names = [];
  254. fs.readdirSync(path.join(getDir(), "execution_instances")).forEach((file)=>{
  255. try{
  256. if(file.split(".")[1] == "json"){
  257. file_names.push(parseInt(file.split(".")[0]));
  258. }
  259. console.log(file);
  260. } catch (error) {
  261. }
  262. })
  263. let eid = 0;
  264. if (file_names.length != 0) {
  265. eid = Math.max(...file_names) + 1;
  266. }
  267. task["id"] = eid;
  268. task = JSON.stringify(task);
  269. fs.writeFile(path.join(getDir(), `execution_instances/${eid}.json`), task, (err) => {});
  270. res.write(eid.toString(), 'utf8');
  271. res.end();
  272. } else if(pathName == "/getConfig"){
  273. let config = fs.readFileSync(path.join(getDir(), `config.json`), 'utf8');
  274. config = JSON.parse(config);
  275. res.write(JSON.stringify(config));
  276. res.end();
  277. } else if(pathName == "/setUserDataFolder"){
  278. let config = fs.readFileSync(path.join(getDir(), `config.json`), 'utf8');
  279. config = JSON.parse(config);
  280. body = querystring.parse(body);
  281. config["user_data_folder"] = body["user_data_folder"];
  282. config = JSON.stringify(config);
  283. fs.writeFile(path.join(getDir(), `config.json`), config, (err) => {});
  284. res.write(JSON.stringify({ "success": "User data folder has been set successfully." }));
  285. res.end();
  286. }
  287. });
  288. }).listen(port);
  289. console.log("Server has started.");
  290. }