-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.coffee
170 lines (138 loc) · 3.41 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Jdb = require './jdb'
###
JDB.Server
###
module.exports = ->
# Public
self = {}
# Private
ego = {
opts: {
interactive: false
dbPath: 'jdb.db'
promise: false
port: 8137
host: '127.0.0.1'
compactDBFile: true
config_path: null
}
init: ->
ego.init_options()
ego.jdb = Jdb()
ego.jdb.init ego.opts
.then ->
if ego.opts.interactive
ego.init_interactive()
else
ego.init_server()
init_options: ->
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', (data) ->
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 = (opts) ->
return if not opts
for k, v of ego.opts
if opts[k]
ego.opts[k] = opts[k]
defaults config
defaults cmder
init_interactive: ->
global.save = ->
ego.jdb.exec {
data: global.doc
command: (jdb, data) ->
jdb.doc = data
jdb.save()
callback: ->
process.stdout.write 'JDB> saved\ncoffee> '
}
return
ego.jdb.exec {
command: (jdb) ->
jdb.send jdb.doc
callback: (err, doc) ->
global.doc = doc
process.argv = []
cmd = require 'coffee-script/lib/coffee-script/command'
cmd.run()
}
init_server: ->
http = require 'http'
ego.server = http.createServer ego.init_routes
ego.server.listen ego.opts.port, ego.opts.host
ego.log "Listen: #{ego.opts.host}:#{ego.opts.port}"
init_routes: (req, res) ->
ht = { req, res }
switch req.url
when '/exec'
ego.exec ht
when '/compactDBFile'
ego.compactDBFile ht
else
ego.not_found ht
log: (msg, level = 0) ->
console.log ">>", msg
send: (ht, body = '', status = 200, 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'
}
ht.res.end buf
exec: (ht) ->
body = ''
ht.req.on 'data', (chunk) -> body += chunk
ht.req.on 'end', ->
try
cmd = JSON.parse body
catch e
ego.send ht, "JSON syntax error: \n" + body, 500
return
if not cmd.command
ego.send ht, 'Empty command', 403
return
try
command = eval "(#{cmd.command})"
catch e
ego.send ht, 'Command syntax error: \n' + cmd.command, 500
return
ego.jdb.exec
data: cmd.data
command: command
callback: (err, data) ->
if err
ego.send ht, JSON.stringify(
{ error: err.message }
), 500
else
ego.send ht, JSON.stringify(data or 'ok')
compactDBFile: (ht) ->
ego.jdb.compactDBFile
.then (err) ->
ego.send ht, 'OK'
.catch (err) ->
ego.send ht, JSON.stringify(
{ error: err.message }
), 500
not_found: (ht) ->
ego.send ht, 'not found', 404
}
ego.init()
self