diff --git a/lib/server/auth.js b/lib/server/auth.js index 61573a4d..598ec671 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -1,29 +1,15 @@ -/* https://github.com/prose/gatekeeper */ -(function(){ +(function() { 'use strict'; - if(!global.cloudcmd) - return console.log( - '# auth.js' + '\n' + - '# -----------' + '\n' + - '# Module is part of Cloud Commander,' + '\n' + - '# used for work with authentication.' + '\n' + - '# If you wont to see at work set auth' + '\n' + - '# parameters in config.json or environment' + '\n' + - '# and start cloudcmd.js or just do' + '\n' + - '# require(\'auth.js\').auth(code, сallback)' + '\n' + - '# http://cloudcmd.io' + '\n'); - - var main = global.cloudcmd.main, - - DIR = './', + var DIR = './', DIR_LIB = DIR + '../', - + DIR_JSON = DIR_LIB + '../json/', + https = require('https'), qs = require('querystring'), pipe = require(DIR + 'pipe'), - Modules = main.modules, + Modules = require(DIR_JSON + 'modules'), Util = require(DIR_LIB + 'util'), GithubAuth = { @@ -43,6 +29,7 @@ code = code.replace('code=', ''); Util.log(code); + authenticate(code, function(error, token) { var result = { 'token': token }; Util.log(error || result); diff --git a/lib/server/main.js b/lib/server/main.js index e72166d0..a4a0cd31 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -4,12 +4,9 @@ /* Global var accessible from any loaded module */ global.cloudcmd = {}; - var DIR, LIBDIR, SRVDIR, JSONDIR, HTMLDIR, - Util, - UTIL = 'util', + var Util, SLASH, - ext, CloudFunc, path, Config = { @@ -22,27 +19,10 @@ /* Constants */ exports.SLASH = SLASH = '/', - - exports.SRVDIR = SRVDIR = __dirname + SLASH, - exports.LIBDIR = LIBDIR = path.normalize(SRVDIR + '../'), - exports.DIR = DIR = path.normalize(LIBDIR + '../'), - exports.HTMLDIR = HTMLDIR = DIR + 'html' + SLASH, - exports.JSONDIR = JSONDIR = DIR + 'json' + SLASH, - - /* Functions */ - exports.require = mrequire, - exports.librequire = librequire, - exports.srvrequire = srvrequire, - exports.rootrequire = rootrequire, - exports.quietrequire = quietrequire, /* we can not use librequare here */ - Util = require(LIBDIR + UTIL), + Util = require('../util'), - /* Main Information */ - exports.modules = jsonrequire('modules'); - exports.ext = ext = jsonrequire('ext'); - exports.mainpackage = rootrequire('package'); /* base configuration */ exports.config = Config, @@ -56,50 +36,11 @@ global.cloudcmd.main = exports; /* Additional Modules */ - CloudFunc = librequire('cloudfunc'), - exports.socket = srvrequire('socket'), - exports.auth = srvrequire('auth').auth, + CloudFunc = require('../cloudfunc'), + exports.socket = require('./socket'), + exports.auth = require('./auth').auth, /* second initializing after all modules load, so global var is * * totally filled of all information that should know all modules */ global.cloudcmd.main = exports; - - /** - * function do safe require of needed module - * @param {Strin} src - */ - function mrequire(src) { - var module, msg, - error = Util.exec.try(function() { - module = require(src); - }); - - if (error) - if (error.code === 'MODULE_NOT_FOUND') - msg = CloudFunc.formatMsg('require', src, 'no'); - else - Util.log(error); - - Util.log(msg); - - return module; - } - - function quietrequire(src) { - var module; - - Util.exec.try(function() { - module = require(src); - }); - - return module; - } - - function rootrequire(src) { return mrequire(DIR + src); } - - function librequire(src) { return mrequire(LIBDIR + src); } - - function srvrequire(src) { return mrequire(SRVDIR + src); } - - function jsonrequire(src) { return mrequire(JSONDIR + src);} })(); diff --git a/lib/server/rest.js b/lib/server/rest.js index f5a3368e..bbb75bd4 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -28,11 +28,12 @@ CloudFunc = require(DIR_LIB + 'cloudfunc'), format = require(DIR_LIB + 'format'), - onFSGet = main.srvrequire('rest/fs/get').onGet, - onFSPut = main.srvrequire('rest/fs/put').onPut, - onDelete = main.srvrequire('rest/fs/delete').onDelete, - markdown = main.srvrequire('rest/markdown'), + onFSGet = require(DIR + 'rest/fs/get').onGet, + onFSPut = require(DIR + 'rest/fs/put').onPut, + onDelete = require(DIR + 'rest/fs/delete').onDelete, + markdown = require(DIR + 'rest/markdown'), + auth = require(DIR + 'auth'), flop = require(DIR + 'flop'), pack = require(DIR + 'pack'), mellow = require(DIR + 'mellow'), @@ -259,7 +260,7 @@ switch(cmd) { case 'auth': - main.auth(body, function(error, token) { + auth(body, function(error, token) { callback(error, Util.stringifyJSON({ data: token })); diff --git a/lib/server/terminal.js b/lib/server/terminal.js index 74c58f30..95913854 100644 --- a/lib/server/terminal.js +++ b/lib/server/terminal.js @@ -1,16 +1,15 @@ (function() { 'use strict'; - var main = global.cloudcmd.main, - DIR = __dirname + '/../../', + var DIR = __dirname + '/../../', DIR_LIB = DIR + 'lib/', - - socket = main.socket, - pty = main.require('pty.js'), + + socket = require('./socket'), Util = require(DIR_LIB + 'util'), CloudFunc = require(DIR_LIB + 'cloudfunc'), + pty = tryRequire('pty.js'), Clients = [], CHANNEL = CloudFunc.CHANNEL_TERMINAL, @@ -18,6 +17,18 @@ ConNum = 0; + function tryRequire(name) { + var module, + error = Util.exec.try(function() { + module = require(name); + }); + + if (error) + Util.log(error.message); + + return module; + } + module.exports = function(io) { var makePty = function(clientSocket) { onConnection(clientSocket, function(channel, data) {