(function(){
'use strict';
var DIR = __dirname + '/',
main = require(DIR + 'lib/server/main'),
LIBDIR = main.LIBDIR,
SRVDIR = main.SRVDIR,
CLIENTDIR = LIBDIR + 'client',
HTMLDIR = main.HTMLDIR,
JSONDIR = main.JSONDIR,
path = main.path,
fs = main.fs,
CloudFunc = main.cloudfunc,
AppCache = main.appcache,
Util = main.util,
update = main.update,
server = main.librequire('server'),
Minify = main.minify,
Config = main.config,
INDEX = HTMLDIR + 'index.html',
CONFIG_PATH = JSONDIR + 'config.json',
CA = DIR + 'ssl/sub.class1.server.ca.pem',
KEY = DIR + 'ssl/ssl.key',
CERT = DIR + 'ssl/ssl.crt',
FILE_TMPL = HTMLDIR + 'file.html',
PATH_TMPL = HTMLDIR + 'path.html',
FileTemplate, PathTemplate,
FS = CloudFunc.FS;
/* reinit main dir os if we on Win32 should be backslashes */
DIR = main.DIR;
readConfig(init);
/**
* additional processing of index file
*/
function indexProcessing(pData){
var lPath, lReplace, lKeysPanel,
lData = pData.data,
lAdditional = pData.additional;
/*
* если выбрана опция минимизировать скрипты
* меняем в index.html обычные css на
* минифицированый
*/
if (Minify.allowed.css){
lPath = '/' + Util.removeStr(Minify.MinFolder, DIR);
lReplace = '';
lData = Util.removeStr(lData, lReplace)
.replace('/css/style.css', lPath + 'all.min.css');
}
lData = Util.render(lData, {
title : CloudFunc.getTitle(),
fm : lAdditional
});
if (!Config.appcache)
lData = Util.removeStr(lData, [
/* min */
' manifest=/cloudcmd.appcache',
/* normal */
' manifest="/cloudcmd.appcache"'
]);
if (!Config.show_keys_panel){
lKeysPanel = '
' + p.name);
pParams.name = main.HTMLDIR + p.name + '.html';
lRet = main.sendFile( pParams );
}
else if ( Util.isContainStrAtBegin(p.name, FS) || Util.strCmp( p.name, '/') )
lRet = sendCommanderContent( pParams );
else
lRet = false;
}
return lRet;
}
function sendCommanderContent(pParams){
var p, lRet = main.checkParams(pParams);
if (lRet){
p = pParams;
p.name = Util.removeStrOneTime(p.name, CloudFunc.FS) || main.SLASH;
fs.stat(p.name, function(pError, pStat){
if (!pError)
if ( pStat.isDirectory() )
processCommanderContent(pParams);
else
main.sendFile( pParams );
else
main.sendError(pParams, pError);
});
}
return lRet;
}
function processCommanderContent(pParams){
var lRet = main.checkParams(pParams);
if (lRet){
var p = pParams;
main.commander.getDirContent(p.name, function(pError, pJSON){
if (!pError){
var lQuery = main.getQuery(p.request);
if ( Util.isContainStr(lQuery, 'json') ){
p.data = Util.stringifyJSON(pJSON);
p.name +='.json';
main.sendResponse(p);
}
else{ /* get back html*/
p.name = Minify.allowed.html ? Minify.getName(INDEX) : INDEX;
fs.readFile(p.name, function(pError, pData){
if (!pError){
var lPanel = CloudFunc.buildFromJSON(pJSON, FileTemplate, PathTemplate),
lList = '
' +
'
';
main.sendResponse(p, indexProcessing({
additional : lList,
data : pData.toString(),
}));
}
else
main.sendError(pParams, pError);
});
}
}
else
main.sendError(pParams, pError);
});
}
}
/* 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;
}
})();