]>
git.r.bdr.sh - rbdr/dotfiles/blob - vim/.backup/server.js~
4 var Ne
= require('neon');
7 Manifest : require('./lib/manifest.js'),
11 Ne
.Class(Nitrogen
.Server
, 'Editor')({
14 express : require('express'),
15 httpProxy : require('http-proxy'),
16 io : require('socket.io'),
17 http : require('http'),
22 occipitalOptions : {},
24 init : function init(config
) {
25 Object
.keys(config
).forEach(function (property
) {
26 this[property
] = config
[property
];
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
);
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
);
42 initRack : function initRack() {
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
) {
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
);
68 this.app
.use(this.express
.bodyParser());
69 this.app
.use('/', this.express
.static('public'));
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
= {};
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
){
86 initRoutes : function initRoutes() {
88 path
= require('path');
90 this.app
.get('/', function (request
, response
) {
91 var file
= path
.resolve('./public/app.html');
92 response
.sendfile(file
);
95 this.app
.get('/api/all', function (req
, res
) {
96 res
.set('Content-Type', 'application/json');
97 res
.send(JSON
.stringify(data
));
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
));
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
;
113 res
.set('Content-Type', 'application/json');
114 res
.send(JSON
.stringify(record
));
117 res
.set('Content-Type', 'application/json');
118 res
.send(404, 'record not found');
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
;
128 data
[model
].splice(data
[model
].indexOf(record
), 1);
130 res
.set('Content-Type', 'application/json');
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);
139 data
[model
].push(record
);
141 res
.set('Content-Type', 'application/json');
142 res
.send(JSON
.stringify(record
));
145 this.app
.post('/api/:model/:id/update', function (req
, res
){
146 var model
= req
.params
.model
;
148 var record
= data
[model
].filter(function (record
) {
149 return record
.id
== req
.params
.id
;
152 var position
= data
[model
].indexOf(record
);
154 data
[model
].splice(position
, 1, req
.body
);
156 var record
= data
[model
].filter(function (record
) {
157 return record
.id
== req
.params
.id
;
159 res
.set('Content-Type', 'application/json');
160 res
.send(JSON
.stringify(record
));
163 this.app
.get('/manifest_doc.js', function (req
, res
) {
164 res
.set('Content-Type', 'application/json');
166 // Generate the package every time for development
167 Manifest
.generatePackage(function () {
168 res
.sendfile(path
.resolve('./public/manifest_doc.js'));
170 /* TODO: Uncomment when file detection is done
171 nitrogen.fs.exists('./public/manifest_doc.js', function (exists) {
173 res.sendfile(path.resolve('./public/manifest_doc.js'));
176 Manifest.generatePackage(function () {
177 res.sendfile(path.resolve('./public/manifest_doc.js'));
183 this.app
.get('/get_assets', function (req
, res
) {
184 res
.set('Content-Type', 'text/plain');
186 // Generate the package everytime for development
187 Manifest
.generatePackage(function () {
188 res
.sendfile(path
.resolve('../app_assets/package.dat'));
190 /* TODO: Uncomment when file detection is ready
191 nitrogen.fs.exists('../app_assets/package.dat', function (exists) {
193 res.sendfile(path.resolve('../app_assets/package.dat'));
196 Manifest.generatePackage(function () {
197 res.sendfile(path.resolve('../app_assets/package.dat'));
203 this.app
.get('/attachments/*', function (req
, res
) {
204 var options
= nitrogen
.occipitalOptions
;
206 nitrogen
.proxy
.proxyRequest( req
, res
, options
);
210 initSyncEvents : function () {
211 this.socket
.sockets
.on('connection', function (socket
) {
212 socket
.emit('connected', {message : "Connection established"});
214 socket
.on('sync', function (sync_data
) {
215 console
.log(">> Incoming LSD DATA".blue
);
217 sync_data
= JSON
.parse(sync_data
);
219 // Let's handle each guy
220 switch(sync_data
.action
) {
222 data
["tasks"].push(sync_data
.data
);
225 var record
= data
["tasks"].filter(function (record
) {
226 return record
.id
== sync_data
.data
.id
;
228 var position
= data
["tasks"].indexOf(record
);
229 data
["tasks"].splice(position
, 1, sync_data
.data
);
232 var record
= data
["tasks"].filter(function (record
) {
233 return record
.id
== sync_data
.data
.id
;
235 var position
= data
["tasks"].indexOf(record
);
236 data
["tasks"].splice(position
, 1);
240 socket
.emit('sync_ok', {message : "Sync Received: OK"});
244 this.socket
.sockets
.on('disconnect', function (socket
) {
245 socket
.emit('disconnected', {message : "Connection lost"});
251 new Nitrogen
.Server
.Editor({
252 port : process
.argv
[2] || 4000,
254 host : (process
.argv
[3]) ? process
.argv
[3].replace(/:.*/, '') : '127.0.0.1',
255 port : (process
.argv
[3]) ? process
.argv
[3].replace(/.*:/, '') : '3001'