mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-28 10:14:13 +00:00
refactored
This commit is contained in:
parent
7673b7d32d
commit
5c6fcfddcc
4 changed files with 73 additions and 102 deletions
122
lib/server.js
122
lib/server.js
|
|
@ -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;
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue