From 79f2f34a79152644ccd608e55bdd673038dc1ab3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 22 Feb 2013 11:18:19 -0500 Subject: [PATCH] minor changes --- cloudcmd.js | 54 +++++++++++++++++++++++++++++++++--------- json/config.json | 2 +- lib/server/appcache.js | 4 ++-- lib/server/main.js | 11 ++++++++- lib/server/rest.js | 13 +++++----- 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/cloudcmd.js b/cloudcmd.js index 6d2b73a7..8aeda716 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -8,6 +8,7 @@ SRVDIR = main.SRVDIR, CLIENTDIR = LIBDIR + 'client', HTMLDIR = main.HTMLDIR, + JSONDIR = main.JSONDIR, path = main.path, fs = main.fs, @@ -18,7 +19,7 @@ server = main.librequire('server'), Minify = main.minify, - Config = main.config, + Config, REQUEST = 'request', RESPONSE = 'response', @@ -29,16 +30,8 @@ * Win32 should be backslashes */ DIR = main.DIR; - readConfig(); - server.start(Config, { - appcache : appCacheProcessing, - minimize : minimize, - rest : rest, - route : route - }); + readConfig(init); - if(update) - update.get(); /** * additional processing of index file @@ -136,7 +129,16 @@ return Util.exec(main.rest, pConnectionData); } - function readConfig(){ + function init(){ + server.start(Config, { + appcache : appCacheProcessing, + minimize : minimize, + rest : rest, + route : route + }); + + if(update) + update.get(); /* Determining server.js directory * and chang current process directory @@ -176,6 +178,36 @@ } } + function readConfig(pCallBack){ + var lConfPath = JSONDIR + 'config.json', + lReaded; + + fs.readFile(lConfPath, function(pError, pData){ + if(!pError){ + Util.log('config: readed'); + var lStr = pData.toString(); + Util.log( lStr ); + main.config = Config = JSON.parse(lStr); + } + else + Util.log(pError); + + Util.exec(pCallBack); + }); + + if(!Config) + fs.watch(lConfPath, function(){ + if(!lReaded){ + lReaded = true; + readConfig(); + } + + setTimeout(function() { + lReaded = false; + }, 10000); + }); + } + /** * routing of server queries */ diff --git a/json/config.json b/json/config.json index 609d9776..e53c7ca4 100644 --- a/json/config.json +++ b/json/config.json @@ -2,7 +2,7 @@ "api_url" : "/api/v1", "appcache" : false, "minification" : { - "js" : false, + "js" : true, "css" : true, "html" : true, "img" : true diff --git a/lib/server/appcache.js b/lib/server/appcache.js index 00360dfc..0a7c7e72 100644 --- a/lib/server/appcache.js +++ b/lib/server/appcache.js @@ -50,7 +50,7 @@ FallBack_s += lName + ' ' + lCurrentName[lName] + '\n'; exports.watch(lCurrentName[lName]); } - + else exports.watch(pFileNames[i]); } else exports.watch(pFileNames); @@ -90,7 +90,7 @@ if(fs.exists) fs.exists(pFileName, lWatch_f); else lWatch_f(); - + NamesList_s += pFileName + '\n'; FileNames[pFileName] = true; } diff --git a/lib/server/main.js b/lib/server/main.js index 7cb1848d..1a80fb20 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -53,6 +53,8 @@ /* compitability with old versions of node */ exports.fs.exists = exports.fs.exists || exports.path.exists; + //exports.fs.watch = isWin32 ? fs.watch : linuxWatch; + /* Needed Modules */ exports.util = Util = require(LIBDIR + 'util'), @@ -60,7 +62,6 @@ exports.zlib = zlib = mrequire('zlib'), /* Main Information */ - exports.config = jsonrequire('config'); exports.modules = jsonrequire('modules'); exports.ext = ext = jsonrequire('ext'); exports.mainpackage = rootrequire('package'); @@ -240,4 +241,12 @@ return lQuery; } + + function linuxWatch(pFile, pCallBack){ + fs.watchFile(pFile, function(pCurr, pPrev){ + if(pCurr.mtime !== pPrev.mtime) + Util.exec(pCallBack); + }); + } + })(); diff --git a/lib/server/rest.js b/lib/server/rest.js index 010e108a..866841ee 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -1,7 +1,7 @@ /* RESTfull module */ (function(){ - "use strict"; + 'use strict'; if(!global.cloudcmd) return console.log( @@ -15,8 +15,7 @@ var main = global.cloudcmd.main, Util = main.util, - Config = main.config, - APIURL = Config.api_url, + OK = 200, Header = main.generateHeaders('api.json', false); @@ -29,13 +28,15 @@ lReq = pParams.request, lRes = pParams.response, lUrl = lReq.url, - lMethod = lReq.method; + lMethod = lReq.method, + lConfig = main.config, + lAPIURL = lConfig.api_url; - if( Util.isContainStr(lUrl, APIURL) ){ + if( Util.isContainStr(lUrl, lAPIURL) ){ lRet = true; getBody(lReq, function(pBody){ - var lCommand = Util.removeStr(lUrl, APIURL), + var lCommand = Util.removeStr(lUrl, lAPIURL), lData = getData({ command : lCommand, method : lMethod,