diff --git a/config.js b/config.js index bf70bf6..aed56bc 100644 --- a/config.js +++ b/config.js @@ -4,6 +4,10 @@ "keyLength": 5, "keySpace": "abcdefghijklmnopqrstuvwxyz0123456789", "staticMaxAge": 86400, + "mongodbUsername": "ButlerBot", + "mongodbPassword": "YzSwi2j2Pp6m5ln0", + "mongodbDbName": "butler_db", + "mongodbCollectionName": "pastes-test", "logging": [ { "level": "verbose", diff --git a/lib/document_store.js b/lib/document_store.js index 1c2e546..bb6d55b 100644 --- a/lib/document_store.js +++ b/lib/document_store.js @@ -1,47 +1,62 @@ -var fs = require("fs"); -var crypto = require("crypto"); +var MongoClient = require("mongodb").MongoClient +var Winston = require("winston") -var FileDocumentStore = function() { - this.basePath = "/data"; - this.expire = null; -}; +class MongoDocumentStore { -FileDocumentStore.md5 = function(str) { - var md5sum = crypto.createHash("md5"); - md5sum.update(str); - return md5sum.digest("hex"); -}; + constructor(options) { + this.connectionURL = `mongodb+srv://${options.username}:${options.password}@cluster0-vycgu.mongodb.net/${options.dbName}?retryWrites=true&w=majority`; + this.dbName = options.dbName; + this.collectionName = options.collectionName; + } -FileDocumentStore.prototype.set = function(key, data, callback) { - try { + set(key, data, callback) { var _this = this; - fs.mkdir(this.basePath, "700", function() { - var fn = _this.basePath + "/" + FileDocumentStore.md5(key); - fs.writeFile(fn, data, "utf8", function(err) { + const client = new MongoClient(this.connectionURL, { useUnifiedTopology: true, useNewUrlParser: true }); + client.connect(err => { + if (err) { + callback(false); + Winston.error("Error connecting to MongoDB database.", { error: err }); + return; + } + const db = client.db(_this.dbName); + const collection = db.collection(_this.collectionName); + + collection.insertOne({key: key, data: data}, (err, result) => { if (err) { callback(false); + Winston.error("Error inserting data into MongoDB database.", { error: error }); + return; } - else { - callback(true); - } + Winston.info("Inserted data into MongoDB database."); + callback(true); }); }); - } catch(err) { - callback(false); } -}; -FileDocumentStore.prototype.get = function(key, callback) { - var _this = this; - var fn = _this.basePath + "/" + FileDocumentStore.md5(key); - fs.readFile(fn, "utf8", function(err, data) { - if (err) { - callback(false); - } - else { - callback(data); - } - }); -}; + get(key, callback) { + var _this = this; + MongoClient.connect(this.connectionURL, { useUnifiedTopology: true }, function(err, client) { + if (err) { + callback(false); + Winston.error("Error connecting to MongoDB database.", { error: err }); + return; + } + const db = client.db(_this.dbName); + const collection = db.collection(_this.collectionName); + + collection.findOne({key: key}, (err, result) => { + if (err || !result) { + callback(false); + if (err) { + Winston.error("Error getting data from MongoDB database.", { error: error }); + } + return; + } + Winston.info("Retrieved data from MongoDB database."); + callback(result.data); + }); + }); + } +} -module.exports = FileDocumentStore; +module.exports = MongoDocumentStore; diff --git a/package.json b/package.json index 9e7ae2d..f813079 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,12 @@ }, "main": "butlerbin", "dependencies": { - "connect-route": "0.1.5", + "busboy": "0.3.1", "connect": "3.7.0", + "connect-route": "0.1.5", + "mongodb": "3.5.8", "st": "2.0.0", - "winston": "0.6.2", - "busboy": "0.3.1" + "winston": "0.6.2" }, "bundledDependencies": [], "engines": { diff --git a/server.js b/server.js index 5d815f3..2eb3400 100644 --- a/server.js +++ b/server.js @@ -31,7 +31,12 @@ if (config.logging) { } // Configure the data store. -store = new Store(); +store = new Store({ + username: config.mongodbUsername, + password: config.mongodbPassword, + dbName: config.mongodbDbName, + collectionName: config.mongodbCollectionName +}); // Pick up a key generator. var keyGenerator = new KeyGenerator({