]>
Commit | Line | Data |
---|---|---|
f0c1d1ca BB |
1 | #!/usr/bin/env node |
2 | ||
3 | require('colors'); | |
4 | var Ne = require('neon'); | |
5 | ||
6 | Nitrogen = { | |
7 | Manifest : require('./lib/manifest.js'), | |
8 | Server : {} | |
9 | }; | |
10 | ||
11 | Ne.Class(Nitrogen.Server, 'Editor')({ | |
12 | prototype : { | |
13 | fs : require('fs'), | |
14 | express : require('express'), | |
15 | httpProxy : require('http-proxy'), | |
16 | io : require('socket.io'), | |
17 | http : require('http'), | |
18 | app : null, | |
19 | server : null, | |
20 | socket : null, | |
21 | port : 4000, | |
22 | occipitalOptions : {}, | |
23 | ||
24 | init : function init(config) { | |
25 | Object.keys(config).forEach(function (property) { | |
26 | this[property] = config[property]; | |
27 | }, this); | |
28 | ||
29 | console.log('Nitrogen Session Server starting '); | |
30 | this.app = this.express(); | |
31 | this.proxy = new this.httpProxy.RoutingProxy(); | |
32 | this.server = this.http.createServer(this.app); | |
33 | this.initRack(); | |
34 | this.initDb(); | |
35 | this.initRoutes(); | |
36 | this.server.listen(this.port); | |
37 | this.socket = this.io.listen(this.server); | |
38 | this.initSyncEvents(); | |
39 | console.log('Nitrogen Session Server listening on port ' + this.port); | |
40 | }, | |
41 | ||
42 | initRack : function initRack() { | |
43 | var nitrogen = this; | |
44 | this.app.use(this.express.logger()); | |
45 | this.app.use(this.express.compress()); | |
46 | this.app.use(function (request, response, next) { | |
47 | response.header("Access-Control-Allow-Origin", "*"); | |
48 | response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); | |
49 | response.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); | |
50 | if ('OPTIONS' == request.method) { | |
51 | response.send(200); | |
52 | } | |
53 | else { | |
54 | next(); | |
55 | } | |
56 | }); | |
57 | // This has to be here in order for a proxied post to work. | |
58 | // See https://github.com/nodejitsu/node-http-proxy/issues/180 | |
59 | // for more information. | |
60 | this.app.use(function (request, response, next) { | |
61 | if (request.url.match(/\/attachments\/.*/) && request.method === "POST" ){ | |
62 | var options = nitrogen.occipitalOptions; | |
63 | nitrogen.proxy.proxyRequest( request, response, options ); | |
64 | } else { | |
65 | return next(); | |
66 | } | |
67 | }); | |
68 | this.app.use(this.express.bodyParser()); | |
69 | this.app.use('/', this.express.static('public')); | |
70 | }, | |
71 | ||
72 | initDb : function initDb() { | |
73 | var fileNames = this.fs.readdirSync(__dirname + '/../').filter(function(fn){ return fn.match(/.*\.json$/); }); | |
74 | var i, filePath, collectionName; | |
75 | data = Nitrogen.data = {}; | |
76 | ||
77 | for( i=0; i < fileNames.length; i+=1 ){ | |
78 | filePath = __dirname + '/../' + fileNames[i]; | |
79 | collectionName = fileNames[i].replace(/.json/, ''); | |
80 | data[collectionName] = this.fs.readFileSync(filePath).toString().replace(/\n$/, "").split(/\n/).map(function(r){ | |
81 | return JSON.parse(r); | |
82 | }); | |
83 | } | |
84 | }, | |
85 | ||
86 | initRoutes : function initRoutes() { | |
87 | var nitrogen = this, | |
88 | path = require('path'); | |
89 | ||
90 | this.app.get('/', function (request, response) { | |
91 | var file = path.resolve('./public/app.html'); | |
92 | response.sendfile(file); | |
93 | }); | |
94 | ||
95 | this.app.get('/api/all', function (req, res) { | |
96 | res.set('Content-Type', 'application/json'); | |
97 | res.send(JSON.stringify(data)); | |
98 | }); | |
99 | ||
100 | this.app.get('/api/:model', function (req, res) { | |
101 | var records = data[req.params.model] | |
102 | res.set('Content-Type', 'application/json'); | |
103 | res.send(JSON.stringify(records)); | |
104 | }); | |
105 | ||
106 | this.app.get('/api/:model/:id', function (req, res){ | |
107 | var model = req.params.model; | |
108 | var record = data[model].filter(function (record) { | |
109 | return record.id == req.params.id; | |
110 | })[0]; | |
111 | ||
112 | if (record) { | |
113 | res.set('Content-Type', 'application/json'); | |
114 | res.send(JSON.stringify(record)); | |
115 | } | |
116 | else { | |
117 | res.set('Content-Type', 'application/json'); | |
118 | res.send(404, 'record not found'); | |
119 | } | |
120 | }); | |
121 | ||
122 | this.app.get('/api/:model/:id/delete', function (req, res){ | |
123 | var model = req.params.model; | |
124 | var record = data[model].filter(function (record) { | |
125 | return record.id == req.params.id; | |
126 | })[0]; | |
127 | ||
128 | data[model].splice(data[model].indexOf(record), 1); | |
129 | ||
130 | res.set('Content-Type', 'application/json'); | |
131 | res.send('{}'); | |
132 | }); | |
133 | ||
134 | this.app.post('/api/:model/create', function (req, res){ | |
135 | var model = req.params.model; | |
136 | var record = req.body; | |
137 | record.id = util.generateUid(24); | |
138 | ||
139 | data[model].push(record); | |
140 | ||
141 | res.set('Content-Type', 'application/json'); | |
142 | res.send(JSON.stringify(record)); | |
143 | }); | |
144 | ||
145 | this.app.post('/api/:model/:id/update', function (req, res){ | |
146 | var model = req.params.model; | |
147 | ||
148 | var record = data[model].filter(function (record) { | |
149 | return record.id == req.params.id; | |
150 | })[0]; | |
151 | ||
152 | var position = data[model].indexOf(record); | |
153 | console.log(req); | |
154 | data[model].splice(position, 1, req.body); | |
155 | ||
156 | var record = data[model].filter(function (record) { | |
157 | return record.id == req.params.id; | |
158 | })[0]; | |
159 | res.set('Content-Type', 'application/json'); | |
160 | res.send(JSON.stringify(record)); | |
161 | }); | |
162 | ||
163 | this.app.get('/manifest_doc.js', function (req, res) { | |
164 | res.set('Content-Type', 'application/json'); | |
165 | ||
166 | // Generate the package every time for development | |
167 | Manifest.generatePackage(function () { | |
168 | res.sendfile(path.resolve('./public/manifest_doc.js')); | |
169 | }); | |
170 | /* TODO: Uncomment when file detection is done | |
171 | nitrogen.fs.exists('./public/manifest_doc.js', function (exists) { | |
172 | if (exists) { | |
173 | res.sendfile(path.resolve('./public/manifest_doc.js')); | |
174 | } else { | |
175 | // create package | |
176 | Manifest.generatePackage(function () { | |
177 | res.sendfile(path.resolve('./public/manifest_doc.js')); | |
178 | }); | |
179 | } | |
180 | }); */ | |
181 | }); | |
182 | ||
183 | this.app.get('/get_assets', function (req, res) { | |
184 | res.set('Content-Type', 'text/plain'); | |
185 | ||
186 | // Generate the package everytime for development | |
187 | Manifest.generatePackage(function () { | |
188 | res.sendfile(path.resolve('../app_assets/package.dat')); | |
189 | }); | |
190 | /* TODO: Uncomment when file detection is ready | |
191 | nitrogen.fs.exists('../app_assets/package.dat', function (exists) { | |
192 | if (exists) { | |
193 | res.sendfile(path.resolve('../app_assets/package.dat')); | |
194 | } else { | |
195 | // create package | |
196 | Manifest.generatePackage(function () { | |
197 | res.sendfile(path.resolve('../app_assets/package.dat')); | |
198 | }); | |
199 | } | |
200 | }); */ | |
201 | }); | |
202 | ||
203 | this.app.get('/attachments/*', function (req, res) { | |
204 | var options = nitrogen.occipitalOptions; | |
205 | ||
206 | nitrogen.proxy.proxyRequest( req, res, options ); | |
207 | }); | |
208 | }, | |
209 | ||
210 | initSyncEvents : function () { | |
211 | this.socket.sockets.on('connection', function (socket) { | |
212 | socket.emit('connected', {message : "Connection established"}); | |
213 | ||
214 | socket.on('sync', function (sync_data) { | |
215 | console.log(">> Incoming LSD DATA".blue); | |
216 | ||
217 | sync_data = JSON.parse(sync_data); | |
218 | ||
219 | // Let's handle each guy | |
220 | switch(sync_data.action) { | |
221 | case "create": | |
222 | data["tasks"].push(sync_data.data); | |
223 | break; | |
224 | case "update": | |
225 | var record = data["tasks"].filter(function (record) { | |
226 | return record.id == sync_data.data.id; | |
227 | })[0]; | |
228 | var position = data["tasks"].indexOf(record); | |
229 | data["tasks"].splice(position, 1, sync_data.data); | |
230 | break; | |
231 | case "remove": | |
232 | var record = data["tasks"].filter(function (record) { | |
233 | return record.id == sync_data.data.id; | |
234 | })[0]; | |
235 | var position = data["tasks"].indexOf(record); | |
236 | data["tasks"].splice(position, 1); | |
237 | break; | |
238 | } | |
239 | ||
240 | socket.emit('sync_ok', {message : "Sync Received: OK"}); | |
241 | }); | |
242 | }); | |
243 | ||
244 | this.socket.sockets.on('disconnect', function (socket) { | |
245 | socket.emit('disconnected', {message : "Connection lost"}); | |
246 | }); | |
247 | } | |
248 | } | |
249 | }); | |
250 | ||
251 | new Nitrogen.Server.Editor({ | |
252 | port : process.argv[2] || 4000, | |
253 | occipitalOptions : { | |
254 | host : (process.argv[3]) ? process.argv[3].replace(/:.*/, '') : '127.0.0.1', | |
255 | port : (process.argv[3]) ? process.argv[3].replace(/.*:/, '') : '3001' | |
256 | } | |
257 | }); | |
258 |