From 8eac958e861ee168b59c50d1e5abd1c6b94fcc69 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 22 Feb 2013 11:36:09 -0500 Subject: [PATCH] added ability to change configs without restart --- ChangeLog | 2 + cloudcmd.js | 13 +- lib/server.js | 368 +++++++++++++++++++++++++------------------------- 3 files changed, 190 insertions(+), 193 deletions(-) diff --git a/ChangeLog b/ChangeLog index bfce1e93..553da0c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -166,6 +166,8 @@ for now it's much simplier. * Fixed the first show of menu in firefox. +* Added ability to change configs without restart. + 2012.12.12, Version 0.1.8 diff --git a/cloudcmd.js b/cloudcmd.js index 8aeda716..7f74ef3e 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -179,8 +179,7 @@ } function readConfig(pCallBack){ - var lConfPath = JSONDIR + 'config.json', - lReaded; + var lConfPath = JSONDIR + 'config.json'; fs.readFile(lConfPath, function(pError, pData){ if(!pError){ @@ -197,14 +196,10 @@ if(!Config) fs.watch(lConfPath, function(){ - if(!lReaded){ - lReaded = true; - readConfig(); - } - + /* every catch up - calling twice */ setTimeout(function() { - lReaded = false; - }, 10000); + readConfig(); + }, 1000); }); } diff --git a/lib/server.js b/lib/server.js index 879b8861..078c945a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,184 +1,184 @@ -(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://coderaiser.github.com/cloudcmd' + '\n'); - - var main = global.cloudcmd.main, - - /* base configuration */ - Config = { - server : true, - socket : true, - port : 80 - }, - - DIR = main.Dir, - LIBDIR = main.LIBDIR, - SRVDIR = main.SRVDIR, - - /* модуль для работы с путями*/ - Path = main.path, - Querystring = main.querystring, - - Minify = main.minify, - AppCache = main.appcache, - Socket = main.socket, - - http = main.http, - Util = main.util, - - Server, Rest, Route, Minimize, Port, IP; - - /* базовая инициализация */ - function init(pAppCachProcessing){ - var lMinifyAllowed = Config.minification; - - /* Change default parameters of - * js/css/html minification - */ - Minify.setAllowed(lMinifyAllowed); - - /* Если нужно минимизируем скрипты */ - Util.exec(Minimize, lMinifyAllowed); - - /* создаём файл app cache */ - if( Config.appcache && AppCache && Config.server ) - Util.exec( pAppCachProcessing ); - } - - - /** - * start server function - * @param pConfig - * @param pProcessing {index, appcache, rest} - */ - function start(pConfig, pProcessing) { - if(!pProcessing) - pProcessing = {}; - - if(pConfig) - Config = pConfig; - - else - Util.log('warning: configuretion file config.json not found...\n' + - 'using default values...\n' + - JSON.stringify(Config)); - - Rest = pProcessing.rest; - Route = pProcessing.route; - Minimize = pProcessing.minimize; - - init(pProcessing.appcache); - - Port = process.env.PORT || /* c9 */ - process.env.app_port || /* nodester */ - process.env.VCAP_APP_PORT || /* cloudfoundry */ - Config.port; - - IP = process.env.IP || /* c9 */ - Config.ip || - (main.WIN32 ? '127.0.0.1' : '0.0.0.0'); - - /* server mode or testing mode */ - if (Config.server) { - var lError = Util.tryCatchLog(function(){ - Server = http.createServer( controller ); - Server.listen(Port, IP); - - var lListen; - if(Config.socket && Socket) - lListen = Socket.listen(Server); - - Util.log('* Sockets ' + (lListen ? 'running' : 'disabled')); - Util.log('* Server running at http://' + IP + ':' + Port); - }); - - if(lError){ - Util.log('Cloud Commander server could not started'); - Util.log(lError); - } - }else - Util.log('Cloud Commander testing mode'); - } - - - /** - * Главная функция, через которую проихсодит - * взаимодействие, обмен данными с клиентом - * @param req - запрос клиента (Request) - * @param res - ответ сервера (Response) - */ - function controller(pReq, pRes) - { - /* Читаем содержимое папки, переданное в url */ - var lRet, - lURL = main.url, - lParsedUrl = lURL.parse(pReq.url), - lPath = lParsedUrl.pathname; - - /* added supporting of Russian language in directory names */ - lPath = Querystring.unescape(lPath); - Util.log('pathname: ' + lPath); - - Util.log("request for " + lPath + " received..."); - - if( Config.rest ) - lRet = Util.exec(Rest, { - request : pReq, - response : pRes - }); - - if( !lRet && Route) - lRet = Util.exec(Route, { - name : lPath, - request : pReq, - response : pRes - }); - - if(!lRet){ - /* добавляем текующий каталог к пути */ - var lName = '.' + lPath; - Util.log('reading ' + lName); - - /* watching is file changed */ - if(Config.appcache) - AppCache.watch(lName); - - Util.log(Path.basename(lName)); - - var lMin = Minify.allowed, - lExt = Util.getExtension(lName), - lResult = lExt === '.js' && lMin.js || - lExt === '.css' && lMin.css || - lExt === '.html' && lMin.html; - - Util.ifExec(!lResult, - function(pParams){ - var lSendName = pParams && pParams.name || lName; - - main.sendFile({ - name : lSendName, - request : pReq, - response : pRes - }); - }, function(pCallBack){ - Minify.optimize(lName, { - request : pReq, - response : pRes, - callback : pCallBack, - returnName : true - }); - }); - } - } - - - exports.start = start; - -})(); +(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://coderaiser.github.com/cloudcmd' + '\n'); + + var main = global.cloudcmd.main, + + /* base configuration */ + Config = { + server : true, + socket : true, + port : 80 + }, + + DIR = main.Dir, + LIBDIR = main.LIBDIR, + SRVDIR = main.SRVDIR, + + /* модуль для работы с путями*/ + Path = main.path, + Querystring = main.querystring, + + Minify = main.minify, + AppCache = main.appcache, + Socket = main.socket, + + http = main.http, + Util = main.util, + + Server, Rest, Route, Minimize, Port, IP; + + /* базовая инициализация */ + function init(pAppCachProcessing){ + var lMinifyAllowed = Config.minification; + + /* Change default parameters of + * js/css/html minification + */ + Minify.setAllowed(lMinifyAllowed); + + /* Если нужно минимизируем скрипты */ + Util.exec(Minimize, lMinifyAllowed); + + /* создаём файл app cache */ + if( Config.appcache && AppCache && Config.server ) + Util.exec( pAppCachProcessing ); + } + + + /** + * start server function + * @param pConfig + * @param pProcessing {index, appcache, rest} + */ + function start(pConfig, pProcessing) { + if(!pProcessing) + pProcessing = {}; + + if(pConfig) + Config = pConfig; + + else + Util.log('warning: configuretion file config.json not found...\n' + + 'using default values...\n' + + JSON.stringify(Config)); + + Rest = pProcessing.rest; + Route = pProcessing.route; + Minimize = pProcessing.minimize; + + init(pProcessing.appcache); + + Port = process.env.PORT || /* c9 */ + process.env.app_port || /* nodester */ + process.env.VCAP_APP_PORT || /* cloudfoundry */ + Config.port; + + IP = process.env.IP || /* c9 */ + Config.ip || + (main.WIN32 ? '127.0.0.1' : '0.0.0.0'); + + /* server mode or testing mode */ + if (Config.server) { + var lError = Util.tryCatchLog(function(){ + Server = http.createServer( controller ); + Server.listen(Port, IP); + + var lListen; + if(Config.socket && Socket) + lListen = Socket.listen(Server); + + Util.log('* Sockets ' + (lListen ? 'running' : 'disabled')); + Util.log('* Server running at http://' + IP + ':' + Port); + }); + + if(lError){ + Util.log('Cloud Commander server could not started'); + Util.log(lError); + } + }else + Util.log('Cloud Commander testing mode'); + } + + + /** + * Главная функция, через которую проихсодит + * взаимодействие, обмен данными с клиентом + * @param req - запрос клиента (Request) + * @param res - ответ сервера (Response) + */ + function controller(pReq, pRes) + { + /* Читаем содержимое папки, переданное в url */ + var lRet, + lURL = main.url, + lParsedUrl = lURL.parse(pReq.url), + lPath = lParsedUrl.pathname; + + /* added supporting of Russian language in directory names */ + lPath = Querystring.unescape(lPath); + Util.log('pathname: ' + lPath); + + Util.log("request for " + lPath + " received..."); + + if( Config.rest ) + lRet = Util.exec(Rest, { + request : pReq, + response : pRes + }); + + if( !lRet && Route) + lRet = Util.exec(Route, { + name : lPath, + request : pReq, + response : pRes + }); + + if(!lRet){ + /* добавляем текующий каталог к пути */ + var lName = '.' + lPath; + Util.log('reading ' + lName); + + /* watching is file changed */ + if(Config.appcache) + AppCache.watch(lName); + + Util.log(Path.basename(lName)); + + var lMin = Minify.allowed, + lExt = Util.getExtension(lName), + lResult = lExt === '.js' && lMin.js || + lExt === '.css' && lMin.css || + lExt === '.html' && lMin.html; + + Util.ifExec(!lResult, + function(pParams){ + var lSendName = pParams && pParams.name || lName; + + main.sendFile({ + name : lSendName, + request : pReq, + response : pRes + }); + }, function(pCallBack){ + Minify.optimize(lName, { + request : pReq, + response : pRes, + callback : pCallBack, + returnName : true + }); + }); + } + } + + + exports.start = start; + +})();