Created
May 13, 2011 18:20
-
-
Save shimondoodkin/971025 to your computer and use it in GitHub Desktop.
nodejs RestStore dojo.store.JsonRest - parital
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs=require('fs'), | |
| path = require("path"); | |
| var http = require('http'), | |
| urlparse = require('url').parse; | |
| urlresolve = require('url').resolve; | |
| var util = require('util'), | |
| exec = require('child_process').exec; | |
| var linq = require('jsinc').jsinc(__dirname+'/node_modules/jslinq/scripts/JSLINQ.js').window.JSLINQ;// http://jslinq.codeplex.com | |
| var linqjs = require('jsinc').jsinc(__dirname+'/node_modules/linqjs/linq.js').Enumerable; // http://linqjs.codeplex.com | |
| var tasks={}; | |
| function ls(callback) | |
| { | |
| var child = exec('ls -la', | |
| function (error, stdout, stderr) | |
| { | |
| //console.log('stdout: ' + stdout); | |
| //console.log('stderr: ' + stderr); | |
| if (error !== null) | |
| { | |
| callback(); | |
| console.log('exec error: ' + error); | |
| } | |
| else | |
| { | |
| callback(stdout); | |
| } | |
| }); | |
| } | |
| function list(cb) | |
| { | |
| var child = exec('cd /;ls -la', | |
| function (error, stdout, stderr) | |
| { | |
| //console.log('stdout: ' + stdout); | |
| //console.log('stderr: ' + stderr); | |
| if (error !== null) | |
| { | |
| callback(""); | |
| console.log('exec error: ' + error); | |
| } | |
| else | |
| { | |
| callback(stdout); | |
| } | |
| }); | |
| } | |
| var etag=Math.floor(Math.random()*11); | |
| http.createServer(function (req, res) | |
| { | |
| var u=urlparse(urlresolve('http://'+req.headers.host,req.url), true), url=u.pathname, q=u.query; | |
| //console.log('req',require('sys').inspect(req)); | |
| //console.log('url',require('sys').inspect(q)); | |
| function send(str,mime,status) | |
| { | |
| res.writeHead(status?status:200, {'Content-Type': mime?mime:'text/plain'}); | |
| res.end(str); | |
| } | |
| function sendfile(file,fmime) | |
| { | |
| if(req.headers['if-none-match'] == etag) | |
| { | |
| res.writeHead(304, { 'Content-Length': 0 }); | |
| res.end(); return; | |
| } | |
| var ext=file.substr(file.lastIndexOf('.')+1).toLowerCase(); | |
| var mime={html:'text/html', js:'text/javascript', css:'text/css', gif: 'image/gif', png: 'image/png', jpg: 'image/jpg', ico:'image/x-icon'}; | |
| res.writeHead(200, {'Content-Type': !!fmime?fmime: (mime[ext]?mime[ext]:mime['html'])}); | |
| res.end(fs.readFileSync(file)); | |
| } | |
| function checkfile(file) | |
| { | |
| return path.existsSync(file) && !fs.lstatSync(file).isDirectory(); | |
| } | |
| var match=null; | |
| if(url=='/index.html'|| | |
| url=='/') { sendfile('index.html');} | |
| else if(url=='/index.css') { sendfile('index.css');} | |
| else if(url=='/index.js') { sendfile('index.js');} | |
| else if(url=='/favicon.ico') { sendfile('favicon.ico');} | |
| else if(url=='/list') { list(send);} | |
| else if(url=='/install') { install(send);} | |
| else if(url=='/ls') { ls(send);} | |
| else if(match=url.match('^\/datastore(/.*)$')) { datastore(req,res,match[1]||"",q,send);} | |
| else if(url=='/grid.csv') { res.writeHead(200, {'Content-Type': 'text/csv'}); res.end(fs.readFileSync('grid-demo-data.csv'));} | |
| else if((match=url.match(/^\/mochaui\/(.*$)/)) && checkfile(__dirname+'/node_modules/mochaui/'+match[1])) { sendfile(__dirname+'/node_modules/mochaui/'+match[1]); } | |
| else if(url=='/exit') { send('ok'); process.exit(0);} | |
| else { res.writeHead(404, {'Content-Type': 'text/plain'}); res.end("File Not Found\r\n"+match);} | |
| }).listen(2222); | |
| console.log('Server running at http://127.0.0.1:2222/'); | |
| var data={} | |
| function datastore(req,res,url,q,send) | |
| { | |
| var k=url.substr(1).split('/'); // remove first '/' and covert to array | |
| req.method=req.method.toUpperCase(); | |
| if (req.method == 'GET' ) | |
| { | |
| var value_start=0,value_end=24; | |
| // = list databases and number of tables // i= | |
| // db = lista tables // i=0 | |
| // db/table = all table items // i=1 | |
| // db/table/id = item // i=2 | |
| var value=Object.keys(data),tdata=data,maybe_error=false,error=false; // i= | |
| for(var i=0;i<k.length;i++) // digg var | |
| { | |
| if(k[i].length==0) { maybe_error=true; continue; } | |
| else if (maybe_error) { error="empty name in request path"; break; } | |
| if(!tdata) { error="object is undefined"; break; } | |
| if(k[i] in tdata) | |
| { | |
| if(i==0){ tdata=tdata[k[i]]; value=Object.keys(tdata);} | |
| else { tdata=tdata[k[i]]; value=tdata;} | |
| } | |
| } | |
| if(error) | |
| send(error+" url="+url,false,404); | |
| else | |
| { | |
| if(value instanceof Array) | |
| { | |
| //linq http://jslinq.codeplex.com/ | |
| var query=(function (o) { function c(o) { for (var i in o) { this[i] = o[i]; } } return new c(o);})(q); | |
| var sort=false,desc=false; | |
| var match; | |
| for(x in query) | |
| { | |
| if(match=x.match(/^sort\((.+)\)$/)) | |
| { | |
| sort=match[1]; | |
| delete query[match[0]]; | |
| sort=sort.split(','); | |
| console.log(sort); | |
| sort=sort[0]||false; | |
| console.log(sort); | |
| if(sort[0]=='-') | |
| { | |
| sort=sort.substr(1); | |
| desc=true; | |
| } | |
| if(sort[0]==' ') | |
| { | |
| sort=sort.substr(1); | |
| } | |
| } | |
| } | |
| if(Object.keys(query).length==0)query=false; | |
| var filtered=false; | |
| if(query) | |
| { | |
| if(!filtered)var filtered=linq(value); | |
| filtered=filtered.Where(function(item) | |
| { | |
| if(item) | |
| { | |
| var ok=true; | |
| for(x in query) | |
| { | |
| if(!(x in item && item[x]==query[x])) | |
| { | |
| ok=false; | |
| break; | |
| } | |
| } | |
| return ok; | |
| } | |
| else | |
| return false; | |
| }); | |
| } | |
| if(sort) | |
| { | |
| if(!filtered)var filtered=linq(value);sort | |
| filtered=filtered[desc?'OrderByDescending':'OrderBy'](function(item) { return item[sort]; }) | |
| } | |
| if(filtered)value=filtered.items; | |
| // end linq | |
| var value_range=req.headers['range']||""; | |
| if(value_range.match(/\d+-\d+/)) | |
| { | |
| value_range=value_range.split("-"); | |
| value_start=parseInt(value_range[0]); | |
| value_start=Math.min(value.length,Math.max(0,value_start)); | |
| value_end=parseInt(value_range[1]); | |
| value_end=Math.min(value.length,value_end); | |
| } | |
| res.writeHead(200, {'Content-Type':'text/plain','Content-Range':'items '+value_start+'-'+value_end+'/'+value.length}); | |
| // res.end(JSON.stringify(value)); | |
| res.end(JSON.stringify(value.slice(value_start,value_end))) | |
| } | |
| else | |
| { | |
| res.writeHead(200, {'Content-Type':'text/plain'}); | |
| res.end(JSON.stringify(value)); | |
| } | |
| } | |
| } | |
| if (req.method == 'DELETE' ) | |
| { | |
| console.log("DELETE is not implemented",url,q,req.headers); | |
| } | |
| if (req.method == 'PUT' || req.method == 'POST' ) | |
| { | |
| var body = ''; | |
| req.on('data', function (data) | |
| { | |
| body += data; | |
| }); | |
| req.on('end', function () | |
| { | |
| console.log("PUT or POST is not implemented",url,q,req.headers); | |
| console.log(body); | |
| // use POST | |
| }); | |
| } | |
| } | |
| function shuffle(o){ //v1.0 | |
| for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
| return o; | |
| }; | |
| var names="shi mon dood kin ga dol".split(" "); | |
| data.db={tbl1:[]}; | |
| for(var i=0;i<100;i++) | |
| { | |
| data.db.tbl1[i]={id:i,name:shuffle(names).slice(0,2).join(''),value:parseInt(Math.random() * 1000),grouptype:parseInt(Math.random() * 7)}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment