summaryrefslogtreecommitdiffstats
path: root/lib/paste.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/paste.js')
-rw-r--r--lib/paste.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/paste.js b/lib/paste.js
new file mode 100644
index 0000000..a60bee2
--- /dev/null
+++ b/lib/paste.js
@@ -0,0 +1,68 @@
+var kyoto = require('kyoto'),
+uuid = require(__dirname + '/uuid');
+
+var db;
+
+var generateId = function(callback) {
+ var id = uuid.generate(14);
+
+ db.get(id, function(err, value) {
+ if (value) {
+ generateId();
+ }
+ else {
+ callback(id);
+ }
+ });
+};
+
+var get = function(id, callback) {
+ db.get(id, function(err, value) {
+ if (value) {
+ value = JSON.parse(value);
+ }
+
+ callback(value);
+ });
+};
+
+var add = function(post, callback) {
+ generateId(function(id) {
+ var data = {
+ content: post.content,
+ language: post.language,
+ time: new Date()
+ };
+
+ db.set(id, JSON.stringify(data), function(err) {
+ callback(err, id);
+ });
+ });
+};
+
+var init = function(config, callback) {
+ db = new kyoto.open(config.database, 'a+', function(err) {
+ if (err) throw err;
+
+ process.on('uncaughtException', function(exeption) {
+ console.error('%j', exeption);
+ process.exit(1);
+ });
+
+ process.on('exit', function() {
+ db.close(function(err) { console.log(err); });
+ });
+
+ var thismodule = {
+ get: get,
+ add: add
+ };
+
+ callback(thismodule);
+
+ });
+};
+
+module.exports = {
+ init: init
+};