Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var sys = require('sys'),
http = require('http'),
url = require('url'),
Dirty = require('./dirty');
var files = {};
exports.connect = function (port) {
http.createServer(function (req, res) {
var url = url.parse(req.url);
var path = url.pathname.match(/^\/([^\/]+)\/([^\/]*)/)
if(path) {
var filename = decodeURIComponent(path[1]);
var id = decodeURIComponent(path[3]);
var body = "";
req.addListener("data", function (data) {
body += data;
});
req.addListener("end", function () {
var dirt = files[filename];
if(dirt) {
handleRequest({
req, req,
res: res,
dirt: dirt,
filename: filename,
id: id,
body: body
});
} else {
dirt = new Dirty(filename);
dirt.load(function (err) {
if(err) {
sendError(err)
} else {
handleRequest({
req, req,
res: res,
dirt: dirt,
filename: filename,
id: id,
body: body
});
}
})
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.write('Hello World');
res.close();
});
} else {
sendBadRequest(res);
}
}).listen(port;
}
function sendError(res, err) {
res.writeHead(500, {'Content-Type': 'application/json'});
res.write(JSON.stringify(err));
res.close();
}
function sendBadRequest(res) {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.close();
}
var actions = {
add: function (info) {
return info.dirt.add(JSON.parse(info.body));
},
set: function (info) {
return info.dirt.set(info.id, JSON.parse(info.body)));
},
get: function (info) {
return info.dirt.get(info.id);
},
filter: function (info) {
var filter = eval(info.body);
return info.dirt.filter(filter);
},
remove: function (info) {
info.dirt.remove(info.id)
}
};
function handleRequest(info) {
var action = "filter";
var method = info.req.method;
if(method === "GET" && info.id) {
action = "get"
}
else if(method === "PUT") {
if(info.id) {
action = "set";
} else {
action = "add"
}
}
else if(method === "DELETE") {
action = "remove"
}
else {
sendBadRequest(info.res)
}
var a = actions[action];
if(!a) {
return sendError(info.res, "Unknown action "+action);
}
try {
var ret = a(info);
} catch(e) {
sendError(info.res, e);
}
res.writeHead(200, {'Content-Type': 'application/json'});
info.res.write(JSON.stringify(ret), "utf8");
info.close();
}
exports.Client = function (filename, host, port) {
this.filename = filename;
this.client = http.createClient(port, host);
this.host = host;
}
export.Client.prototype = {
request: function (method, id, data, cb) {
var path = "/"+encodeURIComponent(this.filename);
if(id) {
path += "/"+encodeURIComponent(id);
}
var req = this.client.request(method, path, {
host: this.host
});
req.write(data, "utf8");
req.addListener('response', function (response) {
if(response.statusCode !== 200) {
response.setBodyEncoding("utf8");
var data = "";
response.addListener("data", function (chunk) {
data += chunk;
})
response.addListener("end", function () {
cb(null, data);
})
} else {
cb(new Error("Error "+response.statusCode))
}
})
req.close();
},
add: function (data, cb) {
this.request("PUT", null, JSON.stringify(data), function (err, data) {
if(err) return cb(err);
var id = JSON.parse(data);
cb(null, id);
})
},
set: function (id, data, cb) {
this.request("PUT", id, JSON.stringify(data), function (err, data) {
if(err) return cb(err);
var doc = JSON.parse(data);
cb(null, doc);
})
},
get: function (id, cb) {
this.request("GET", id, null, function (err, data) {
if(err) return cb(err);
var obj = JSON.parse(data);
cb(null, obj);
})
},
remove: function (id, cb) {
this.request("REMOVE", id, null, function (err, data) {
if(err) return cb(err);
cb(null, id);
})
},
filter: function (id, filter, cb) {
var filterStr = filter ? filter.toString() : ""
this.request("GET", null, filterStr, function (err, data) {
if(err) return cb(err);
var obj = JSON.parse(data);
cb(null, obj);
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.