refactored

This commit is contained in:
coderaiser 2013-01-30 05:31:15 -05:00
parent e8506d94e3
commit 7ca72302c0
4 changed files with 64 additions and 72 deletions

View file

@ -15,6 +15,7 @@
update = main.update,
Server = main.require(LIBDIR + 'server'),
Minify = Server.Minify,
srv = Server.CloudServer,
Config = main.config;
@ -47,7 +48,7 @@
* меняем в index.html обычные css на
* минифицированый
*/
if(srv.Minify._allowed.css){
if(Minify._allowed.css){
var lPath = '/' + srv.Minify.MinFolder.replace(DIR, '');
lReplace_s = '<link rel=stylesheet href="/css/reset.css">';
lData = Util.removeStr(lData, lReplace_s)

View file

@ -78,11 +78,17 @@
/* модуль для работы с путями*/
Path = main.path,
crypto = main.crypto,
Fs = main.fs, /* модуль для работы с файловой системой*/
Querystring = main.querystring,
/* node v0.4 not contains zlib */
/* Обьект для работы с кэшем */
Cache = main.cache,
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' +
@ -91,36 +97,25 @@
/* добавляем модуль с функциями */
var CloudFunc = main.cloudfunc,
Util = main.util;
/* Обьект для работы с кэшем */
CloudServer.Cache = main.cache,
CloudServer.Minify = main.minify,
CloudServer.AppCache = main.appcache,
CloudServer.Socket = main.socket;
/* базовая инициализация */
CloudServer.init = function(pAppCachProcessing){
var lConfig = this.Config,
lMinify = this.Minify,
lCache = this.Cache,
lAppCache = this.AppCache,
lMinifyAllowed = lConfig.minification;
/* Переменная в которой храниться кэш*/
lCache.setAllowed(lConfig.cache);
Cache.setAllowed(lConfig.cache);
/* Change default parameters of
* js/css/html minification
*/
lMinify.setAllowed(lMinifyAllowed);
Minify.setAllowed(lMinifyAllowed);
/* Если нужно минимизируем скрипты */
Util.exec(CloudServer.minimize, lMinifyAllowed);
/* создаём файл app cache */
if( lConfig.appcache && lAppCache && lConfig.server )
if( lConfig.appcache && AppCache && lConfig.server )
Util.exec( pAppCachProcessing );
};
@ -169,9 +164,9 @@
this.Server.listen(this.Port, this.IP);
var lListen;
if(lConfig.socket && CloudServer.Socket){
lListen = CloudServer.Socket.listen(this.Server);
}
if(lConfig.socket && Socket)
lListen = Socket.listen(this.Server);
Util.log('* Sockets ' + (lListen ? 'running' : 'disabled'));
Util.log('* Server running at http://' + this.IP + ':' + this.Port);
}, this));
@ -287,7 +282,7 @@
* сжатый файл - если gzip-поддерживаеться браузером
* не сжатый - в обратном случае
*/
var lFileData = CloudServer.Cache.get(
var lFileData = Cache.get(
CloudServer.Gzip?(lName+'_gzip') : lName);
Util.log(Path.basename(lName));
@ -311,7 +306,7 @@
/* if file not in one of caches
* and minimizing setted then minimize it
*/
else if(lName.indexOf('min') < 0 && CloudServer.Minify){
else if(lName.indexOf('min') < 0 && Minify){
var lMin_o = lConfig.minification,
lCheck_f = function(pExt){
@ -323,8 +318,7 @@
(lCheck_f('html') && lMin_o.html);
if(lResult)
lResult = CloudServer.Minify.optimize(lName, {
name : lName,
lResult = Minify.optimize(lName, {
request : pReq,
response : pRes,
callback : function(pFileData){
@ -403,12 +397,9 @@
* загружаем сжатый html-файл в дальнейшем
*/
var lMinFileName = CloudServer.Minify.MinFolder +
crypto.createHash('sha1')
.update(main.DIR + 'html/index.html')
.digest('hex') + '.html';
var lMinFileName = Minify.getName(main.DIR + 'html/index.html');
CloudServer.INDEX = (CloudServer.Minify._allowed.html ?
CloudServer.INDEX = (Minify._allowed.html ?
lMinFileName : CloudServer.INDEX);
/*
@ -555,7 +546,7 @@
* с жатием или без, взависимости
* от настроек
*/
var lFileData = CloudServer.Cache.get(CloudServer.INDEX);
var lFileData = Cache.get(CloudServer.INDEX);
/* если их нет там - вычитываем из файла*/
if(!lFileData) {
Fs.readFile(CloudServer.INDEX,
@ -603,7 +594,7 @@
lIndexName = lSrv.INDEX;
/* и сохраняем в кэш */
lSrv.Cache.set(lIndexName, pIndex);
Cache.set(lIndexName, pIndex);
pIndex = pIndex.toString();
@ -710,10 +701,10 @@
* если установлена работа с кэшем
* сохраняем сжатые данные
*/
if(CloudServer.Cache.isAllowed){
if(Cache.isAllowed){
/* устанавливаем кєш */
Util.log(pName+' gziped');
CloudServer.Cache.set(pName+'_gzip', pResult);
Cache.set(pName+'_gzip', pResult);
}
CloudServer.sendResponse(pHeader, pResult, pName);
}
@ -750,9 +741,12 @@
* @param pConfig
* @param pProcessing {index, appcache, rest}
*/
exports.start = function(pConfig, pProcessing){
exports.start = function(pConfig, pProcessing){
CloudServer.start(pConfig, pProcessing);
};
exports.CloudServer = CloudServer;
})();
exports.CloudServer = CloudServer;
exports.Minify = Minify;
exports.AppCache = AppCache;
})();

View file

@ -185,28 +185,34 @@
*/
function sendFile(pParams){
var lRet,
lName, lReq, lRes;
if(pParams){
lName = pParams.name,
lReq = pParams.request,
lRes = pParams.response,
lEnc = lReq.headers['accept-encoding'] || '',
lGzip = lEnc.match(/\bgzip\b/),
lRes = pParams.response;
}
if(lName && lRes && lReq){
var lEnc = lReq.headers['accept-encoding'] || '',
lGzip = lEnc.match(/\bgzip\b/),
lReadStream = fs.createReadStream(lName, {
'bufferSize': 4 * 1024
});
lReadStream = fs.createReadStream(lName, {
'bufferSize': 4 * 1024
});
lReadStream.on('error', function(pError){
lRes.writeHead(ERROR, 'OK');
lRes.end(String(pError));
});
lRes.writeHead(OK, generateHeaders(lName, lGzip) );
if (lGzip)
lReadStream = lReadStream.pipe( zlib.createGzip() );
lReadStream.pipe(lRes);
lReadStream.on('error', function(pError){
lRes.writeHead(ERROR, 'OK');
lRes.end(String(pError));
});
lRes.writeHead(OK, generateHeaders(lName, lGzip) );
if (lGzip)
lReadStream = lReadStream.pipe( zlib.createGzip() );
lReadStream.pipe(lRes);
}
return lRet;
}

View file

@ -54,22 +54,11 @@
if(this._allowed.css || this._allowed.js || this._allowed.html){
main.srvrequire("ischanged").isFileChanged(pName, false, function(pChanged){
if(pChanged){
if(pChanged)
Minify.optimize(pName, pParams);
}else{
var lDot = pName.lastIndexOf('.'),
lExt = pName.substr(lDot);
pName = Minify.MinFolder + main.crypto.createHash('sha1')
.update(pName)
.digest('hex') + lExt;
console.log(pName);
pParams.callback = null;
pParams.name = pName;
if(pParams.request && pParams.response)
main.sendFile(pParams);
else{
pParams.name = Minify.getName(pName);
main.sendFile(pParams);
}
});
@ -92,6 +81,8 @@
},
/* minification folder name */
MinFolder : ''
MinFolder : '',
getName : Minify ? Minify.getName : Util.retFalse
};
})();