Last active
January 2, 2016 20:29
-
-
Save jacwright/8356772 to your computer and use it in GitHub Desktop.
This file contains 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
// Required modules. | |
var cradle = require('cradle'); | |
var util = require('util'); | |
var http = require('http'); | |
// Config. | |
var config = require('./config'); | |
// Create a new HTTP server to accept Postmark POSTs. | |
http.createServer(function(req, res) { | |
// Variable to hold chunked data from Postmark. | |
var mailRaw = ''; | |
req.on('data', function(chunk) { | |
mailRaw += chunk; | |
}); | |
req.on('end', function() { | |
// Get the JSON payload from Postmark. | |
var mailJSON = JSON.parse(mailRaw); | |
// Transform attachments (fairly cheesy, but seems to work). | |
if(mailJSON.Attachments) { | |
mailJSON._attachments = {}; | |
mailJSON.Attachments.forEach(function(attachment) { | |
mailJSON._attachments[attachment.Name] = { | |
content_type: attachment.ContentType, | |
data: attachment.Content | |
}; | |
}); | |
delete mailJSON.Attachments; | |
} | |
// Connect to CouchDB instance. | |
var db = new (cradle.Connection)(config.couchHost, config.couchPort, { | |
auth : { | |
username : config.couchUserName, | |
password : config.couchPassword | |
} | |
}).database(config.couchDatabase); | |
// Insert new document. | |
db.save(mailJSON.MessageID, mailJSON, function (err, res) { | |
if(err) { | |
util.puts('Error! Could not save mailRaw. ' + err.reason); | |
} | |
else { | |
util.puts('Saved document with id: ' + res.id); | |
} | |
}); | |
// Send an empty response. | |
res.end(); | |
}); | |
}).listen(config.serverPort); |
This file contains 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
module.exports = { | |
serverPort: 8000, | |
couchHost: '127.0.0.1', | |
couchPort: 5984, | |
couchUserName: '', | |
couchPassword: '', | |
couchDatabase: 'couchmail' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment