This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from WeltN24/addsnsrouting
add sns as routing provider
- Loading branch information
Showing
9 changed files
with
712 additions
and
341 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"use strict"; | ||
|
||
function handler(routeConfig) { | ||
const eventProcessorMapping = extractEventProcessorMapping(routeConfig); | ||
return (event, context, callback) => { | ||
for (const eventProcessorName of eventProcessorMapping.keys()) { | ||
|
||
try { | ||
// the contract of 'processors' is as follows: | ||
// - their method 'process' is called with (config, event) | ||
// - the method... | ||
// - returns null: the processor does not feel responsible for the event | ||
// - throws Error: the 'error.toString()' is taken as the error message of processing the event | ||
// - returns object: this is taken as the result of processing the event | ||
// - returns promise: when the promise is resolved, this is taken as the result of processing the event | ||
const result = eventProcessorMapping.get(eventProcessorName)(routeConfig[eventProcessorName], event); | ||
if (result) { | ||
// be resilient against a processor returning a value instead of a promise: | ||
return Promise.resolve(result) | ||
.then(result => callback(null, result)) | ||
.catch(error => { | ||
console.log(error.stack); | ||
callback(error.toString()); | ||
}); | ||
} | ||
} catch (error) { | ||
if (error.stack) { | ||
console.log(error.stack); | ||
} | ||
callback(error.toString()); | ||
return; | ||
} | ||
} | ||
callback('No event processor found to handle this kind of event!'); | ||
} | ||
} | ||
|
||
function extractEventProcessorMapping(routeConfig) { | ||
const processorMap = new Map(); | ||
for (let key of Object.keys(routeConfig)) { | ||
try { | ||
processorMap.set(key, require(`./lib/${key}`)); | ||
} catch (error) { | ||
throw new Error(`The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`); | ||
} | ||
} | ||
return processorMap; | ||
} | ||
|
||
module.exports = {handler: handler}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use strict"; | ||
|
||
function process(snsConfig, event) { | ||
// detect if it's an sns-event at all: | ||
if(snsConfig.debug){ | ||
console.log('sns:Event', JSON.stringify(event)); | ||
} | ||
|
||
if (!Array.isArray(event.Records) || event.Records.length<1 || !event.Records[0].Sns) { | ||
console.log('Event does not look like SNS'); | ||
return null; | ||
} | ||
|
||
const sns = event.Records[0].Sns; | ||
for(let routeConfig of snsConfig.routes){ | ||
if(routeConfig.subject instanceof RegExp){ | ||
if(routeConfig.subject.test(sns.Subject)){ | ||
const result = routeConfig.action(sns); | ||
return result || {}; | ||
} | ||
}else{ | ||
console.log(`SNS-Route with subject-regex '${routeConfig.subject}' is not a Regex; it is ignored!`); | ||
} | ||
} | ||
|
||
if (snsConfig.debug) { | ||
console.log(`No subject-match for ${sns.Subject}`); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
module.exports = process; | ||
|
||
/* | ||
const cfgExample = { | ||
routes:[ | ||
{ | ||
subject: /.*\/, | ||
action: sns => articleService.invalidate(JSON.parse(sns.Message).escenicId) | ||
} | ||
] | ||
}; | ||
*/ | ||
|
||
|
||
/* this is an example for a standard SNS notification message: | ||
{ | ||
"Records": [ | ||
{ | ||
"EventSource": "aws:sns", | ||
"EventVersion": "1.0", | ||
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:933782373565:production-escenic-updates:2fdd994c-f2b7-4c2f-a2f9-da83b590e0fc", | ||
"Sns": { | ||
"Type": "Notification", | ||
"MessageId": "0629603b-448e-5366-88b4-309d651495c5", | ||
"TopicArn": "arn:aws:sns:eu-west-1:933782373565:production-escenic-updates", | ||
"Subject": null, | ||
"Message": "{\"escenicId\":\"159526803\",\"model\":\"news\",\"status\":\"draft\"}", | ||
"Timestamp": "2016-11-16T08:56:58.227Z", | ||
"SignatureVersion": "1", | ||
"Signature": "dtXM9BlAJJhYkVObnKmzY012kjgl4uYHEPQ1DLUalBHnPNzkDf12YeVcvHmq0SF6QbdgGwSYw0SgtsOkBiW3WSxVosqEb5xKUWIbQhlXwKdZnzekUigsgl3d231RP+9U2Cvd4QUc6klH5P+CuQM/F70LBIIv74UmR2YNMaxWxrv7Q+ETmz/TF6Y5v8Ip3+GLikbu6wQ/F5g3IHO2Lm7cLpV/74odm48SQxoolh94TdgvtYaUnxNjFVlF8Js8trbRkr7DYTogh73cTwuR77Mo+K9GlYn53txiMW5rMl3KhVdw4U3L190gtBJVwgHbqcB60pmNdEAE9f4bEOohizfPhg==", | ||
"SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-b95095beb82e8f6a046b3aafc7f4149a.pem", | ||
"UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:933782373565:production-escenic-updates:2fdd994c-f2b7-4c2f-a2f9-da83b590e0fc", | ||
"MessageAttributes": {} | ||
} | ||
} | ||
] | ||
} | ||
*/ |
This file contains 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
Oops, something went wrong.