forked from rbrcurtis/Brewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
executable file
·166 lines (147 loc) · 4.05 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
#!/usr/bin/env coffee
sys = require 'sys'
fs = require 'fs'
URL = require 'url'
http = require 'http'
qs = require 'querystring'
util = require 'util'
coffee = require 'coffee-script'
exec = require('child_process').exec
config = {}
trim = (str)->str.replace(/(^\s+|\s+$)/,'')
processData = (data, method, response) ->
out = ""
params = qs.parse data
console.dir "processing req #{method}:"
for key,val of params
console.log "\t#{key}=#{val}"
try
console.log "processing request for #{params.action}"
switch method
when 'POST'
switch params.action
when null
out = "specify the action yo"
when 'compile'
out = coffee.compile params.content
when 'fileList'
list = {}
fl = fs.readdirSync params.path
for f in fl
try
stats = fs.statSync "#{params.path}/#{f}"
if stats.isFile() and f.indexOf('.coffee')<1
console.log "skipping #{f}"
continue
list[f] = if stats.isDirectory() then 'directory' else 'file'
catch e
console.log e
out = list
console.log("got file list:#{out.toString()}")
when 'log'
console.log "LOG:#{params.message}"
out = ""
when 'getConfig'
out = {config:config}
when 'save'
if params.fn is null
out = "filename missing"
else if params.content is null
out = "no file content found!"
else
console.log "saving file #{params.fn}"
fs.writeFileSync params.fn, params.content
out = "save complete"
when 'open'
if params.fn is null
out = "filename missing"
else if params.content is null
out = "no file content found!"
else
console.log "opening file #{params.fn}"
content = fs.readFileSync params.fn, 'utf8'
console.log "got content \n #{content}"
out = {content:content}
updateLastFilesConfig('add', params.fn)
saveConfig()
when 'close'
if params.fn is null
out = "filename missing"
else
updateLastFilesConfig 'del', params.fn
catch e
console.log e.stack
out = e.message
out = JSON.stringify out
response.end out
console.log "sending back #{out}"
loadConfig = ->
fs.readFile ".brewer.conf", (err,data) ->
try
if err then throw err
else
config = JSON.parse(data)
console.log "config loaded"
catch e
exec 'cd;pwd',(e,so,se)->
config.home = trim(so)
config.lastFiles = []
console.log "home folder is #{config.home}"
saveConfig()
saveConfig = ->
fs.writeFile ".brewer.conf", JSON.stringify(config), (err) ->
throw err if err
console.log "config saved"
updateLastFilesConfig = (action, fn) ->
console.log "updateLastFileConfig #{action} #{fn}"
console.log "lastFiles before update:#{config.lastFiles}"
try
for i,f of config.lastFiles
console.log "#{f} vs #{fn}"
if f is fn
if action is 'add'
return
else if action is 'del'
config.lastFiles.splice i, 1
if action is 'add'
config.lastFiles.push fn
finally
console.log "lastFiles after update:#{config.lastFiles}"
############ MAIN ############
loadConfig()
http.createServer( (req, res) ->
try
# console.log req.url
url = URL.parse(".#{req.url}").pathname
console.log "url: #{url}"
method = req.method
console.log "method:#{method}"
data = null
req.addListener 'data',(d) ->
data = d.toString()
# console.log "data from #{method}:#{data}"
req.addListener 'end', ->
if data? and method isnt "GET"
console.log "data:#{data}"
res.writeHead(200, {'Content-Type': 'application/json'});
processData(data, method, res)
else
try
res.writeHead(200, {'Content-Type': 'text/html'});
stats = fs.statSync(url)
# console.log stats
if stats.isDirectory()
# console.log 'isDir'
url = url+'/index.html'
url = url.replace '//', '/'
res.end fs.readFileSync url
else
res.end fs.readFileSync url
catch e
console.error e.message
res.end e.message
catch e
console.error e.message
res.end e.message
).listen(8000);
console.log 'server started'