From cd296f84bec7af6922043bb0d97591f9e5dfa063 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 16 Sep 2014 04:51:22 -0400 Subject: [PATCH] feature(config) add --- cloudcmd.js | 76 +++++++------------------------------------- lib/server.js | 40 ++++++++--------------- lib/server/config.js | 40 +++++++++++++++++++++++ lib/server/rest.js | 30 +++++++---------- lib/server/route.js | 10 +++--- 5 files changed, 81 insertions(+), 115 deletions(-) create mode 100644 lib/server/config.js diff --git a/cloudcmd.js b/cloudcmd.js index 1e421763..a382350d 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -5,84 +5,32 @@ DIR_LIB = DIR + 'lib/', DIR_SERVER = DIR + 'lib/server/', - fs = require('fs'), - win = require(DIR_SERVER + 'win'), - main = require(DIR_SERVER + 'main'), update = require(DIR_SERVER + 'update'), - tryRequire = require(DIR_SERVER + 'tryRequire'), - Util = require(DIR_LIB + 'util'), server = require(DIR_LIB + 'server'), - Config = main.config; + config = require(DIR_SERVER + 'config'); module.exports = function(params) { - readConfig(function(error, config) { - var keys; + var keys; + + if (params) { + keys = Object.keys(params); - if (error) - Util.log(error.message); - - if (!config) - config = {}; - - if (params) { - keys = Object.keys(params); - - keys.forEach(function(item) { - config[item] = params[item]; - }); - } - - init(config); - }); + keys.forEach(function(name) { + config(name, params[name]); + }); + } win.prepareCodePage(); - }; - - function init(config) { - Util.log('server dir: ' + DIR); if (update) update.get(); - if (config) { - main.config = Config = config; - - if (config.test) - Config.server = false; - } - - if (Config.logs) { - Util.log('log param setted up in config.json\n' + - 'from now all logs will be writed to log.txt'); - - writeLogsToFile(); - } + if (config('test')) + config(server, false); server(); - } - - function readConfig(callback) { - var error, - configPath = DIR + 'json/config.json', - config = tryRequire(configPath, function(err) { - if (err) - error = err; - }); - - Util.checkArgs(arguments, ['callback']); - - callback(error, config); - } - - /* function sets stdout to file log.txt */ - function writeLogsToFile() { - var stdout = process.stdout, - writeFile = fs.createWriteStream('log.txt'), - write = writeFile.write.bind(writeFile); - - stdout.write = write; - } + }; })(); diff --git a/lib/server.js b/lib/server.js index 99e4a68b..b5dc45c1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,17 +1,7 @@ (function() { 'use strict'; - if (!global.cloudcmd) - return console.log( - '# server.js' + '\n' + - '# -----------' + '\n' + - '# Module is part of Cloud Commander,' + '\n' + - '# easy to use web server.' + '\n' + - '# http://cloudcmd.io' + '\n'); - - var main = global.cloudcmd.main, - - DIR = __dirname + '/../', + var DIR = __dirname + '/../', DIR_LIB = DIR + 'lib/', DIR_SERVER = DIR_LIB + 'server/', @@ -31,6 +21,8 @@ rest = require(DIR_SERVER + 'rest'), route = require(DIR_SERVER + 'route'), + config = require(DIR_SERVER + 'config'), + join = require(DIR_SERVER + 'join'), ponse = require(DIR_SERVER + 'ponse'), express = require(DIR_SERVER + 'express'), @@ -58,35 +50,31 @@ */ module.exports = function() { var port, ip, - HTTP = 'http://', - config = main.config; + HTTP = 'http://'; port = process.env.PORT || /* c9 */ process.env.VCAP_APP_PORT || /* cloudfoundry */ - config.port, + config('port'), ip = process.env.IP || /* c9 */ - config.ip || + config('ip') || '0.0.0.0'; - if (config.server) + if (config('server')) createServer(port, ip, HTTP); }; function createServer(port, ip, protocol, callback) { var server, app, respondApp, - config = main.config, middle = controller.middle(DIR), isOption = function(name) { - return function() { - return main.config[name]; - }; + return config(name); }, - isMinify = isOption('minify'), - isOnline = isOption('online'), + isMinify = isOption.bind(null, 'minify'), + isOnline = isOption.bind(null, 'online'), funcs = [ rest, @@ -140,10 +128,9 @@ function addSockets(server) { var socket, msg, - config = main.config, status = 'off'; - if (config.socket && io) { + if (io && config('socket')) { socket = io.listen(server); if (socket) { @@ -177,8 +164,7 @@ * @param res - ответ сервера (Response) */ function controller(dir, req, res) { - var config = main.config, - parsedUrl = URL.parse(req.url), + var parsedUrl = URL.parse(req.url), query = parsedUrl.search || '', name = ponse.getPathName(req); @@ -189,7 +175,7 @@ ponse.sendFile({ name : name, - cache : config.cache, + cache : config('cache'), gzip : true, request : req, response : res diff --git a/lib/server/config.js b/lib/server/config.js new file mode 100644 index 00000000..dffa32fd --- /dev/null +++ b/lib/server/config.js @@ -0,0 +1,40 @@ +(function() { + + var DIR_SERVER = __dirname + '/', + DIR_LIB = DIR_SERVER + '../', + DIR = DIR_SERVER + '../../', + + fs = require('fs'), + + Util = require(DIR_LIB + 'util'), + tryRequire = require(DIR_SERVER + 'tryRequire'), + + ConfigPath = DIR + 'json/config.json', + + config = tryRequire(ConfigPath) || {}; + + module.exports = function(key, value) { + var result; + + if (value === undefined) + result = config[key]; + else + config[key] = value; + + return result; + }; + + module.exports.save = function(callback) { + var data = Util.stringifyJSON(config); + + Util.checkArgs(arguments, ['callback']); + + if (data) + fs.writeFile(ConfigPath, data, callback); + else + callback({ + message: 'Error: config is empty!' + }); + }; + +})(); diff --git a/lib/server/rest.js b/lib/server/rest.js index d5bda55f..7b639c37 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -1,21 +1,15 @@ -/* RESTful module */ - (function() { 'use strict'; - if (!global.cloudcmd) - return console.log( - '# rest.js' + '\n' + - '# -----------' + '\n' + - '# Module is part of Cloud Commander,' + '\n' + - '# used for work with REST API.' + '\n' + - '# If you wont to see at work set rest: true' + '\n' + - '# and apiURL in config.json' + '\n' + - '# http://cloudcmd.io' + '\n'); + /* + '# rest.js' + '\n' + + '# -----------' + '\n' + + '# Module is part of Cloud Commander,' + '\n' + + '# used for work with REST API.' + '\n' + + '# http://cloudcmd.io' + '\n'); + */ - var main = global.cloudcmd.main, - - DIR = './', + var DIR = './', DIR_LIB = DIR + '../', DIR_ROOT = __dirname + '/' + DIR_LIB + '../', DIR_JSON = DIR_ROOT + 'json/', @@ -36,6 +30,7 @@ mellow = require(DIR + 'mellow'), ponse = require(DIR + 'ponse'), pipe = require(DIR + 'pipe'), + config = require(DIR + 'config'), isWin32 = process.platform === 'win32', @@ -270,7 +265,7 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(name, body, callback) { - var cmd, files, json, config, data, from, to, error; + var cmd, files, json, data, from, to, error; Util.checkArgs(arguments, ['name', 'body', 'callback']); @@ -386,7 +381,6 @@ case 'config': var passwd = files && files.password, sha = crypto.createHash('sha1'); - config = main.config; if (passwd) { sha.update(passwd); @@ -395,12 +389,12 @@ } Object.keys(files).forEach(function(name) { - config[name] = files[name]; + config(name, files[name]); }); json = Util.stringifyJSON(config) + '\n'; - fs.writeFile(DIR_JSON + 'config.json', json, function(error) { + config.save(function(error) { data = formatMsg('config', name); callback(error, data); }); diff --git a/lib/server/route.js b/lib/server/route.js index 4a33e428..af981957 100644 --- a/lib/server/route.js +++ b/lib/server/route.js @@ -9,18 +9,16 @@ 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'), + config = require(DIR_SERVER + 'config'), 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 = [ @@ -51,7 +49,7 @@ data = options.data, panel = options.panel; - if (!Config.showKeysPanel) { + if (!config('showKeysPanel')) { keysPanel = '