UNPKG

jdb

Version:

A database that hacks.

172 lines (166 loc) 4.76 kB
// Generated by CoffeeScript 1.7.1 require('../'); /* JDB.Server */ JDB.Server = (function() { function Server() { var ego, k, self, v; self = {}; ego = { opts: { db_path: 'jdb.db', port: 8137, host: '127.0.0.1', compact_db_file: true, config_path: null }, init: function() { ego.init_options(); ego.jdb = new JDB.Jdb(ego.opts); return ego.init_server(); }, init_options: function() { var commander, config, defaults; commander = require('commander'); commander.usage('[options] [config_path.json or config_path.js]').option('-d, --db_path <path>', 'Data base file path').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, --compact_db_file', 'Whether compact db file at start up or not').parse(process.argv); if (commander.args[0]) { ego.opts.config_path = commander.args[0]; } if (ego.opts.config_path) { config = require(ego.opts.config_path); } defaults = function(opts) { var k, v, _ref, _results; 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(commander); }, 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; ego.log(req.url); ht = { req: req, res: res }; switch (req.url) { case '/favicon.ico': return ego.favicon(ht); case '/exec': return ego.exec(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) { if (body == null) { body = ''; } if (status == null) { status = 200; } if (type == null) { type = 'application/json'; } ht.res.writeHead(status, { 'Content-Type': type, 'Content-Length': body.length }); return ht.res.end(body); }, favicon: function(ht) { var smallest_gif; smallest_gif = new Buffer('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==', 'base64'); return ego.send(ht, smallest_gif, 200, 'image/x-icon'); }, exec: function(ht) { var body; if (ht.req.method !== "POST") { return ego.not_found(ht); } else { 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) { data = { error: err.message }; } return ego.send(ht, JSON.stringify(data)); } }); }); } }, not_found: function(ht) { return ego.send(ht, 'not found', 404); } }; for (k in self) { v = self[k]; this[k] = v; } self = this; for (k in ego) { v = ego[k]; if (typeof v === 'function') { v.bind(self); } } ego.init(); return self; } return Server; })();