handler/APIHandler: use jshint

This commit is contained in:
booo 2011-12-22 12:26:56 +01:00
parent 1f41ef3365
commit da7098168e

View file

@ -28,7 +28,7 @@ try
{ {
apikey = fs.readFileSync("../APIKEY.txt","utf8"); apikey = fs.readFileSync("../APIKEY.txt","utf8");
} }
catch(e) catch(e)
{ {
apikey = randomString(32); apikey = randomString(32);
fs.writeFileSync("../APIKEY.txt",apikey,"utf8"); fs.writeFileSync("../APIKEY.txt",apikey,"utf8");
@ -37,28 +37,28 @@ catch(e)
//a list of all functions //a list of all functions
var functions = { var functions = {
"createGroup" : [], "createGroup" : [],
"createGroupIfNotExistsFor" : ["groupMapper"], "createGroupIfNotExistsFor" : ["groupMapper"],
"deleteGroup" : ["groupID"], "deleteGroup" : ["groupID"],
"listPads" : ["groupID"], "listPads" : ["groupID"],
"createPad" : ["padID", "text"], "createPad" : ["padID", "text"],
"createGroupPad" : ["groupID", "padName", "text"], "createGroupPad" : ["groupID", "padName", "text"],
"createAuthor" : ["name"], "createAuthor" : ["name"],
"createAuthorIfNotExistsFor": ["authorMapper" , "name"], "createAuthorIfNotExistsFor": ["authorMapper" , "name"],
"createSession" : ["groupID", "authorID", "validUntil"], "createSession" : ["groupID", "authorID", "validUntil"],
"deleteSession" : ["sessionID"], "deleteSession" : ["sessionID"],
"getSessionInfo" : ["sessionID"], "getSessionInfo" : ["sessionID"],
"listSessionsOfGroup" : ["groupID"], "listSessionsOfGroup" : ["groupID"],
"listSessionsOfAuthor" : ["authorID"], "listSessionsOfAuthor" : ["authorID"],
"getText" : ["padID", "rev"], "getText" : ["padID", "rev"],
"setText" : ["padID", "text"], "setText" : ["padID", "text"],
"getHTML" : ["padID", "rev"], "getHTML" : ["padID", "rev"],
"setHTML" : ["padID", "html"], "setHTML" : ["padID", "html"],
"getRevisionsCount" : ["padID"], "getRevisionsCount" : ["padID"],
"deletePad" : ["padID"], "deletePad" : ["padID"],
"getReadOnlyID" : ["padID"], "getReadOnlyID" : ["padID"],
"setPublicStatus" : ["padID", "publicStatus"], "setPublicStatus" : ["padID", "publicStatus"],
"getPublicStatus" : ["padID"], "getPublicStatus" : ["padID"],
"setPassword" : ["padID", "password"], "setPassword" : ["padID", "password"],
"isPasswordProtected" : ["padID"] "isPasswordProtected" : ["padID"]
}; };
@ -72,12 +72,12 @@ var functions = {
exports.handle = function(functionName, fields, req, res) exports.handle = function(functionName, fields, req, res)
{ {
//check the api key! //check the api key!
if(fields["apikey"] != apikey.trim()) if(fields.apikey != apikey.trim())
{ {
res.send({code: 4, message: "no or wrong API Key", data: null}); res.send({code: 4, message: "no or wrong API Key", data: null});
return; return;
} }
//check if this is a valid function name //check if this is a valid function name
var isKnownFunctionname = false; var isKnownFunctionname = false;
for(var knownFunctionname in functions) for(var knownFunctionname in functions)
@ -88,30 +88,30 @@ exports.handle = function(functionName, fields, req, res)
break; break;
} }
} }
//say goodbye if this is a unkown function //say goodbye if this is a unkown function
if(!isKnownFunctionname) if(!isKnownFunctionname)
{ {
res.send({code: 3, message: "no such function", data: null}); res.send({code: 3, message: "no such function", data: null});
return; return;
} }
//put the function parameters in an array //put the function parameters in an array
var functionParams = []; var functionParams = [];
for(var i=0;i<functions[functionName].length;i++) for(var i=0;i<functions[functionName].length;i++)
{ {
functionParams.push(fields[functions[functionName][i]]); functionParams.push(fields[functions[functionName][i]]);
} }
//add a callback function to handle the response //add a callback function to handle the response
functionParams.push(function(err, data) functionParams.push(function(err, data)
{ {
// no error happend, everything is fine // no error happend, everything is fine
if(err == null) if(!err)
{ {
if(!data) if(!data)
data = null; data = null;
res.send({code: 0, message: "ok", data: data}); res.send({code: 0, message: "ok", data: data});
} }
// parameters were wrong and the api stopped execution, pass the error // parameters were wrong and the api stopped execution, pass the error
@ -126,15 +126,15 @@ exports.handle = function(functionName, fields, req, res)
ERR(err); ERR(err);
} }
}); });
//call the api function //call the api function
api[functionName](functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]); api[functionName](functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]);
} };
/** /**
* Generates a random String with the given length. Is needed to generate the Author Ids * Generates a random String with the given length. Is needed to generate the Author Ids
*/ */
function randomString(len) function randomString(len)
{ {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var randomstring = ''; var randomstring = '';