Last active
February 10, 2019 20:52
-
-
Save ilgooz/57a52022b129439e8322f73fe4cb2ac4 to your computer and use it in GitHub Desktop.
MESG Live Coding Session - Subscription App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<strong>Welcome</strong> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="subscription"> | |
<label for="email">Give me your email:</label> | |
<input id="email" name="email" value=""> | |
<input type="submit" value="Subscribe!"> | |
</form> | |
<script> | |
var formSelector = document.getElementById("subscription") | |
var emailSelector = document.getElementById("email") | |
formSelector.addEventListener("submit", (event) => { | |
event.preventDefault(); | |
var req = new XMLHttpRequest() | |
req.addEventListener("load", (event) => { | |
alert('subscribed!'); | |
}); | |
req.addEventListener("error", (event) => { | |
alert('oops! something went wrong.') | |
}) | |
req.open("POST", "http://localhost:2300/api/subscribe" | |
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8") | |
req.send(JSON.stringify({ email: emailSelector.value })) | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// services used in this app: | |
// https://github.com/ilgooz/service-http-server | |
// https://github.com/mesg-foundation/service-email-sendgrid | |
// https://github.com/mesg-foundation/service-influxdb | |
const mesg = require('mesg-js').application() | |
const path = require('path') | |
const fs = require('fs') | |
const sengridAPIKey = 'YOUR_KEY_GOES_HERE_:)' | |
mesg.listenEvent({ | |
serviceID: 'http-server', | |
eventFilter: 'request' | |
}) | |
.on('data', (event) => { | |
const data = JSON.parse(event.eventData) | |
switch (data.method) { | |
case 'GET': | |
serveStatic(data) | |
break; | |
case 'POST': | |
serveAPI(data) | |
break; | |
} | |
}) | |
.on('error', (err) => { | |
console.error('error while listening for request event:', err) | |
}) | |
function serveAPI(data){ | |
const email = JSON.parse(data.body).email | |
const apiResponse = mesg.executeTask({ | |
serviceID: 'http-server', | |
taskKey: 'completeSession', | |
inputData: JSON.stringify({ | |
sessionID: data.sessionID, | |
mimeType: 'application/json', | |
code: 202, | |
content: JSON.stringify({ email: email }) | |
}) | |
}) | |
const sendingEmail = mesg.executeTask({ | |
serviceID: 'email-sendgrid', | |
taskKey: 'send', | |
inputData: JSON.stringify({ | |
apiKey: sengridAPIKey, | |
from: '[email protected]', | |
to: email, | |
subject: 'You just subscribed to MESG!', | |
text: `You'll hear from us!` | |
}) | |
}) | |
const saveMetric = mesg.executeTask({ | |
serviceID: 'influxdb', | |
taskKey: 'write', | |
inputData: JSON.stringify({ | |
measurement: 'new_subscriptions', | |
fields: { count: 1 }, | |
tags: { email: email } | |
}) | |
}) | |
Promise.all([apiResponse, sendingEmail, saveMetric]) | |
.then(() => { | |
console.log('a new subscription made for:', email) | |
}) | |
.catch((err) => { | |
console.error('subscription is failed with err:', err) | |
}) | |
} | |
function serveStatic(data) { | |
const name = data.path == '/' ? 'index' : data.path.split('/')[1] | |
const filePath = path.resolve(__dirname, 'static-'+name+'.html') | |
try { | |
const content = fs.readFileSync(filePath, 'utf8') | |
mesg.executeTask({ | |
serviceID: 'http-server', | |
taskKey: 'completeSession', | |
inputData: JSON.stringify({ | |
sessionID: data.sessionID, | |
mimeType: 'text/html', | |
content: content | |
}) | |
}).catch((e) => { | |
console.error('error while sending a response to http request:', e) | |
}) | |
} catch(e) { | |
console.log('file not found for:', name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to deploy and start services
to run the application
mesg-js
via npm and then runsubscription.js
with node!usage
http://localhost:2300/subscribe
and submit with an email to create a new subscription.http://localhost:3000
to create and see subscription graphs via Grafana.flow