Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const prettyPrint = require('./modules/logger')
  2. const logger = require('pino')({
  3.   name: 'api-gateway',
  4.   prettyPrint
  5. })
  6. const CWRMQ = require('cwrmq');
  7. const uuid = require('uuid');
  8. const config = require('./config')
  9.  
  10. let n = 0
  11. const mock = {
  12.   createBuilding: (n, show) => ({
  13.     resp: 'api-gateway',
  14.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  15.     data: {//data to service
  16.       user: 'visarut.on@throughwave.co.th',
  17.       api: 'create-building',
  18.       body: {
  19.         name: show && 'hello'+n,
  20.         type: 'Building'
  21.       },
  22.     }
  23.   }),
  24.   getBuilding: {
  25.     resp: 'api-gateway',
  26.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  27.     data: {//data to service
  28.       user: 'visarut.on@throughwave.co.th',
  29.       api: 'get-building',
  30.       query: {
  31.         id: 'b1'
  32.       },
  33.     }
  34.   },
  35.   updateBuilding: {
  36.     resp: 'api-gateway',
  37.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  38.     data: {//data to service
  39.       user: 'visarut.on@throughwave.co.th',
  40.       api: 'update-building',
  41.       query: {
  42.         id: 'b1',
  43.       },
  44.       body: {
  45.         name: 'Thai CC Tower kiki',
  46.       },
  47.     }
  48.   },
  49.   deleteBuilding: {
  50.     resp: 'api-gateway',
  51.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  52.     data: {//data to service
  53.       user: 'visarut.on@throughwave.co.th',
  54.       api: 'delete-building',
  55.       query: {
  56.         id: 'b1',
  57.       },
  58.     }
  59.   },
  60.   createFloor: {
  61.     resp: 'api-gateway',
  62.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  63.     data: {//data to service
  64.       user: 'visarut.on@throughwave.co.th',
  65.       api: 'create-floor',
  66.       body: {
  67.         name: 'floor 1',
  68.         type: 'Floor'
  69.       },
  70.     }
  71.   },
  72.   getFloor: {
  73.     resp: 'api-gateway',
  74.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  75.     data: {//data to service
  76.       user: 'visarut.on@throughwave.co.th',
  77.       api: 'get-floor',
  78.       query: {
  79.         id: 'f1',
  80.       },
  81.     }
  82.   },
  83.   updateFloor: {
  84.     resp: 'api-gateway',
  85.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  86.     data: {//data to service
  87.       user: 'visarut.on@throughwave.co.th',
  88.       api: 'update-floor',
  89.       query: {
  90.         id: 'f1',
  91.       },
  92.       body: {
  93.         name: 'floor 1',
  94.         type: 'Floor'
  95.       },
  96.     }
  97.   },
  98.   deleteFloor: {
  99.     resp: 'api-gateway',
  100.     id: '311b745c-82f9-4a5e-8747-19c0c6de8693',
  101.     data: {//data to service
  102.       user: 'visarut.on@throughwave.co.th',
  103.       api: 'delete-floor',
  104.       query: {
  105.         id: 'f1',
  106.       },
  107.     }
  108.   }
  109. }
  110.  
  111. function printOption() {
  112.   logger.info(`Choose api:
  113.       1: create building
  114.       2: get building
  115.       3: update building
  116.       4: delete building
  117.       5: create floor
  118.       6: get floor
  119.       7: update floor
  120.       8: delete floor
  121.     `)
  122. }
  123.  
  124. function callback(sender, channel, jobID, func, data, done) {
  125.   logger.info('Receive data from: '+channel+' => '+data)
  126.   done()
  127. }
  128.  
  129. const cwrmq = new CWRMQ('api-gateway-module', 'api-gateway-worker-id');
  130. cwrmq.regis('api-gateway', [], callback, err => {
  131.   if (!err) {
  132.     logger.info('Register api-gateway success')
  133.     // cwrmq.sendMessage(uuid(), config.busModule, 'create-building',
  134.     //   JSON.stringify(mock.createBuilding(n++, true)))
  135.     printOption()
  136.     const stdin = process.openStdin();
  137.     stdin.addListener('data', data => {
  138.       logger.info(`you entered: ${data.toString().trim()}`);
  139.       switch(data.toString().trim()) {
  140.         case '1':
  141.           const uid = uuid()
  142.           n++
  143.           logger.info(`Sending message: cwrmq.sendMessage(${uid}, ${config.busModule}, 'create-building', ${JSON.stringify(mock.createBuilding(n, true))})`)
  144.           cwrmq.sendMessage(uid, config.busModule, 'create-building', JSON.stringify(mock.createBuilding(n, true)))
  145.           break
  146.         case '0':
  147.           logger.info('Sending message: create-building')
  148.           cwrmq.sendMessage(uuid(), config.busModule, 'create-building',
  149.             JSON.stringify(mock.createBuilding(n)))
  150.           break
  151.         case '2':
  152.           logger.info('Sending message: get-building')
  153.           cwrmq.sendMessage(uuid(), config.busModule, 'get-building',
  154.             JSON.stringify(mock.getBuilding))
  155.           break
  156.         case '3':
  157.           logger.info('Sending message: update-building')
  158.           cwrmq.sendMessage(uuid(), config.busModule, 'update-building',
  159.             JSON.stringify(mock.updateBuilding))
  160.           break
  161.         case '4':
  162.           logger.info('Sending message: delete-building')
  163.           cwrmq.sendMessage(uuid(), config.busModule, 'delete-building',
  164.             JSON.stringify(mock.deleteBuilding))
  165.           break
  166.         case '5':
  167.           logger.info('Sending message: create-floor')
  168.           cwrmq.sendMessage(uuid(), config.busModule, 'create-floor',
  169.             JSON.stringify(mock.createFloor))
  170.           break
  171.         case '6':
  172.           logger.info('Sending message: get-floor')
  173.           cwrmq.sendMessage(uuid(), config.busModule, 'get-floor',
  174.             JSON.stringify(mock.getFloor))
  175.           break
  176.         case '7':
  177.           logger.info('Sending message: update-floor')
  178.           cwrmq.sendMessage(uuid(), config.busModule, 'update-floor',
  179.             JSON.stringify(mock.updateFloor))
  180.           break
  181.         case '8':
  182.           logger.info('Sending message: delete-floor')
  183.           cwrmq.sendMessage(uuid(), config.busModule, 'delete-floor',
  184.             JSON.stringify(mock.deleteFloor))
  185.           break
  186.         case 'p':
  187.           printOption()
  188.           break
  189.       }
  190.     });
  191.   }
  192.   else logger.error('Register api-gateway failed')
  193. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement