refactored

This commit is contained in:
coderaiser 2013-02-05 08:48:18 -05:00
parent c13c012cc7
commit 866cf30790
4 changed files with 73 additions and 102 deletions

View file

@ -11,12 +11,12 @@
path = main.path,
fs = main.fs,
CloudFunc = main.cloudfunc,
AppCache = main.appcache,
Util = main.util,
update = main.update,
Server = main.require(LIBDIR + 'server'),
Minify = Server.Minify,
srv = Server.CloudServer,
Minify = main.minify,
Config = main.config,
REQUEST = 'request',
@ -30,7 +30,6 @@
readConfig();
Server.start(Config, {
appcache : appCacheProcessing,
index : indexProcessing,
minimize : minimize,
rest : rest,
route : route
@ -81,16 +80,15 @@
* init and process of appcache if it allowed in config
*/
function appCacheProcessing(){
var lAppCache = srv.AppCache,
lFiles = [
var lFiles = [
{'//themes.googleusercontent.com/static/fonts/droidsansmono/v4/ns-m2xQYezAtqh7ai59hJUYuTAAIFFn5GTWtryCmBQ4.woff' : './font/DroidSansMono.woff'},
{'//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js' : './lib/client/jquery.js'}];
if(srv.Minify._allowed.css)
if(Config.minification.css)
lFiles.push('node_modules/minify/min/all.min.css');
lAppCache.addFiles(lFiles);
lAppCache.createManifest();
AppCache.addFiles(lFiles);
AppCache.createManifest();
}
/**

View file

@ -11,24 +11,13 @@
var main = global.cloudcmd.main,
/*
* Обьект содержащий все функции и переменные
* серверной части Cloud Commander'а
*/
CloudServer = {
/* base configuration */
Config : {
server : true,
socket : true,
port : 80
Config = {
server : true,
socket : true,
port : 80
},
/* server varible */
Server : {},
/* КОНСТАНТЫ */
INDEX : main.DIR + 'html/index.html'
},
DIR = main.Dir,
LIBDIR = main.LIBDIR,
SRVDIR = main.SRVDIR,
@ -37,23 +26,18 @@
Path = main.path,
Querystring = main.querystring,
Minify = main.minify,
AppCache = main.appcache,
Socket = main.socket,
Minify = main.minify,
AppCache = main.appcache,
Socket = main.socket,
/* node v0.4 not contains zlib */
Zlib = main.zlib; /* модуль для сжатия данных gzip-ом*/
if(!Zlib)
Util.log('to use gzip-commpression' +
'you should use newer node version\n');
/* добавляем модуль с функциями */
var Util = main.util;
http = main.http,
Util = main.util,
Server, Rest, Route, Minimize, Port, IP;
/* базовая инициализация */
CloudServer.init = function(pAppCachProcessing){
var lConfig = this.Config,
lMinifyAllowed = lConfig.minification;
function init(pAppCachProcessing){
var lMinifyAllowed = Config.minification;
/* Change default parameters of
* js/css/html minification
@ -61,63 +45,59 @@
Minify.setAllowed(lMinifyAllowed);
/* Если нужно минимизируем скрипты */
Util.exec(CloudServer.minimize, lMinifyAllowed);
Util.exec(Minimize, lMinifyAllowed);
/* создаём файл app cache */
if( lConfig.appcache && AppCache && lConfig.server )
if( Config.appcache && AppCache && Config.server )
Util.exec( pAppCachProcessing );
};
}
/**
* Функция создаёт сервер
* start server function
* @param pConfig
* @param pProcessing {index, appcache, rest}
*/
CloudServer.start = function (pConfig, pProcessing) {
exports.start = function start(pConfig, pProcessing) {
if(!pProcessing)
pProcessing = {};
if(pConfig)
this.Config = pConfig;
Config = pConfig;
else
Util.log('warning: configuretion file config.json not found...\n' +
'using default values...\n' +
JSON.stringify(this.Config));
JSON.stringify(Config));
var lConfig = this.Config;
Rest = pProcessing.rest;
Route = pProcessing.route;
Minimize = pProcessing.minimize;
CloudServer.rest = pProcessing.rest;
CloudServer.route = pProcessing.route;
CloudServer.minimize = pProcessing.minimize;
init(pProcessing.appcache);
this.init(pProcessing.appcache);
this.Port = process.env.PORT || /* c9 */
Port = process.env.PORT || /* c9 */
process.env.app_port || /* nodester */
process.env.VCAP_APP_PORT || /* cloudfoundry */
lConfig.port;
Config.port;
this.IP = process.env.IP || /* c9 */
this.Config.ip ||
(main.WIN32 ?
'127.0.0.1' :
'0.0.0.0');
IP = process.env.IP || /* c9 */
Config.ip ||
(main.WIN32 ? '127.0.0.1' : '0.0.0.0');
/* server mode or testing mode */
if (lConfig.server) {
var http = main.http,
lError = Util.tryCatchLog(Util.bind(function(){
this.Server = http.createServer( controller );
this.Server.listen(this.Port, this.IP);
if (Config.server) {
var lError = Util.tryCatchLog(function(){
Server = http.createServer( controller );
Server.listen(Port, IP);
var lListen;
if(lConfig.socket && Socket)
lListen = Socket.listen(this.Server);
if(Config.socket && Socket)
lListen = Socket.listen(Server);
Util.log('* Sockets ' + (lListen ? 'running' : 'disabled'));
Util.log('* Server running at http://' + this.IP + ':' + this.Port);
}, this));
Util.log('* Server running at http://' + IP + ':' + Port);
});
if(lError){
Util.log('Cloud Commander server could not started');
@ -125,7 +105,7 @@
}
}else
Util.log('Cloud Commander testing mode');
};
}
/**
@ -138,7 +118,6 @@
{
/* Читаем содержимое папки, переданное в url */
var lRet,
lConfig = CloudServer.Config,
lURL = main.url,
lParsedUrl = lURL.parse(pReq.url),
lPath = lParsedUrl.pathname;
@ -149,14 +128,14 @@
Util.log("request for " + lPath + " received...");
if( lConfig.rest )
lRet = Util.exec(CloudServer.rest, {
if( Config.rest )
lRet = Util.exec(Rest, {
request : pReq,
response : pRes
});
if( !lRet && CloudServer.route)
lRet = Util.exec(CloudServer.route, {
if( !lRet && Route)
lRet = Util.exec(Route, {
name : lPath,
request : pReq,
response : pRes
@ -168,7 +147,7 @@
Util.log('reading ' + lName);
/* watching is file changed */
if(lConfig.appcache)
if(Config.appcache)
AppCache.watch(lName);
Util.log(Path.basename(lName));
@ -198,17 +177,4 @@
}
}
/**
* start server function
* @param pConfig
* @param pProcessing {index, appcache, rest}
*/
exports.start = function(pConfig, pProcessing){
CloudServer.start(pConfig, pProcessing);
};
exports.CloudServer = CloudServer;
exports.Minify = Minify;
exports.AppCache = AppCache;
})();

View file

@ -49,7 +49,7 @@
fs.stat(lPath, function(pError, pStat){
if(!pError)
if(pStat.isDirectory())
fs.readdir(lPath, call(readDir, pParams) );
fs.readdir(lPath, Util.call(readDir, pParams) );
else
main.sendFile({
name : lPath,
@ -123,7 +123,7 @@
stats : lStats,
};
fs.stat( lName, call(getFilesStat, lParams) );
fs.stat( lName, Util.call(getFilesStat, lParams) );
}
}
else
@ -227,7 +227,7 @@
lList = '<ul id=left class=panel>' + lPanel + '</ul>' +
'<ul id=right class=panel>' + lPanel + '</ul>';
fs.readFile(INDEX, call(readFile, {
fs.readFile(INDEX, Util.call(readFile, {
list : lList,
request : lReq,
response : lRes
@ -360,18 +360,6 @@
return lRet;
}
function call(pFunc, pParams){
var lFunc = function(pError, pData){
Util.exec(pFunc, {
error : pError,
data : pData,
params : pParams
});
};
return lFunc;
}
function noJS(pReq){
var lNoJS, lPath;
@ -432,7 +420,7 @@
},
function(pCallBack){
zlib.gzip (p.data, call(gzipData, {
zlib.gzip (p.data, Util.call(gzipData, {
callback : pCallBack
}));
});

View file

@ -33,6 +33,25 @@ Util = exports || {};
return lRet;
};
/**
* callback for functions(pError, pData)
* thet moves on our parameters.
*
* @param pFunc
* @param pParams
*/
Util.call = function(pFunc, pParams){
var lFunc = function(pError, pData){
Util.exec(pFunc, {
error : pError,
data : pData,
params : pParams
});
};
return lFunc;
};
/**
* Функция ищет в имени файла расширение
* и если находит возвращает true