1 |
|
2 | require('../');
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | JDB.Server = (function() {
|
10 | function Server() {
|
11 | var ego, k, self, v;
|
12 | self = {};
|
13 | ego = {
|
14 | opts: {
|
15 | interactive: false,
|
16 | dbPath: 'jdb.db',
|
17 | port: 8137,
|
18 | host: '127.0.0.1',
|
19 | compactDBFile: true,
|
20 | config_path: null
|
21 | },
|
22 | init: function() {
|
23 | ego.init_options();
|
24 | ego.jdb = new JDB.Jdb;
|
25 | return ego.jdb.init(ego.opts).done(function() {
|
26 | if (ego.opts.interactive) {
|
27 | return ego.init_interactive();
|
28 | } else {
|
29 | return ego.init_server();
|
30 | }
|
31 | });
|
32 | },
|
33 | init_options: function() {
|
34 | var cmder, conf, config, defaults;
|
35 | cmder = require('commander');
|
36 | 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) {
|
37 | return data === 'true';
|
38 | }).option('-v, --ver', 'Print JDB version').parse(process.argv);
|
39 | if (cmder.ver) {
|
40 | conf = require('../package');
|
41 | console.log('JDB v' + conf.version);
|
42 | process.exit();
|
43 | }
|
44 | if (cmder.args[0]) {
|
45 | ego.opts.config_path = cmder.args[0];
|
46 | }
|
47 | if (ego.opts.config_path) {
|
48 | config = require(ego.opts.config_path);
|
49 | }
|
50 | defaults = function(opts) {
|
51 | var k, v, _ref, _results;
|
52 | if (!opts) {
|
53 | return;
|
54 | }
|
55 | _ref = ego.opts;
|
56 | _results = [];
|
57 | for (k in _ref) {
|
58 | v = _ref[k];
|
59 | if (opts[k]) {
|
60 | _results.push(ego.opts[k] = opts[k]);
|
61 | } else {
|
62 | _results.push(void 0);
|
63 | }
|
64 | }
|
65 | return _results;
|
66 | };
|
67 | defaults(config);
|
68 | return defaults(cmder);
|
69 | },
|
70 | init_interactive: function() {
|
71 | global.save = function() {
|
72 | return ego.jdb.exec({
|
73 | data: global.doc,
|
74 | command: function(jdb, data) {
|
75 | jdb.doc = data;
|
76 | return jdb.save();
|
77 | }
|
78 | });
|
79 | };
|
80 | return ego.jdb.exec({
|
81 | command: function(jdb) {
|
82 | return jdb.send(jdb.doc);
|
83 | },
|
84 | callback: function(err, doc) {
|
85 | var cmd;
|
86 | global.doc = doc;
|
87 | process.argv = [];
|
88 | cmd = require('coffee-script/lib/coffee-script/command');
|
89 | return cmd.run();
|
90 | }
|
91 | });
|
92 | },
|
93 | init_server: function() {
|
94 | var http;
|
95 | http = require('http');
|
96 | ego.server = http.createServer(ego.init_routes);
|
97 | ego.server.listen(ego.opts.port, ego.opts.host);
|
98 | return ego.log("Listen: " + ego.opts.host + ":" + ego.opts.port);
|
99 | },
|
100 | init_routes: function(req, res) {
|
101 | var ht;
|
102 | ht = {
|
103 | req: req,
|
104 | res: res
|
105 | };
|
106 | switch (req.url) {
|
107 | case '/exec':
|
108 | return ego.exec(ht);
|
109 | case '/compactDBFile':
|
110 | return ego.compactDBFile(ht);
|
111 | default:
|
112 | return ego.not_found(ht);
|
113 | }
|
114 | },
|
115 | log: function(msg, level) {
|
116 | if (level == null) {
|
117 | level = 0;
|
118 | }
|
119 | return console.log(">>", msg);
|
120 | },
|
121 | send: function(ht, body, status, type) {
|
122 | var buf;
|
123 | if (body == null) {
|
124 | body = '';
|
125 | }
|
126 | if (status == null) {
|
127 | status = 200;
|
128 | }
|
129 | if (type == null) {
|
130 | type = 'application/json; charset=utf-8';
|
131 | }
|
132 | buf = new Buffer(body);
|
133 | ht.res.writeHead(status, {
|
134 | 'Content-Type': type,
|
135 | 'Content-Length': buf.length,
|
136 | 'X-Powered-By': 'jdb'
|
137 | });
|
138 | return ht.res.end(buf);
|
139 | },
|
140 | exec: function(ht) {
|
141 | var body;
|
142 | body = '';
|
143 | ht.req.on('data', function(chunk) {
|
144 | return body += chunk;
|
145 | });
|
146 | return ht.req.on('end', function() {
|
147 | var cmd, command, e;
|
148 | try {
|
149 | cmd = JSON.parse(body);
|
150 | } catch (_error) {
|
151 | e = _error;
|
152 | ego.send(ht, "JSON syntax error: \n" + body, 500);
|
153 | return;
|
154 | }
|
155 | if (!cmd.command) {
|
156 | ego.send(ht, 'Empty command', 403);
|
157 | return;
|
158 | }
|
159 | try {
|
160 | command = eval("(" + cmd.command + ")");
|
161 | } catch (_error) {
|
162 | e = _error;
|
163 | ego.send(ht, 'Command syntax error: \n' + cmd.command, 500);
|
164 | return;
|
165 | }
|
166 | return ego.jdb.exec({
|
167 | data: cmd.data,
|
168 | command: command,
|
169 | callback: function(err, data) {
|
170 | if (err) {
|
171 | return ego.send(ht, JSON.stringify({
|
172 | error: err.message
|
173 | }), 500);
|
174 | } else {
|
175 | return ego.send(ht, JSON.stringify(data || 'ok'));
|
176 | }
|
177 | }
|
178 | });
|
179 | });
|
180 | },
|
181 | compactDBFile: function(ht) {
|
182 | return ego.jdb.compactDBFile.then(function(err) {
|
183 | return ego.send(ht, 'OK');
|
184 | })["catch"](function(err) {
|
185 | return ego.send(ht, JSON.stringify({
|
186 | error: err.message
|
187 | }), 500);
|
188 | }).done();
|
189 | },
|
190 | not_found: function(ht) {
|
191 | return ego.send(ht, 'not found', 404);
|
192 | }
|
193 | };
|
194 | for (k in self) {
|
195 | v = self[k];
|
196 | this[k] = v;
|
197 | }
|
198 | self = this;
|
199 | for (k in ego) {
|
200 | v = ego[k];
|
201 | if (typeof v === 'function') {
|
202 | v.bind(self);
|
203 | }
|
204 | }
|
205 | ego.init();
|
206 | return self;
|
207 | }
|
208 |
|
209 | return Server;
|
210 |
|
211 | })();
|