Created
December 8, 2021 19:32
-
-
Save DennyLoko/49cbd901da36bfcca279ac25b304f3b4 to your computer and use it in GitHub Desktop.
AWS SNS automatic topic subscription accepter
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
const express = require('express') | |
const request = require('request') | |
const bodyParser = require('body-parser') | |
const app = express() | |
const port = process.env.PORT | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.use(bodyParser.json()) | |
app.post('/*', (req, res) => { | |
let body = '' | |
req.on('data', (chunk) => { | |
body += chunk.toString() | |
}) | |
req.on('end', () => { | |
let payload = JSON.parse(body) | |
if (payload.Type === 'SubscriptionConfirmation') { | |
const promise = new Promise((resolve, reject) => { | |
const url = payload.SubscribeURL | |
const topic = payload.TopicArn | |
const token = payload.Token | |
console.log(`Token: ${token} | Topic: ${topic}`) | |
console.log(`URL: ${url}`) | |
request(url, (error, response) => { | |
if (!error && response.statusCode == 200) { | |
console.log(`Confirmation accepted from AWS for topic ${topic}`) | |
return resolve() | |
} else { | |
return reject() | |
} | |
}) | |
}) | |
promise.then(() => { | |
res.status(200).json({ success: true, path: req.path }) | |
}) | |
} | |
}) | |
}) | |
app.get('/*', (req, res) => { | |
res.status(200).json({ success: true, path: req.path }) | |
}) | |
app.listen(port, () => console.log(`App listening on port ${port}`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment