this took 10 min why did I put it off for days
This commit is contained in:
3
index.js
3
index.js
@@ -11,6 +11,9 @@ const db = new Redis({
|
|||||||
})
|
})
|
||||||
module.exports = { app, db, globalConfig } // Export database connection for other files
|
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) => {
|
fs.readdir(globalConfig.routesDirectory, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("FATAL: Unable to read ./routes")
|
console.log("FATAL: Unable to read ./routes")
|
||||||
|
|||||||
50
routes/azure.js
Normal file
50
routes/azure.js
Normal file
@@ -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
|
||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user