server.js 11 KB

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