@geek-fun/serverlessinsight
Version:
Full life cycle cross providers serverless application management for your fast-growing business.
39 lines (38 loc) • 1.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startEvents = void 0;
const node_http_1 = __importDefault(require("node:http"));
const common_1 = require("../../common");
const types_1 = require("../../types");
const lodash_1 = require("lodash");
const startApiGatewayServer = (event) => {
const server = node_http_1.default.createServer((req, res) => {
const matchedTrigger = event.triggers.find((trigger) => trigger.method === req.method && trigger.path === req.url);
if (!matchedTrigger) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not Found\n');
common_1.logger.warn(`API Gateway Event - ${req.method} ${req.url} -> Not Found`);
return;
}
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end(`Invoked backend: ${matchedTrigger.backend}\n`);
common_1.logger.info(`API Gateway Event - ${req.method} ${req.url} -> ${matchedTrigger.backend}`);
});
const port = 3000 + Math.floor(Math.random() * 1000);
server.listen(port, () => {
common_1.logger.info(`API Gateway "${event.name}" listening on http://localhost:${port}`);
});
};
const startEvents = (events) => {
const apiGateways = events?.filter((event) => event.type === types_1.EventTypes.API_GATEWAY);
if ((0, lodash_1.isEmpty)(apiGateways)) {
return;
}
apiGateways.forEach((gateway) => {
startApiGatewayServer(gateway);
});
};
exports.startEvents = startEvents;