Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mattcam/19fcd2a9c606174acafa39a607c00499 to your computer and use it in GitHub Desktop.
Save mattcam/19fcd2a9c606174acafa39a607c00499 to your computer and use it in GitHub Desktop.
// (1) function to get KV variables
const _ENV = () => [replace-with-namespace-name].get('_ENV');
let ENV={};
// (2) listen for a request
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// (3) function to log to LOGDNA
async function log(message) {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json; charset=UTF-8');
myHeaders.append('Authorization', 'Basic ' + btoa( ENV.LOGDNA_API + ':' + ''));
let response = await fetch('https://logs.logdna.com/logs/ingest?hostname=' + ENV.LOGDNA_HOSTNAME, {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(
{"lines": [{"line":message,"app":ENV.LOGDNA_HOSTNAME,"level": "INFO","env": "production","meta": {"customfield": {"nestedfield": "nestedvalue"}}}]}
)
});
return response;
}
// (4) function to add subscriber to Mailchimp
async function addSubscriber(email) {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json; charset=UTF-8');
myHeaders.append('Authorization', 'Basic ' + btoa( '' + ':' + ENV.MAILCHIMP_API));
let response = await fetch('https://us3.api.mailchimp.com/3.0/lists/' + ENV.MAILCHIMP_LIST + '/members/', {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(
{'email_address':email,status:'subscribed'}
)
});
console.log('*** ',response.statusText);
// await log(JSON.stringify(response.statusText));
}
// (5) main function that handles request
async function handleRequest(request) {
// (6) get environment variables from KV
ENV.raw = await _ENV();
ENV.LOGDNA_API = JSON.parse(ENV.raw).LOGDNA_API;
ENV.LOGDNA_HOSTNAME = JSON.parse(ENV.raw).LOGDNA_HOSTNAME;
ENV.LOGDNA_APPNAME = JSON.parse(ENV.raw).LOGDNA_APPNAME;
ENV.MAILCHIMP_API = JSON.parse(ENV.raw).MAILCHIMP_API;
ENV.MAILCHIMP_LIST = JSON.parse(ENV.raw).MAILCHIMP_LIST;
// (7) define default worker response
const init = {
headers: { 'content-type': 'text/plain; charset=us-ascii' },
status: 204,
statusText: 'success'
}
try {
// (8) get request (triggered from Ghost callback)
var body = await request.json();
// (9) validate payload on request
if(body != null || body != undefined) {
if ( (typeof body.member !== 'undefined') && (typeof body.member.current !== 'undefined') && (typeof body.member.current.email !== 'undefined') ) {
// (10) add subscriber to mailchimp
await addSubscriber(body.member.current.email);
}
}
}
catch(err) {
// (11) handle failure
init.status=404;
init.statusText="failure";
}
// (12) return null response, header contains response codes
return new Response(null, init)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment