node.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. /*
  3. Created by Deben Oldert
  4. */
  5. const express = require('express');
  6. const parser = require('body-parser');
  7. const needle = require('needle');
  8. //ENVIRONMENT VARIABLES
  9. /*
  10. CC => ip:port of the CC server
  11. PORT => Port to listen on
  12. IP => The ip address to listen on
  13. GUID => Unique ID of this host
  14. */
  15. // Constants
  16. const PORT = process.env.PORT || 8080;
  17. const CC = process.env.CC || "172.17.0.2:8092";
  18. const IP = process.env.IP || "172.17.0.3";
  19. const GUID = process.env.GUID || "00000000-0000-0000-0000-000000000000";
  20. //CLASSES
  21. class DOCUMENT {
  22. constructor(id, title, author, date, text){
  23. this.id = id;
  24. this.title = title;
  25. this.author = author;
  26. this.date = date;
  27. this.text = text;
  28. }
  29. }
  30. //VARS
  31. var docs = [];
  32. var peer = [IP]; //Have to add yourself here BUG in ccserver
  33. //FUNCTIONS
  34. //Check how many times a string occurs in another string
  35. String.prototype.occurs = function(str){
  36. if(this.length <= 0) return 0;
  37. //Make sure it's a string
  38. str += "";
  39. var i = 0;
  40. var pos = 0;
  41. while(true){
  42. //Get the position of the occurrence
  43. pos = str.indexOf(this, pos);
  44. //Found it
  45. if(pos >= 0){
  46. ++i;
  47. //Update start search position
  48. pos+= this.length;
  49. }
  50. //Found nothing? Stop the search
  51. else break;
  52. }
  53. //Return the number of found occurrences
  54. return i;
  55. }
  56. //Function to announce ourselfs to the CC
  57. function announce(){
  58. //Create a post request
  59. needle.post(
  60. "http://"+CC+"/v2.0/aanmelden/",
  61. {
  62. cc: CC,
  63. hostid: GUID,
  64. peernodes: peer
  65. },
  66. {
  67. json: true,
  68. },
  69. function(err, resp){
  70. //Got an error?
  71. if(err || resp.statusCode != 200){
  72. console.warn("Error: Couldn't announce to "+CC+". Status:"+resp.statusCode);
  73. return;
  74. }
  75. //No error, all good
  76. console.log("Succesfully announced");
  77. if("peernodes" in resp.body){
  78. for(var i=0; i<resp.body.peernodes.length; i++){
  79. peer.push(resp.body.peernodes[i]);
  80. }
  81. console.log("Retrieved " + peer.length + " peer nodes");
  82. }
  83. }
  84. )
  85. }
  86. // App
  87. const app = express();
  88. app.use(parser.json());
  89. app.post('/v2.0/pushdocument', (req, res) => {
  90. //Required variables
  91. var required = ["docid", "titel", "auteur", "datum", "tekst"];
  92. //Check if we have all the required variables
  93. for(var i=0; i<req.length; i++){
  94. //Variable not found? Stop the request
  95. if(!(required[i] in req.body)){
  96. console.warn("PushDocument requierd variable missing: " + required[i]);
  97. resp.status(500).send();
  98. return;
  99. }
  100. }
  101. //All good, create new document class
  102. docs.push(new DOCUMENT(req.body.docid, req.body.titel, req.body.auteur, req.body.datum, req.body.tekst));
  103. console.log("Succesfully added document: " + req.body.docid);
  104. //Tell the ccserver we good
  105. res.status(200).send();
  106. });
  107. app.post('/v2.0/reset', (req, res) => {
  108. console.log("Reset command recieved");
  109. //Speeks for itself
  110. for(var i=0; i<peer.length; i++){
  111. console.log("Telling peer " + peer[i] + " to reset");
  112. //Send peer a post request
  113. needle.post("http://"+peer[i]+"/reset", {});
  114. }
  115. //Starting 5s cooldown before we announce ourself again to the ccserver
  116. console.log("Starting cooldown before re-announcing");
  117. setTimeout(announce, 5000);
  118. res.status(200).send();
  119. });
  120. app.post('/v2.0/selecteer', (req, res) => {
  121. var out = [];
  122. //Is the filter variable set?
  123. if("filter" in req.body) {
  124. var filter = req.body.filter;
  125. console.log("Filtering for: " + filter.join(", "));
  126. //Check each document
  127. for(var i=0; i<docs.length; i++){
  128. var cnt = 0;
  129. //Check each line in the document
  130. for(var j=0; j<docs[i].text.length; j++){
  131. //Check each filter for each line in the document
  132. for(var k=0; k<filter.length; k++){
  133. //Increase the score with the amount of occurrences found
  134. cnt += filter[k].occurs(docs[i].text[j]);
  135. }
  136. }
  137. //Did it occur at least once? Add it to the output array
  138. if(cnt > 0) out.push({docid: docs[i].id, score: cnt});
  139. }
  140. //Function to compare each document in the output array
  141. function compare(a, b){
  142. if(a.score > b.score) return -1;
  143. if(a.score < b.score) return 1;
  144. return 0;
  145. }
  146. //Now actually sort the array
  147. out.sort(compare);
  148. console.log("Found " + out.length + " documents");
  149. res.send(out);
  150. }
  151. else { //Oops filter variable not set
  152. console.warn("Select filter variable missing");
  153. res.status(500).send();
  154. }
  155. });
  156. //Announce
  157. console.log("Announcing node to cc on: " + CC);
  158. announce();
  159. //Start listening
  160. console.log("Starting listener service on: " + IP + ":" + PORT);
  161. console.log("GUID: " + GUID);
  162. app.listen(PORT);