Skip to content

Instantly share code, notes, and snippets.

@berak
Last active October 4, 2015 17:23
Show Gist options
  • Select an option

  • Save berak/dbd3ca367d913aaa9e09 to your computer and use it in GitHub Desktop.

Select an option

Save berak/dbd3ca367d913aaa9e09 to your computer and use it in GitHub Desktop.
http.io
db = {
_get: function(hook,key,who,proc) {
hook.datastore.get(key, function(err, result){
if (err) { return hook.res.end(err.message); }
proc(hook,result,who);
});
},
_setraw: function(hook,key,val,proc) {
hook.datastore.set(key, val, function(err, val){
if (err) { return hook.res.end(err.message); }
proc(hook,key,val);
});
},
_set: function(hook,key,who,proc) {
db._get(hook,key,who,function(hook,result,who){
var res = proc(result,who);
db._setraw(hook,key,result,function(hook,key,val){ hook.res.end(res); });
});
},
clear: function(hook,key) {
db._setraw(hook,key,who,{},function(hook,key,val){hook.res.end("ok.");});
},
del: function(hook,key) {
hook.datastore.del(key, function(err){
if (err) { return hook.res.end(err.message); }
hook.res.end("ok.");
});
},
incr: function(hook,key,who) {
db._set(hook,key,who,function(result,who) {
if (result[who]==undefined)
result[who] = 0;
result[who] += 1;
return result[who];
});
},
set: function(hook,key,who,prm) {
db._set(hook,key,who,function(result,who) {
result[who]=prm;
return result[who];
});
},
get: function(hook,key,who) {
db._get(hook,key,who,function(hook,result,who){
hook.res.end(result[who]);
});
},
all: function(hook,key) {
db._get(hook,key,"",function(hook,result,who){
hook.res.end(result);
});
},
keys: function(hook,key) {
db._get(hook,key,"",function(hook,result,who){
var m=[]; for (k in result) m.push(k);
hook.res.end(JSON.stringify(m,null,2));
});
},
index: function(hook,key) {
db._get(hook,key,"",function(hook,result,who){
var s=""; for (k in result) s+="<a href=\"/berak/chili?who="+k+"\">"+k+"</a><br>";
hook.res.end(s);
});
},
};
module['exports'] = function datastoreExample (hook) {
var prm = hook.params["set"];
var who = hook.params["who"];
var key = 'chili';
console.log(hook.req.url)
if (hook.req.url.indexOf("/berak/chili/_all")==0) { return db.all(hook,key); }
if (hook.req.url.indexOf("/berak/chili/_clear")==0) { return db.clear(hook,key); }
if (hook.req.url.indexOf("/berak/chili/keys")==0) { return db.keys(hook,key); }
if (hook.req.url.indexOf("/berak/chili/index")==0) { return db.index(hook,key); }
if (hook.req.url.indexOf("/berak/chili/incr")==0) { return db.incr(hook,key,who); }
who = who ? who : "def";
if (prm) {
db.set(hook,key,who,prm);
} else {
db.get(hook,key,who);
}
};
//
// counter?who=maria
//
module['exports'] = function counterHttp (hook) {
var who = hook.params["who"];
console.log("who: "+who);
if (who == undefined) {
hook.res.end("who ?");
return;
}
if (who == "clear") {
hook.datastore.set("counter", {}, function(err,data){
if (err) { return hook.res.end("error: " + err.message); }
hook.res.end("ok.");
});
return;
}
hook.datastore.get("counter", function(err,data){
console.log("err: " + err);
console.log("data: " + data);
if (err) { return hook.res.end("error: " + err.message); }
//hook.res.end(JSON.stringify(data, true, 2));
console.log("item: " + data[who]);
if (data[who]==undefined)
data[who] = 0;
data[who] += 1;
var num = data[who];
hook.datastore.set("counter", data, function(err,data){
if (err) { return hook.res.end("error: " + err.message); }
hook.res.end(num)
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment