const bodyParser = require('body-parser'); import {sendActionToService} from '@truss/comm'; const debug = require('debug')('truss:gateway:listener'); const parser = bodyParser.json(); export default function(serviceMap, req, res) { parser(req, res, () => { debug(`HTTP ${req.method} ${req.url}`); // map to action const action = {type: undefined, payload: {url: req.url}}; switch (req.method) { case 'POST': // truss action if (req.url.startsWith('/truss/action/')) { action.type = req.url.substr('/truss/action/'.length); } // truss/http-post else { action.type = 'truss/http-post'; } action.payload.body = req.body; break; default: // truss/http-(get|delete|...) action.type = `truss/http-${req.method.toLowerCase()}`; action.payload.headers = req.headers; break; } // listeners... for (service of serviceMap.listeners[action.type] || []) { sendActionToService(action, service).done(); } // executor if (!(action.type in serviceMap.executors)) { debug(`No executor for ${action.type}!`); res.writeHead(501); res.end( '