Created
August 28, 2017 13:15
-
-
Save theburningmonk/c1eb912a113cad27e02d6244dad15f8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const co = require('co'); | |
const log = require('./log'); | |
const reqContext = require('./requestContext'); | |
function setRequestContext (event, context) { | |
// extracts correlation IDs, User-Agent, etc. from HTTP headers | |
// and stores them in the requestContext | |
}; | |
function OK (result) { | |
// return 200 response | |
}; | |
function createApiHandler (f) { | |
return co.wrap(function* (event, context, cb) { | |
console.log(JSON.stringify(event)); | |
reqContext.clearAll(); | |
try { | |
setRequestContext(event, context); | |
} catch (err) { | |
log.warn(`couldn't set current request context: ${err}`, err.stack); | |
} | |
try { | |
let result = yield Promise.resolve(f(event, context)); | |
result = result || {}; | |
log.info('SUCCESS', JSON.stringify(result)); | |
cb(null, OK(result)); | |
} catch (err) { | |
log.error("Failed to process request", err); | |
cb(err); | |
} | |
}); | |
} | |
module.exports = createApiHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment