refactored

This commit is contained in:
rjrunner44 2020-09-19 13:46:47 -07:00
parent 84c37f24a6
commit fba0d48e04
3 changed files with 22 additions and 22 deletions

View File

@ -67,9 +67,10 @@ Url Checker returns size and additional information about requested file. This s
### Report
Report endpoints sends an email via SendGrid or MailGun mail delivery service.
Set delivery service to use via `MAILSERVICE` environment variable, supported values: 'sendgrid' or 'mailgun' (defaults to sendgrid mailservice if mailgun is not specified for backwards compatibility)
Set delivery service to use via `MAILSERVICE` environment variable, supported values: 'SendGrid' or 'MailGun' (defaults to SendGrid if missing or not set or backwards compatibility)
for SendGrid, requires `SENDGRID_API_KEY` set as environment variable.
for MailGun, requires `MAILGUN_API_KEY` and `MAILGUN_DOMAIN` set as environment variables.
**Endpoint:** POST `/api/v1/report`
**Request Parameters**

View File

@ -5,7 +5,7 @@ const config = {
sendgridApiKey: process.env.SENDGRID_API_KEY || '',
mailgunApiKey: process.env.MAILGUN_API_KEY || '',
mailgunDomain: process.env.MAILGUN_DOMAIN || '',
mailService: process.env.MAILSERVICE || 'sendgrid', // use either mailgun or sendgrid
mailService: process.env.MAILSERVICE || 'SendGrid', // use either SendGrid or MailGun
ipfsGatewayUri: process.env.IPFS_GATEWAY_URI || 'https://gateway.ipfs.io'
}

View File

@ -1,7 +1,7 @@
import { Router, Request, Response } from 'express'
import config from '../config'
import SendgridMail from '@sendgrid/mail'
var mailgun = require('mailgun-js')({ apiKey: config.mailgunApiKey, domain: config.mailgunDomain });
var mailgun = require('mailgun-js');
export class ReportRouter {
public router: Router
@ -14,34 +14,33 @@ export class ReportRouter {
if (!req.body.msg) {
return res.send({ status: 'error', message: 'missing message' })
}
if (config.mailService === 'mailgun') {
const data = {
"from": req.body.msg.from,
"to": req.body.msg.to,
"subject": req.body.msg.subject + " (via " + config.mailService + ")",
"text": req.body.msg.text,
"html": req.body.msg.html
};
try {
await mailgun.messages().send(data, (error, body) => {
try {
if (config.mailService === 'MailGun') {
const data = {
"from": req.body.msg.from,
"to": req.body.msg.to,
"subject": req.body.msg.subject,
"text": req.body.msg.text,
"html": req.body.msg.html
};
var x = config.mailgunApiKey;
var y = config.mailgunDomain;
const mg = mailgun({ apiKey: config.mailgunApiKey, domain: config.mailgunDomain });
await mg.messages().send(data, (error, body) => {
if (error) console.log(error)
else return res.send({ status: 'success' });
});
} catch (error) {
console.error(`${error.code} - ${error.message}`) // eslint-disable-line
res.send(`${error.code} - ${error.message}`)
}
}
else { // default back to sendgrid
try {
else {
SendgridMail.setApiKey(config.sendgridApiKey)
await SendgridMail.send(req.body.msg)
return res.send({ status: 'success' })
} catch (error) {
console.error(`${error.code} - ${error.message}`) // eslint-disable-line
res.send(`${error.code} - ${error.message}`)
}
}
catch (error) {
console.error(`${error.code} - ${error.message}`) // eslint-disable-line
res.send(`${error.code} - ${error.message}`)
}
}
public init() {