Add MongoDB support to Hastebin.
TerribleCodeClub/ButlerBin/pipeline/head This commit looks good Details

This commit is contained in:
Jack Hadrill 2020-06-09 01:26:48 +01:00
parent a21a6c595a
commit f35eae30d5
4 changed files with 64 additions and 39 deletions

View File

@ -4,6 +4,10 @@
"keyLength": 5,
"keySpace": "abcdefghijklmnopqrstuvwxyz0123456789",
"staticMaxAge": 86400,
"mongodbUsername": "ButlerBot",
"mongodbPassword": "YzSwi2j2Pp6m5ln0",
"mongodbDbName": "butler_db",
"mongodbCollectionName": "pastes-test",
"logging": [
{
"level": "verbose",

View File

@ -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;

View File

@ -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": {

View File

@ -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({