diff --git a/cloudcmd.js b/cloudcmd.js index a1782236..93ee0533 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -2,42 +2,20 @@ 'use strict'; var DIR = __dirname + '/', - DIR_HTML = DIR + 'html/', DIR_LIB = DIR + 'lib/', DIR_SERVER = DIR + 'lib/server/', - DIR_FS = DIR_HTML + 'fs/', fs = require('fs'), win = require(DIR_SERVER + 'win'), main = require(DIR_SERVER + 'main'), - mellow = require(DIR_SERVER + 'mellow'), - ponse = require(DIR_SERVER + 'ponse'), - files = require(DIR_SERVER + 'files'), update = require(DIR_SERVER + 'update'), - minify = require(DIR_SERVER + 'minify'), tryRequire = require(DIR_SERVER + 'tryRequire'), Util = require(DIR_LIB + 'util'), - CloudFunc = require(DIR_LIB + 'cloudfunc'), server = require(DIR_LIB + 'server'), - format = require(DIR_LIB + 'format'), - Config = main.config, - - PATH_INDEX = DIR_FS + 'index.html', - - TMPL_PATH = [ - 'file', - 'panel', - 'path', - 'pathLink', - 'link', - ], - - Template = {}, - - FS = CloudFunc.FS; + Config = main.config; exports.start = function(params) { readConfig(function(error, config) { @@ -63,44 +41,7 @@ win.prepareCodePage(); }; - /** - * additional processing of index file - */ - function indexProcessing(options) { - var keysPanel, left, right, - LEFT = CloudFunc.PANEL_LEFT, - RIGHT = CloudFunc.PANEL_RIGHT, - data = options.data, - panel = options.panel; - - if (!Config.showKeysPanel) { - keysPanel = '
' + name); - - p.name = DIR_HTML + name + '.html'; - ponse.sendFile(p); - } else if (isFS) { - name = Util.rmStrOnce(name, CloudFunc.FS) || main.SLASH; - path = mellow.convertPath(name); - - mellow.read(path, function(error, dir) { - if (dir) - dir.path = format.addSlashToEnd(name); - - if (error) - if (error.code !== 'ENOTDIR') - ponse.sendError(error, p); - else - fs.realpath(path, function(error, pathReal) { - if (!error) - p.name = pathReal; - else - p.name = path; - - p.gzip = false; - ponse.sendFile(p); - }); - else - buildIndex(dir, function(error, data) { - var NOT_LOG = true; - - p.name = PATH_INDEX; - - if (error) - ponse.sendError(error, p); - else - ponse.send(data, p, NOT_LOG); - }); - }); - } - } - } - - function buildIndex(json, callback) { - var isMinify = Config.minify; - - Util.exec.if(!isMinify, function(error, name) { - fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) { - var panel, data; - - if (!error) { - panel = CloudFunc.buildFromJSON({ - data : json, - template : Template - }), - - data = indexProcessing({ - panel : panel, - data : template, - }); - } - - Util.exec(callback, error, data); - }); - }, function(callback) { - minify(PATH_INDEX, { - log : true, - returnName : true - }, callback); - }); + server.start(); } function readConfig(callback) { diff --git a/lib/server.js b/lib/server.js index 67734c37..d61b7048 100644 --- a/lib/server.js +++ b/lib/server.js @@ -27,7 +27,10 @@ minify = require(DIR_SERVER + 'minify'), terminal = !WIN ? require(DIR_SERVER + 'terminal') : function() {}, + rest = require(DIR_SERVER + 'rest'), + route = require(DIR_SERVER + 'route'), + join = require(DIR_SERVER + 'join'), ponse = require(DIR_SERVER + 'ponse'), express = require(DIR_SERVER + 'express'), @@ -38,7 +41,7 @@ Util.exec(next); }, - webconsole, expressApp, Route; + webconsole, expressApp; emptyFunc.middle = function() { return emptyFunc; @@ -52,18 +55,12 @@ /** * start server function * - * @param options */ - function start(options) { + function start() { var port, ip, HTTP = 'http://', config = main.config; - if (!options) - options = {}; - - Route = options.route; - port = process.env.PORT || /* c9 */ process.env.VCAP_APP_PORT || /* cloudfoundry */ config.port, @@ -93,7 +90,7 @@ funcs = [ rest, - Route, + route, join({ minify: isMinify diff --git a/lib/server/route.js b/lib/server/route.js new file mode 100644 index 00000000..3e90300c --- /dev/null +++ b/lib/server/route.js @@ -0,0 +1,202 @@ +(function() { + 'use strict'; + + var DIR = __dirname + '/../../', + DIR_HTML = DIR + 'html/', + DIR_LIB = DIR + 'lib/', + DIR_SERVER = __dirname + '/', + DIR_FS = DIR_HTML + 'fs/', + + fs = require('fs'), + + main = require(DIR_SERVER + 'main'), + mellow = require(DIR_SERVER + 'mellow'), + ponse = require(DIR_SERVER + 'ponse'), + files = require(DIR_SERVER + 'files'), + minify = require(DIR_SERVER + 'minify'), + + Util = require(DIR_LIB + 'util'), + CloudFunc = require(DIR_LIB + 'cloudfunc'), + format = require(DIR_LIB + 'format'), + + Config = main.config, + + PATH_INDEX = DIR_FS + 'index.html', + + TMPL_PATH = [ + 'file', + 'panel', + 'path', + 'pathLink', + 'link', + ], + + Template = {}, + + FS = CloudFunc.FS; + + module.exports = route; + + readFiles(); + + /** + * additional processing of index file + */ + function indexProcessing(options) { + var keysPanel, left, right, + LEFT = CloudFunc.PANEL_LEFT, + RIGHT = CloudFunc.PANEL_RIGHT, + data = options.data, + panel = options.panel; + + if (!Config.showKeysPanel) { + keysPanel = '
' + name); + + p.name = DIR_HTML + name + '.html'; + ponse.sendFile(p); + } else if (isFS) { + name = Util.rmStrOnce(name, CloudFunc.FS) || main.SLASH; + path = mellow.convertPath(name); + + mellow.read(path, function(error, dir) { + if (dir) + dir.path = format.addSlashToEnd(name); + + if (error) + if (error.code !== 'ENOTDIR') + ponse.sendError(error, p); + else + fs.realpath(path, function(error, pathReal) { + if (!error) + p.name = pathReal; + else + p.name = path; + + p.gzip = false; + ponse.sendFile(p); + }); + else + buildIndex(dir, function(error, data) { + var NOT_LOG = true; + + p.name = PATH_INDEX; + + if (error) + ponse.sendError(error, p); + else + ponse.send(data, p, NOT_LOG); + }); + }); + } + } + } + + function buildIndex(json, callback) { + var isMinify = Config.minify; + + Util.exec.if(!isMinify, function(error, name) { + fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) { + var panel, data; + + if (!error) { + panel = CloudFunc.buildFromJSON({ + data : json, + template : Template + }), + + data = indexProcessing({ + panel : panel, + data : template, + }); + } + + Util.exec(callback, error, data); + }); + }, function(callback) { + minify(PATH_INDEX, { + log : true, + returnName : true + }, callback); + }); + } +})();