You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.0 KiB

  1. var http = require('http');
  2. const fs = require('fs');
  3. var url = require('url');
  4. var path = require('path');
  5. var StringDecoder = require('string_decoder').StringDecoder;
  6. var exec = require("child_process").exec;
  7. var Promise= require('promise');
  8. var mimeTypes = {
  9. "html": "text/html",
  10. "jpeg": "image/jpeg",
  11. "jpg": "image/jpeg",
  12. "png": "image/png",
  13. "js": "text/javascript",
  14. "css": "text/css",
  15. "json": "text/plain"};
  16. function writenodes(nodes) {
  17. fs.writeFile('public/Verbesserungen.txt', nodes, { flag: 'w' }, (err) => {
  18. if (err)
  19. console.log(err);
  20. else {
  21. console.log("File written successfully\n");
  22. console.log("The written has the following contents:");
  23. console.log(fs.readFileSync("public/nodes.txt", "utf8"));
  24. }
  25. })};
  26. // function writeedges(edges) {
  27. //
  28. // fs.writeFile('public/edges.txt', edges, { flag: 'w' }, (err) => {
  29. // if (err)
  30. // console.log(err);
  31. // else {
  32. // console.log("File written successfully\n");
  33. // console.log("The written has the following contents:");
  34. // console.log(fs.readFileSync("public/edges.txt", "utf8"));
  35. // }
  36. // })};
  37. // the following read funcitons are not asyncronous, that means they will block the process..
  38. // for scalability this has to be changed in future
  39. // function readnodes() {
  40. // var input = fs.readFileSync('public/nodes.txt', 'utf8')
  41. // // console.log('input ooohyeah',input)
  42. // // // var danodes = JSON.parse(input);
  43. // //
  44. // // const lines = input.split(/\r?\n/);
  45. // // console.log('line numbaaa 1', lines, lines[1], lines[1].length)
  46. // // var string = '';
  47. // // var i;
  48. // //
  49. // // for (i = 0; i < (lines.length - 2); i++) {
  50. // // string += '"data' + i.toString() + '":' + lines[i] + ' ,' ;
  51. // // };
  52. // // string += '"data' + lines.length.toString() + '":' + lines[lines.length - 2] ;
  53. // // string = '{' + string + '}' ;
  54. // // const jsonlines = JSON.parse(string)
  55. // //
  56. // //
  57. // // let sortedlines = [];
  58. // // sortedlines = [].slice.call(jsonlines).sort(function(a,b) {
  59. // // var x = a.length;
  60. // // var y = b.length;
  61. // // if (x < y) {
  62. // // return -1;
  63. // // }
  64. // // if (x > y) {
  65. // // return 1;
  66. // // }
  67. // // return 0;
  68. // //
  69. // // });
  70. // // console.log('daline1 in nodes', sortedlines)
  71. // return input
  72. // };
  73. // function readedges() {
  74. //
  75. // var daedges = fs.readFileSync('public/edges.txt', 'utf8');
  76. // // const lines = daedges.split(/\r?\n/);
  77. // // // console.log(lines)
  78. // // sortedlines = lines.sort(function(a,b) {
  79. // // var x = a.length;
  80. // // var y = b.length;
  81. // // if (x < y) {
  82. // // return -1;
  83. // // }
  84. // // if (x > y) {
  85. // // return 1;
  86. // // }
  87. // // return 0;
  88. // //
  89. // // });
  90. // // console.log('daline1 in edges', sortedlines[0])
  91. // return daedges
  92. // };
  93. //
  94. http.createServer((request, response)=>{
  95. var pathname = url.parse(request.url).pathname;
  96. var filename;
  97. if(pathname === "/"){
  98. filename = "Prototyp.html";
  99. }
  100. else{
  101. filename = path.join(process.cwd(), pathname);
  102. }
  103. try{
  104. fs.accessSync(filename, fs.F_OK);
  105. var fileStream = fs.createReadStream(filename);
  106. var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
  107. response.writeHead(200, {'Content-Type':mimeType});
  108. fileStream.pipe(response);
  109. if (mimeType === "text/plain"){
  110. // Get the payload,if any
  111. var decoder = new StringDecoder('utf-8');
  112. var buffer = '';
  113. var theanswer = '';
  114. request.on('data', function(data) {
  115. buffer += decoder.write(data);
  116. });
  117. var theanswer = '';
  118. console.log('i am at least come til here')
  119. request.on('end', function() {
  120. buffer += decoder.end();
  121. console.log(buffer);
  122. if (buffer[3] === 'i'){
  123. console.log(buffer);
  124. }
  125. if (buffer[3] === 'f'){
  126. console.log(buffer);
  127. theanswer = 'your changes were taken into the\n\nM A T R I X'
  128. }
  129. console.log(buffer[3])
  130. if (buffer === 'DONE'){
  131. console.log('recieved a DONE')
  132. exec("cp /dev/null > public/data.json")
  133. }
  134. if (buffer.length > 1) {
  135. console.log('starting hte python code, argument is ' + buffer)
  136. const execpython = new Promise((resolve, reject) => {
  137. exec("python prototype_all_chained.py " + buffer + " > public/data.json", (error, stdout, stderr) => {
  138. if (error) {
  139. console.log(`error: ${error.message}`);
  140. reject(error)
  141. } else {
  142. console.log(`stdout: ${stdout}`);
  143. resolve(stdout)
  144. }
  145. });
  146. })
  147. const checkIfItsDone = () => {
  148. execpython
  149. .then(ok => {
  150. console.log(ok)
  151. })
  152. .catch(err => {
  153. console.log(err)
  154. })
  155. }
  156. checkIfItsDone()
  157. }
  158. // var string = 'oi';
  159. // // var string = execpython('oi');
  160. // // console.log('output of execpython', string);
  161. // Promise.resolve(string)
  162. // .then(execpython(string))
  163. // .then(s => response.end(s))
  164. // Log the request/response
  165. console.log('Payload received: ',buffer);
  166. });
  167. }
  168. }
  169. catch(e) {
  170. console.log('File not exists: ' + filename);
  171. response.writeHead(404, {'Content-Type': 'text/plain'});
  172. response.write('404 Not Found\n');
  173. response.end();
  174. return;
  175. }
  176. return;
  177. }
  178. ).listen(8888);