From 28e5f0822f54e7ea4814d1c7b595401b41f62a45 Mon Sep 17 00:00:00 2001 From: Jeremy Green Date: Sat, 4 Jun 2016 11:06:19 -0500 Subject: [PATCH 1/4] Extract the app router --- demo-app.js | 7 ++++--- demo-router.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 demo-router.js diff --git a/demo-app.js b/demo-app.js index 59f5040..b2b8f07 100644 --- a/demo-app.js +++ b/demo-app.js @@ -1,11 +1,12 @@ var Server = require('./server'); +var Router = require('./demo-router'); + var util = require('util'); var server = new Server(); +var router = new Router(); -server.routes(function() { - this.get('/posts', {from: 'demo-app-posts', with: 'index'}); -}); +router.loadRoutes(server); server.start(3000); diff --git a/demo-router.js b/demo-router.js new file mode 100644 index 0000000..cbca409 --- /dev/null +++ b/demo-router.js @@ -0,0 +1,13 @@ +function Router() { + +} + +Router.prototype.loadRoutes = function(server){ + + server.routes(function() { + this.get('/posts', {from: 'demo-app-posts', with: 'index'}); + }); + +} + +module.exports = Router; From 1b2ebda76fe6ad98f89c7bdc2206d208d01986eb Mon Sep 17 00:00:00 2001 From: Jeremy Green Date: Sat, 4 Jun 2016 11:16:37 -0500 Subject: [PATCH 2/4] Refactor post endpoint to follow express style --- demo-app-posts.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/demo-app-posts.js b/demo-app-posts.js index 2462e0d..8b68f29 100644 --- a/demo-app-posts.js +++ b/demo-app-posts.js @@ -1,5 +1,6 @@ module.exports = { index: function(req, res) { - return [200, {}, "ohai"]; + res.status(200); + res.json({ message: "ohai"}); } -}; \ No newline at end of file +}; From 87f84767ada998be5d0bfe6a4dea348634393b2c Mon Sep 17 00:00:00 2001 From: Jeremy Green Date: Sat, 4 Jun 2016 12:22:02 -0500 Subject: [PATCH 3/4] Add a demo lambda handler and a runner for testing it locally --- demo-app-posts-lambda-handler.js | 9 ++ demo-app-posts-lambda-runner.js | 17 ++++ lambda-utils.js | 156 +++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 demo-app-posts-lambda-handler.js create mode 100644 demo-app-posts-lambda-runner.js create mode 100644 lambda-utils.js diff --git a/demo-app-posts-lambda-handler.js b/demo-app-posts-lambda-handler.js new file mode 100644 index 0000000..0f36723 --- /dev/null +++ b/demo-app-posts-lambda-handler.js @@ -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); +}; diff --git a/demo-app-posts-lambda-runner.js b/demo-app-posts-lambda-runner.js new file mode 100644 index 0000000..0ce812d --- /dev/null +++ b/demo-app-posts-lambda-runner.js @@ -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); diff --git a/lambda-utils.js b/lambda-utils.js new file mode 100644 index 0000000..fa0151f --- /dev/null +++ b/lambda-utils.js @@ -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; +} + From f3dc126f75452563c0d54cde0d629bc7ccf8535d Mon Sep 17 00:00:00 2001 From: Jeremy Green Date: Sat, 4 Jun 2016 12:30:22 -0500 Subject: [PATCH 4/4] Revert "Extract the app router" This reverts commit 28e5f0822f54e7ea4814d1c7b595401b41f62a45. --- demo-app.js | 7 +++---- demo-router.js | 13 ------------- 2 files changed, 3 insertions(+), 17 deletions(-) delete mode 100644 demo-router.js diff --git a/demo-app.js b/demo-app.js index b2b8f07..59f5040 100644 --- a/demo-app.js +++ b/demo-app.js @@ -1,12 +1,11 @@ var Server = require('./server'); -var Router = require('./demo-router'); - var util = require('util'); var server = new Server(); -var router = new Router(); -router.loadRoutes(server); +server.routes(function() { + this.get('/posts', {from: 'demo-app-posts', with: 'index'}); +}); server.start(3000); diff --git a/demo-router.js b/demo-router.js deleted file mode 100644 index cbca409..0000000 --- a/demo-router.js +++ /dev/null @@ -1,13 +0,0 @@ -function Router() { - -} - -Router.prototype.loadRoutes = function(server){ - - server.routes(function() { - this.get('/posts', {from: 'demo-app-posts', with: 'index'}); - }); - -} - -module.exports = Router;