Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

V037

  • Loading branch information...
commit f75933bc93eba719ed9b1090de277522317a6eb1 1 parent efedcf3
@mrohad authored
Showing with 48 additions and 47 deletions.
  1. +8 −3 README.markdown
  2. +37 −44 lib/engine.js
  3. +3 −0  lib/log.js
View
11 README.markdown
@@ -189,7 +189,7 @@ Example settings file:
{
"web_app_name" : "Alligator TestApp",
- "port" : 82,
+ "port" : 8080,
"path" : {
"root" : "/home/vadmin/ws/Alligator/WWW/",
"lib" : "/home/vadmin/ws/Alligator/WWWlib/"
@@ -208,7 +208,12 @@ Example settings file:
"port" : 11211
}
},
- "debug_mode" : 1,
+ "logs" :{
+ "Engine" :"DEBUG",
+ "ASemaphore" :"ERROR",
+ "Session" :"ERROR",
+ "Application" :"ERROR"
+ },
"nodes" : 1
}
@@ -230,7 +235,7 @@ Explanation of properties:
- `server_script.end` - the end tag for server-side scripting (default = '?>')
- `server_script.session_minutes` - session timeout after X minutes (default = 30)
- `server_script.memcached` - integration with memcached
-- `debug_mode` - 1 for debug mode, 0 for non-debug mode, on debug mode we add the exception to the response + the log level = debug
+- `logs` - the log level for each module
- `nodes` - number of process running this application server ** currently we support shared memory using memcached! we recommand to set the number of nodes as the number of cores
View
81 lib/engine.js
@@ -12,18 +12,15 @@ sessionManager = require('./session'),
incomingForm = require('./incoming_form').incomingForm,
multi = require('./multinode/multi-node'),
asemaphore = require('./asemaphore'),
-applicationManager = require('./application');
+applicationManager = require('./application');
+var moduleName = "Engine";
var cachedJssp = [];
var pageObjCache = new Object();
pageObjCache.get = function(key){
var val = pageObjCache[key];
- //log.debug("Engine","===CACHE-GET by key - "+key);
- //if(val != undefined)
- // log.debug("Engine","val.page - "+val.page.toString());
return val;
};
pageObjCache.set = function(key,val){
- //log.debug("Engine","===CACHE-SET by key - "+key + " val.page - " + val.page.toString());
pageObjCache[key] = val;
};
@@ -37,22 +34,17 @@ exports.start = function(settings) {
var logObj = globalSettings.logs;
for (property in logObj)
log.setLevel(property,logObj[property]);
-// if(globalSettings.debug_mode)
-// log.level = log.levels.DEBUG;
-// else
-// log.level = log.levels.WARN;
- log.info("Engine","Starting Web App - " +globalSettings.web_app_name);
+ log.info(moduleName,"Starting Web App - " +globalSettings.web_app_name);
//setting up sessionScope and applicationScope:
applicationScope = applicationManager.start(globalSettings.web_app_name,globalSettings.server_script.memcached);
sessionScope = sessionManager.start(globalSettings.server_script.session_minutes,globalSettings.web_app_name);
-
//Load utils dynamicly
if(globalSettings.path.lib != undefined)
fs.stat(globalSettings.path.lib, function (err, stats) {
if (err)
- log.error("Engine","Loading lib error:"+err);
+ log.error(moduleName,"Loading lib error:"+err);
else if(!stats.isDirectory())
- log.error("Engine","Loading lib error: path must be a directory");
+ log.error(moduleName,"Loading lib error: path must be a directory");
else{
var files = fs.readdirSync(globalSettings.path.lib);
for(i=0;i<files.length;i++){
@@ -61,14 +53,14 @@ exports.start = function(settings) {
var fileNoJs = file.substring(0,files[i].length-3);
var path = pathlib.join(globalSettings.path.lib,fileNoJs);
eval("lib."+fileNoJs+" = require(path)");
- log.info("Engine","Loading lib file:" + path);
+ log.info(moduleName,"Loading lib file:" + path);
}
}
}
});
//END util loading
var server = http.createServer(function (req, res) {
- log.debug("Engine","request arrived!");
+ log.debug(moduleName,"request arrived!");
var url = uri.parse(req.url,true);
var pathname = (url.pathname || '/');
var cleanPathname = pathname
@@ -83,7 +75,7 @@ exports.start = function(settings) {
}
else if (req.method == "POST"){
incomingForm.parse(req, function(err, fields, files) {
- //log.debug("Engine","POST fields:" + utils.arrayToString(fields));
+ //log.debug(moduleName,"POST fields:" + utils.arrayToString(fields));
params = new Object();
req.parameters = fields;
handleRequest(req,res,cleanPathname);
@@ -99,17 +91,17 @@ exports.start = function(settings) {
nodes: globalSettings.nodes
}, server);
- log.warn("Engine",'Server running at port '+globalSettings.port);
+ log.warn(moduleName,'Server running at port '+globalSettings.port);
};
function handleRequest(req,res,cleanPathname,newSessionId){
var root = globalSettings.path.root;
var path = pathlib.join(root, cleanPathname);
if(newSessionId==undefined)
- log.info("Engine","Handling request to: " +path + " pid("+process.pid+")");
+ log.info(moduleName,"Handling request to: " +path + " pid("+process.pid+")");
else
- log.info("Engine","Forwarding request to: " +path + " pid("+process.pid+")");
- //log.debug("Engine","Request headers: "+utils.arrayToString(req.headers));
+ log.info(moduleName,"Forwarding request to: " +path + " pid("+process.pid+")");
+ //log.debug(moduleName,"Request headers: "+utils.arrayToString(req.headers));
fs.stat(path, function (err, stats) {
if (err) {
// ENOENT is normal on 'file not found'
@@ -132,12 +124,12 @@ function handleRequest(req,res,cleanPathname,newSessionId){
sessionScope.hello(sessionId);
}
if(!(path.endsWith("."+globalSettings.server_script.template_ext) || path.endsWith("."+globalSettings.server_script.script_ext))){
- log.info("Engine","Static request");
+ log.info(moduleName,"Static request");
sendHeaders(req, res,undefined, stats.size, mime.mimeType(path), stats.mtime);
var readStream = fs.createReadStream(path);
sys.pump(readStream,res);
}else{
- log.info("Engine","Dyanmic request");
+ log.info(moduleName,"Dyanmic request");
if(path.endsWith("."+globalSettings.server_script.template_ext) && (cachedJssp[path] == undefined || cachedJssp[path+"#date"] != stats.mtime.toUTCString())){
var readStream = fs.createReadStream(path);
var script = [];
@@ -145,14 +137,14 @@ function handleRequest(req,res,cleanPathname,newSessionId){
script.push(chunk.toString());
});
readStream.addListener("end", function () {
- log.info("Engine","STARTING PROCESSING JSSP");
+ log.info(moduleName,"STARTING PROCESSING JSSP");
SSJSWriter(script.join(""),req,res,path,stats.mtime,sessionId);
- log.info("Engine","END OF JSSP PROCESSING");
+ log.info(moduleName,"END OF JSSP PROCESSING");
});
req.connection.addListener('timeout', function() {
/* dont destroy it when the fd's already closed */
if (readStream.readable) {
- log.debug("Engine",'timed out. destroying file read stream');
+ log.debug(moduleName,'timed out. destroying file read stream');
readStream.destroy();
}
});
@@ -161,7 +153,7 @@ function handleRequest(req,res,cleanPathname,newSessionId){
readStream.destroy();
});
readStream.addListener('fd', function(fd) {
- log.debug("Engine","opened",path,"on fd",fd);
+ log.debug(moduleName,"opened",path,"on fd",fd);
});
readStream.addListener('error', function (err) {
@@ -171,7 +163,7 @@ function handleRequest(req,res,cleanPathname,newSessionId){
}
else{
- log.info("Engine","RUN JSSP FROM CACHE");
+ log.info(moduleName,"RUN JSSP FROM CACHE");
var currentFileNameToRead = cachedJssp[path];
if(path.endsWith("."+globalSettings.server_script.script_ext))
currentFileNameToRead = path.substring(0,path.length-globalSettings.server_script.script_ext.length-1);;
@@ -195,7 +187,7 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId,i
responseHead.status = 200;
responseHead.headers = {};
var afterEval = [];
- //log.debug("Engine",asemaphore);
+ //log.debug(moduleName,asemaphore);
var currentAsemaphore = asemaphore.ctor(1,function(){
if(!error)
result.html = afterEval.join("");
@@ -224,7 +216,7 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId,i
lib:lib,
log:log,
write : function(text){
- //log.debug("Engine","WRITE afterEval : "+ afterEval);
+ //log.debug(moduleName,"WRITE afterEval : "+ afterEval);
afterEval.push(text);
},
writeEscapedText :function(text){
@@ -240,17 +232,17 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId,i
}
};
//=============================================
- //log.debug("Engine",typeof functoRun.page);
- log.debug("Engine","READING pageObject from cache, key - "+file );
+ //log.debug(moduleName,typeof functoRun.page);
+ log.debug(moduleName,"READING pageObject from cache, key - "+file );
var pageObjFromCache = pageObjCache.get(file);
if(pageObjFromCache == undefined || isForceEval){//not in cache
- log.debug("Engine","....and....cache is EMPTY for key - "+file);
+ log.debug(moduleName,"....and....cache is EMPTY for key - "+file);
var pageObjFunc = new Function(functoRun.toString() + " ; return this;");
pageObjFromCache = new pageObjFunc(context);
pageObjCache.set(file,pageObjFromCache);
}
else
- log.debug("Engine","....and....cache is FULL for key - "+file);
+ log.debug(moduleName,"....and....cache is FULL for key - "+file);
if(request.parameters.serverFunction == undefined){
pageObjFromCache.page(context);
}else{//Server command
@@ -258,14 +250,15 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId,i
pageObjFromCache[serverFunction](context);
}
currentAsemaphore.p();
- //log.debug("Engine","sessionId - "+result.sessionId);
- }catch(err){
+ //log.debug(moduleName,"sessionId - "+result.sessionId);
+ }
+ catch(err){
log.error("parse problem:"+err+" stack:"+err.stack);
flushResponse = true;
error = true;
responseHead.status = 500;
result.html = ["<h1>",globalSettings.web_app_name," - SERVER ERROR</h1>."].join("");
- if(globalSettings.debug_mode){
+ if(log.getLevel(moduleName)==0){
result.html += "(Debug Mode) Could not parse jssp<br/> ";
var erStr = "Details: ";
result.html += erStr+err.stack;
@@ -277,11 +270,11 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId,i
};
if(cachedJssp[file]!= undefined && !isForceEval){//funcCaching
- log.debug("Engine","SSJS RUN: bring file from CACHE: "+file);
+ log.debug(moduleName,"SSJS RUN: bring file from CACHE: "+file);
executeTheSJSFile(undefined,undefined);
}else{
var fileNameToRead = [newfileName,".",globalSettings.server_script.script_ext].join("");
- log.debug("Engine","SSJS RUN: reading file from FS: "+fileNameToRead);
+ log.debug(moduleName,"SSJS RUN: reading file from FS: "+fileNameToRead);
fs.readFile(fileNameToRead,executeTheSJSFile);
}
}
@@ -351,17 +344,17 @@ function SSJSWriter(str,request,response,file,lastMod,sessionId){
var finalFunction = ["this.page = function (context){",parsedTextArray.join(""),"};",externalParsedTextArray.join("")].join("");
var newfileName = file.substring(0,file.length-globalSettings.server_script.template_ext.length-1);
fs.writeFile([newfileName,".",globalSettings.server_script.script_ext].join(""), finalFunction, function (err) {
- //log.debug("Engine","Error writin cache file: "+err);
+ //log.debug(moduleName,"Error writin cache file: "+err);
cachedJssp[file] = newfileName;
cachedJssp[file + "#date"] = lastMod.toUTCString();
- log.info("Engine","Caching - "+file + ",last mod - "+ lastMod.toUTCString());
+ log.info(moduleName,"Caching - "+file + ",last mod - "+ lastMod.toUTCString());
serverSideRunning(newfileName,request,response,file,lastMod,sessionId,true);
});
}
function sendHeaders(req, res,responseHead, length, content_type, modified_time,sessionId) {
- //log.debug("Engine","send headers: sessionId - "+sessionId);
+ //log.debug(moduleName,"send headers: sessionId - "+sessionId);
if(responseHead==undefined)
responseHead = new Object();
if(responseHead.status==undefined)
@@ -381,13 +374,13 @@ function sendHeaders(req, res,responseHead, length, content_type, modified_time,
responseHead.headers["Content-Type"] = content_type || "application/octet-stream";
if (modified_time)
responseHead.headers["Last-Modified"] = modified_time.toUTCString();
- //log.debug("Engine","RESPONSE Headers :"+utils.arrayToString(responseHead.headers)+" +++ RESPONSE Status :"+responseHead.status);
+ //log.debug(moduleName,"RESPONSE Headers :"+utils.arrayToString(responseHead.headers)+" +++ RESPONSE Status :"+responseHead.status);
res.writeHead(responseHead.status, responseHead.headers);
- log.info("Engine",req.connection.remoteAddress,req.method,responseHead.status,length);
+ log.info(moduleName,req.connection.remoteAddress,req.method,responseHead.status,length);
}
function fileNotFound(req,res,path) {
- log.debug("Engine","404 opening path: '"+path+"'");
+ log.debug(moduleName,"404 opening path: '"+path+"'");
var body = "404: " + req.url + " not found.\n";
var responseHead= new Object();
responseHead.status = 404;
View
3  lib/log.js
@@ -21,6 +21,9 @@ exports.setLevel = function(sector,level){
sectors[sector] = levels[level];
log("DEBUG",sector,"1SET LOG level to - "+level );
};
+exports.getLevel = function(sector){
+ return sectors[sector];
+};
exports.debug = function(sector) {
log("DEBUG", sector, arguments);
Please sign in to comment.
Something went wrong with that request. Please try again.