From fd613120030648b7e7601e1f096ce67400a547ad Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 2 Nov 2012 10:59:10 -0400 Subject: [PATCH] refactored --- cloudcmd.js | 6 +-- lib/server/auth.js | 113 +++++++++++++++++++++++---------------------- lib/server/main.js | 21 +++------ 3 files changed, 67 insertions(+), 73 deletions(-) diff --git a/cloudcmd.js b/cloudcmd.js index 0dbe22fb..a018d5e2 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -1,8 +1,8 @@ "use strict"; -var main = require('./lib/server/main'), - - DIR = main.DIR, +var DIR = process.cwd() + '/', + main = require(DIR + 'lib/server/main'), + LIBDIR = main.LIBDIR, SRVDIR = main.SRVDIR, diff --git a/lib/server/auth.js b/lib/server/auth.js index 50dc7f01..7b43bc4c 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -1,62 +1,63 @@ /* https://github.com/prose/gatekeeper */ - -var DIR = process.cwd() + '/', - main = require(DIR + 'lib/server/main'), - LIBDIR = main.LIBDIR, - SRVDIR = main.SRVDIR, - - https = main.https, - qs = main.querystring, +(function(){ + "use strict"; - Config = main.config, - - Util = main.uti, - - GithubAuth = { - host: "github.com", - port: 443, - path: "/login/oauth/access_token", - method: "POST" - }; - -/** - * function do authentication - * @param pCode - * @param pCallBack - */ - -exports.auth = function(pCode, pCallBack){ - pCode = pCode.replace('code=',''); - - console.log(pCode); - authenticate(pCode, function(token) { - var result = { "token": token }; - console.log(result); + var DIR = process.cwd() + '/', + main = require(DIR + 'lib/server/main'), - Util.exec(pCallBack); - }); -}; - - -function authenticate(pCode, pCallBack) { - var data = qs.stringify({ - client_id : Config.oauth_client_id, - client_secret : Config.oauth_client_secret, - code : pCode - }); - - GithubAuth.headers = { 'content-length': data.length }; + https = main.https, + qs = main.querystring, + + Config = main.config, - var body = "", - req = https.request(GithubAuth, function(res) { - res.setEncoding('utf8'); - res.on('data', function (chunk) { body += chunk; }); - res.on('end', function() { - pCallBack(qs.parse(body).access_token); - }); + Util = main.util, + + GithubAuth = { + host: "github.com", + port: 443, + path: "/login/oauth/access_token", + method: "POST" + }; + + /** + * function do authentication + * @param pCode + * @param pCallBack + */ + + exports.auth = function(pCode, pCallBack){ + pCode = pCode.replace('code=',''); + + console.log(pCode); + authenticate(pCode, function(token) { + var result = { "token": token }; + console.log(result); + + Util.exec(pCallBack); }); + }; - req.write(data); - req.end(); - req.on('error', function(e) { pCallBack(e.message); }); -} \ No newline at end of file + + function authenticate(pCode, pCallBack) { + var data = qs.stringify({ + client_id : Config.oauth_client_id, + client_secret : Config.oauth_client_secret, + code : pCode + }); + + GithubAuth.headers = { 'content-length': data.length }; + + var body = "", + req = https.request(GithubAuth, function(res) { + res.setEncoding('utf8'); + res.on('data', function (chunk) { body += chunk; }); + res.on('end', function() { + pCallBack(qs.parse(body).access_token); + }); + }); + + req.write(data); + req.end(); + req.on('error', function(e) { pCallBack(e.message); }); + } +})(); diff --git a/lib/server/main.js b/lib/server/main.js index 60fd81c0..16cf2f25 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -40,7 +40,7 @@ exports.object = srvrequire('object'); exports.socket = srvrequire('socket'), exports.update = srvrequire('update'), - exports.zlib = mrequire('zlib'), + exports.zlib = mrequire('zlib'); /** @@ -59,22 +59,15 @@ return lModule; } - function rootrequire(pSrc){ - return mrequire(DIR + pSrc); - } + function rootrequire(pSrc){ return mrequire(DIR + pSrc); } - function librequire(pSrc){ - return mrequire(LIBDIR + pSrc); - } + function librequire(pSrc){ return mrequire(LIBDIR + pSrc); } - function srvrequire(pSrc){ - return mrequire(SRVDIR + pSrc); - } + function srvrequire(pSrc){ return mrequire(SRVDIR + pSrc); } /** * function check is current platform is win32 */ - function isWin32(){ - return process.platform === 'win32'; - } -}()); \ No newline at end of file + function isWin32(){ return process.platform === 'win32'; } + +})(); \ No newline at end of file