Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a lambda handler for demo-app-posts #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo-app-posts-lambda-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const lambdaUtils = require('./lambda-utils');

const endpointLogic = require('./demo-app-posts');

module.exports.handler = function(event, context, cb) {
var req = lambdaUtils.buildRequest(event,context,cb);
var res = lambdaUtils.buildResponse(event,context,cb);
endpointLogic.index(req, res);
};
17 changes: 17 additions & 0 deletions demo-app-posts-lambda-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const lambdaHandler = require('./demo-app-posts-lambda-handler');

var event = {
headers: {}
};
var context = {};
function cb(error, data){
if(error){
console.log("got error-------");
console.log(error);
return;
}
console.log("got data-------");
console.log(data);
}

lambdaHandler.handler(event, context, cb);
5 changes: 3 additions & 2 deletions demo-app-posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
index: function(req, res) {
return [200, {}, "ohai"];
res.status(200);
res.json({ message: "ohai"});
}
};
};
156 changes: 156 additions & 0 deletions lambda-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
module.exports.buildRequest = function(event, context, cb) {
var req = {};

// http://expressjs.com/en/api.html#req
// Properties
req.app = null; // Not sure this applies in our context...
req.baseUrl = event.baseUrl;
req.body = event.body;
req.cookies = {}; // TODO : Figure out how to get cookies from AWS Gateway
req.fresh = true; // TODO : Does this apply in our context?
req.hostname = event.headers.Host;
//req.ip = event.headers["X-Forwarded-For"].split(", ")[0];
req.ips = event.headers["X-Forwarded-For"];
req.method = event.method;
req.originalUrl = ""; // TODO : Figrure out how to reconstruct this
req.params = event.params;
req.path = event.baseUrl; // TODO : This isn't quite right...
req.protocol = event.headers["X-Forwarded-Proto"];
req.query = event.query;
req.route = null; // TODO : Does this apply to us?
req.secure = req.protocol === 'https';
req.signedCookies = {}; // TODO : ???
req.stale = false; // TODO : Does this apply in our context?
req.subdomains = []; // TODO: Does this apply?
req.xhr = event.headers["X-Requested-With"] === 'XMLHttpRequest';

// Methods
/*
var accept = realAccepts(req);
req.accepts = function accepts(types){
// TODO : Fix me!
return types;
//return accept.type(types);
};
*/
req.acceptsCharsets = function acceptsCharsets(){
// TODO : Fix me!
};

req.acceptsEncodings = function acceptsEncodings(){
// TODO : Fix me!
};

req.acceptsLanguages = function acceptsLanguages(){
// TODO : Fix me!
};

req.get = function get(field){
return event.headers[field];
}

req.is = function is(type){
// TODO: Fix me!
}

return req;
}

module.exports.buildResponse = function(event, context, cb) {
var res = {};

// Properties
res.app = null; // TODO : Does this apply?
res.headersSent = false; // TODO : Is this right?
res.locals = {}; // TODO: Does this apply?

res.headers = [];

// Methods
res.append = function append(field,value){
res.headers.push([field,value]);
}

res.attachment = function attachment(){
// TODO: Can we return attachments through API Gatewway?
}

res.cookie = function cookie(name, value, options){
// TODO: Can we set cookies?
}

res.clearCookie = function clearCookie(name, options){
// TODO: Can we set cookies?
}

res.download = function download(){
// TODO: Can we do this?
}

res.end = function end(){
// TODO: Implement me!
}

res.format = function format(){
// TODO: does this apply?
}

res.get = function get(field){
return headers[field];
}

res.json = function json(payload){
cb(null, payload);
}

res.jsonp = function jsonp(){
// TODO: Do we need to support josnp?
}

res.links = function links(){
// TODO: Implement me!
}

res.location = function location(){
// TODO: Implement me!
}

res.redirect = function redirect(){
// TODO: Implement me!
}

res.render = function render(){
// TODO: Does this apply?
}

res.send = function send(){
// TODO: Does this apply?
}

res.sendFile = function sendFile(){
// TODO: Does this apply?
}

res.sendStatus = function sendStatus(){
// TODO: Implement me!
}

res.set = function set(){
// TODO: Implement me!
}

res.status = function status(){
// TODO: Implement me!
}

res.type = function type(){
// TODO: Implement me!
}

res.vary = function vary(){
// TODO: Implement me!
}

return res;
}