Skip to content

Commit

Permalink
moving botid to context
Browse files Browse the repository at this point in the history
  • Loading branch information
roaringdev committed Mar 8, 2018
1 parent 3b7c590 commit e023218
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
21 changes: 15 additions & 6 deletions lib/wrappers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ require('source-map-support').install({
handleUncaughtExceptions: false
});

var file = require("____FILE____");
var handler = "____HANDLER____";
const path = require("path");
const filePath = "____FILE____";
const handler = "____HANDLER____";
const pkg = path.dirname(filePath);
const botId = pkg.name;
const settings = pkg.config && pkg.config.leo && pkg.config.leo.cron && pkg.config.leo.cron.settings || {};



function empty(obj) {
for (let k in obj) {
Expand All @@ -18,12 +24,15 @@ function empty(obj) {
}

module.exports = {
handler: function (event, context, callback) {
handler: function(event, context, callback) {
context.resources = process.resources;
context.botId = botId;
context.settings = settings;

for (let x of process.listeners('uncaughtException')) { //remove lambdas default listener
process.removeListener('uncaughtException', x);
}
process.on('uncaughtException', function (err) {
process.on('uncaughtException', function(err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
callback(null, {
Expand All @@ -39,6 +48,6 @@ module.exports = {
config.registry.id = process.env.AWS_LAMBDA_FUNCTION_NAME;
}

file[handler](event, context, callback);
require(filePath)[handler](event, context, callback);
}
};
};
17 changes: 14 additions & 3 deletions lib/wrappers/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ require('source-map-support').install({
handleUncaughtExceptions: false
});

const path = require("path");
const filePath = "____FILE____";
const handler = "____HANDLER____";
const pkg = path.dirname(filePath);
const botId = pkg.name;
const settings = pkg.config && pkg.config.leo && pkg.config.leo.cron && pkg.config.leo.cron.settings || {};


var moment = require("moment");
var handler = "____HANDLER____";
let decrypted = false;
let botHandler = function(event, context, callback) {
let tasks = [];
Expand All @@ -36,7 +43,7 @@ let botHandler = function(event, context, callback) {
return callback(err);
}
decrypted = true;
require("____FILE____")[handler](event, context, callback);
require(filePath)[handler](event, context, callback);
});
};

Expand Down Expand Up @@ -80,6 +87,10 @@ module.exports = {
handler: function(event, context, callback) {
let debug = process.env.debug === "true";
context.resources = process.resources;
context.botId = botId;
context.settings = settings;


if (event.requestContext) { //new lambda proxy method
if (event.isBase64Encoded) {
event.body = JSON.parse(new Buffer(event.body, 'base64'));
Expand Down Expand Up @@ -186,4 +197,4 @@ module.exports = {
});
}
}
};
};
22 changes: 15 additions & 7 deletions lib/wrappers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,38 @@ require('source-map-support').install({
handleUncaughtExceptions: false
});

var file = require("____FILE____");
var handler = "____HANDLER____";
const path = require("path");
const filePath = "____FILE____";
const handler = "____HANDLER____";
const pkg = path.dirname(filePath);
const botId = pkg.name;
const settings = pkg.config && pkg.config.leo && pkg.config.leo.cron && pkg.config.leo.cron.settings || {};


function empty(obj) {
for (let k in obj) {
delete obj[k];
}
}

var getUser = function (event, context, callback) {
var getUser = function(event, context, callback) {
callback();
};

module.exports = {
handler: function (event, context, callback) {
handler: function(event, context, callback) {
context.resources = process.resources;
context.botId = botId;
context.settings = settings;

//clear out the registry
empty(config.registry);
config.registry.user = null;

for (let x of process.listeners('uncaughtException')) { //remove lambdas default listener
process.removeListener('uncaughtException', x);
}
process.on('uncaughtException', function (err) {
process.on('uncaughtException', function(err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
callback(null, {
Expand Down Expand Up @@ -63,7 +71,7 @@ module.exports = {
}

getUser(event, context, (err) => {
file[handler](event, context, function (err, data) {
require(filePath)[handler](event, context, function(err, data) {
if (data && typeof data === "object" && "statusCode" in data) {
if (config.cors && !("Access-Control-Allow-Origin" in data.headers)) {
data.headers["Access-Control-Allow-Origin"] = config.cors;
Expand Down Expand Up @@ -100,4 +108,4 @@ module.exports = {
});
});
}
};
};
4 changes: 2 additions & 2 deletions templates/bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const ls = leo.streams;


exports.handler = function(event, context, callback) {
const ID = event.botId;
const ID = context.botId;

// Do work
callback();
};
};

0 comments on commit e023218

Please sign in to comment.