jdb
Version:
A database that hacks.
202 lines (197 loc) • 5.44 kB
JavaScript
// Generated by CoffeeScript 1.9.3
var Jdb;
Jdb = require('./jdb');
/*
JDB.Server
*/
module.exports = function() {
var ego, self;
self = {};
ego = {
opts: {
interactive: false,
dbPath: 'jdb.db',
promise: false,
port: 8137,
host: '127.0.0.1',
compactDBFile: true,
config_path: null
},
init: function() {
ego.init_options();
ego.jdb = Jdb();
return ego.jdb.init(ego.opts).then(function() {
if (ego.opts.interactive) {
return ego.init_interactive();
} else {
return ego.init_server();
}
});
},
init_options: function() {
var cmder, conf, config, defaults;
cmder = require('commander');
cmder.usage('[options] [config.json or config.js]').option('-d, --dbPath <path>', 'Data base file path').option('-i, --interactive', 'Start with interactive mode').option('-p, --port <port>', 'Port to listen to. Default is ' + ego.opts.port, parseInt).option('--host <host>', "Host to listen to. Default is " + ego.opts.host + " only").option('-c, --compactDBFile <true>', 'Whether compact db file at start up or not', function(data) {
return data === 'true';
}).option('-v, --ver', 'Print JDB version').parse(process.argv);
if (cmder.ver) {
conf = require('../package');
console.log('JDB v' + conf.version);
process.exit();
}
if (cmder.args[0]) {
ego.opts.config_path = cmder.args[0];
}
if (ego.opts.config_path) {
config = require(ego.opts.config_path);
}
defaults = function(opts) {
var k, ref, results, v;
if (!opts) {
return;
}
ref = ego.opts;
results = [];
for (k in ref) {
v = ref[k];
if (opts[k]) {
results.push(ego.opts[k] = opts[k]);
} else {
results.push(void 0);
}
}
return results;
};
defaults(config);
return defaults(cmder);
},
init_interactive: function() {
global.save = function() {
ego.jdb.exec({
data: global.doc,
command: function(jdb, data) {
jdb.doc = data;
return jdb.save();
},
callback: function() {
return process.stdout.write('JDB> saved\ncoffee> ');
}
});
};
return ego.jdb.exec({
command: function(jdb) {
return jdb.send(jdb.doc);
},
callback: function(err, doc) {
var cmd;
global.doc = doc;
process.argv = [];
cmd = require('coffee-script/lib/coffee-script/command');
return cmd.run();
}
});
},
init_server: function() {
var http;
http = require('http');
ego.server = http.createServer(ego.init_routes);
ego.server.listen(ego.opts.port, ego.opts.host);
return ego.log("Listen: " + ego.opts.host + ":" + ego.opts.port);
},
init_routes: function(req, res) {
var ht;
ht = {
req: req,
res: res
};
switch (req.url) {
case '/exec':
return ego.exec(ht);
case '/compactDBFile':
return ego.compactDBFile(ht);
default:
return ego.not_found(ht);
}
},
log: function(msg, level) {
if (level == null) {
level = 0;
}
return console.log(">>", msg);
},
send: function(ht, body, status, type) {
var buf;
if (body == null) {
body = '';
}
if (status == null) {
status = 200;
}
if (type == null) {
type = 'application/json; charset=utf-8';
}
buf = new Buffer(body);
ht.res.writeHead(status, {
'Content-Type': type,
'Content-Length': buf.length,
'X-Powered-By': 'jdb'
});
return ht.res.end(buf);
},
exec: function(ht) {
var body;
body = '';
ht.req.on('data', function(chunk) {
return body += chunk;
});
return ht.req.on('end', function() {
var cmd, command, e;
try {
cmd = JSON.parse(body);
} catch (_error) {
e = _error;
ego.send(ht, "JSON syntax error: \n" + body, 500);
return;
}
if (!cmd.command) {
ego.send(ht, 'Empty command', 403);
return;
}
try {
command = eval("(" + cmd.command + ")");
} catch (_error) {
e = _error;
ego.send(ht, 'Command syntax error: \n' + cmd.command, 500);
return;
}
return ego.jdb.exec({
data: cmd.data,
command: command,
callback: function(err, data) {
if (err) {
return ego.send(ht, JSON.stringify({
error: err.message
}), 500);
} else {
return ego.send(ht, JSON.stringify(data || 'ok'));
}
}
});
});
},
compactDBFile: function(ht) {
return ego.jdb.compactDBFile.then(function(err) {
return ego.send(ht, 'OK');
})["catch"](function(err) {
return ego.send(ht, JSON.stringify({
error: err.message
}), 500);
});
},
not_found: function(ht) {
return ego.send(ht, 'not found', 404);
}
};
ego.init();
return self;
};