Skip to content

Instantly share code, notes, and snippets.

@rwboyer
Last active March 31, 2019 23:03
Show Gist options
  • Select an option

  • Save rwboyer/bc42e8477a8540d177def3339e945a3a to your computer and use it in GitHub Desktop.

Select an option

Save rwboyer/bc42e8477a8540d177def3339e945a3a to your computer and use it in GitHub Desktop.
const { json, send } = require('micro');
const cors = require('micro-cors')();
const fetch = require('node-fetch');
const parse = require('urlencoded-body-parser');
const crypto = require('crypto');
const Mailgun = require('mailgun-js');
const db = require('../../models/db');
const el = require('../../models/email-list');
const dl = require('../../models/dl-item');
const email = require('../../email-template');
const secret = process.env.SECRET;
const mg_api_key = process.env.MAILGUN_API;
const mg_url = process.env.MAILGUN_URL;
const mg_from = process.env.MAILGUN_FROM;
const download_url = process.env.DOWNLOAD_URL;
module.exports = cors(async function (req, res){
//parse body
try{
data = await parse(req);
}
catch(e){
res.statusCode = 500;
res.end(JSON.stringify(e));
return;
}
//verify recaptcha
var grec = {
"secret": secret,
"response": data["g-recaptcha-response"]
};
var verifyURL = `https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${data["g-recaptcha-response"]}`;
// strange hybrid await + .then promise handling is for promise debugging only - fix now that it's working
const v = await fetch(verifyURL, {
method: 'post',
body: JSON.stringify(grec),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => json);
if(process.env.DEBUG){
console.log(v);
}
else if(!v.success){
console.log(v);
return await send(res, 400, JSON.stringify(v));
}
// external mailing list DB update
q = await el.find({ email: data.email.toLowerCase()});
if(! (q && q.length)){
try{
el_item = new el({ email: data.email.toLowerCase(), download: "printing ebook", count: 1 });
new_el_item = await el_item.save();
}
catch(e){
console.log(e);
}
}else{
q[0].count = q[0].count + 1;
el_item = await q[0].save();
}
// create download key used for secure expiring AWS S3 link later
code = crypto.createHash('md5').update(data.email.toLowerCase() + secret + (new Date()).toISOString()).digest("hex");
try{
dl_item = new dl({ code: code, email: data.email.toLowerCase(), download: "printing ebook", count: 5 });
new_dl_item = await dl_item.save();
}
catch(e){
console.log("Duplicate MD5 Hash: " + code);
return await send(res, 500, JSON.stringify(e));
}
// send fake link reference via email with mailgun
var mg = new Mailgun({apiKey: mg_api_key, domain: mg_url});
console.log(mg);
var data = {
from: mg_from,
to: data.email,
subject: 'Hello from LPFA',
html: email(download_url + dl_item.code)
}
// strange hybrid await hybrid only to debug promise - fix at some point now that it's working.
const p = await mg.messages().send(data).then(function (d) {
console.log("MAILGUN FINISHED");
console.log(d);
}, function (err) {
console.log("got an error: ");
console.log(err);
return err;
});
// remove now that everything is working
console.log("MAILGUN AFTER... PROBLEM WITH PROMISE?");
console.log(p);
if (p) {
await send(res, 500, JSON.stringify(p));
}
else{
await send(res, 200, JSON.stringify(data));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment