helloworld.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* a simple hello world plugin... it listens for "POST /hello" requests... requests are received by "csworker" and forwarded to "asworker"... both respond with their __worker\'s id and with a counter of the number of handled requests */
  2. const {
  3. __errorContinuable,
  4. __httpReq,
  5. __wHttpReq,
  6. __postInternalErrorMsg, __postMessage,
  7. __sequenceNumber,
  8. __successContinuable,
  9. __uri_to_id
  10. } = require("../__worker");
  11. const _do = require("../___do");
  12. module.exports = {
  13. 'interfaces' : [{'method':'POST', 'url=':'/hello'}],
  14. 'localcontext' : {'counter':0},
  15. 'csworker' :
  16. function(resp,method,uri,reqData,wcontext)
  17. {
  18. var self = this;
  19. _do.chain(
  20. [__wHttpReq(method, uri+'?wid='+wcontext.__aswid, reqData)])(
  21. function()
  22. {
  23. self.localcontext.counter++;
  24. __postMessage(
  25. {'statusCode':200,
  26. 'changelog':
  27. [{'op':'SYSOUT',
  28. 'text':'Hello #'+self.localcontext.counter+' from csworker #'+__wid}],
  29. 'sequence#':__sequenceNumber(),
  30. 'respIndex':resp});
  31. },
  32. function(err) {__postInternalErrorMsg(resp,err);}
  33. );
  34. },
  35. 'asworker' :
  36. function(resp,method,uri,reqData,wcontext)
  37. {
  38. this.localcontext.counter++;
  39. __postMessage(
  40. {'statusCode':200,
  41. 'changelog':
  42. [{'op':'SYSOUT',
  43. 'text':'Hello #'+this.localcontext.counter+' from asworker #'+__wid}],
  44. 'sequence#':__sequenceNumber(),
  45. 'respIndex':resp});
  46. }
  47. };