diff --git a/index.js b/index.js index c762d51..fe76164 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,9 @@ const db = new Redis({ }) module.exports = { app, db, globalConfig } // Export database connection for other files +app.use(express.json()) // Allows receiving JSON bodies +// see important note: https://expressjs.com/en/api.html#express.json + fs.readdir(globalConfig.routesDirectory, (err, files) => { if (err) { console.log("FATAL: Unable to read ./routes") diff --git a/routes/azure.js b/routes/azure.js new file mode 100644 index 0000000..20e4ed0 --- /dev/null +++ b/routes/azure.js @@ -0,0 +1,50 @@ +const { app, db, globalConfig } = require("../index.js") // Get globals from index +const { EmailClient } = require("@azure/communication-email"); +const azureEmail = new EmailClient(globalConfig.azure.connectionString) + + + +app.post("/sendemail", (rreq,rres) => { + + db.get(globalConfig.azure.authKeyInDb).then(dbres => { + if (dbres == null) { + console.log("ERROR: Configured key containing azure authkeys is null") + rres.sendStatus(500) + } else { + let validKeys = dbres.split(',') + if (validKeys.includes(rreq.get("Authorization"))) { + + let message = { + senderAddress: globalConfig.azure.senderAddress, + content: { + subject: rreq.body.message.subject, + plainText: rreq.body.message.content, + }, + recipients: { + to: [ + { + address: rreq.body.recipient.emailAddr, + displayName: rreq.body.recipient.emailName, + }, + ], + }, + } + + azureEmail.beginSend(message).then(ares => { + rres.sendStatus(200) + }).catch(err => { + rres.sendStatus(500) + console.log(err) + }) + + } else { + console.log(`${rreq.ip} POST /sendemail returned 401`) + rres.sendStatus(401) + } + } + }) + +}) + + +module.exports = {app} // export routes to be imported by index for execution \ No newline at end of file diff --git a/routes/notifications.js b/routes/notifications.js deleted file mode 100644 index ce33768..0000000 --- a/routes/notifications.js +++ /dev/null @@ -1,5 +0,0 @@ -const { app, db, globalConfig } = require("../index.js") // Get globals from index - - - -module.exports = {app} // export routes to be imported by index for execution \ No newline at end of file