add eclipse project
[nodejsws.git] / lib / wsrequest.js
index c0bf4d1..17547e7 100644 (file)
@@ -99,21 +99,49 @@ function urlServicer(request, response, purl) {
        // first resolve the module making the call and determine layout/purl
        var purlClass = layouts;
        var purlLayout = layouts.standard();
-       var mainPath = path.dirname(require.main.filename) + "/purls/";
+       var mainPath = "";
+       
+       if(typeof global.njspurls.mainPath != "undefined") {
+               mainPath = global.njspurls.mainPath;
+               console.log("gettingmainpath from global: ", mainPath);
+       } else {
+               mainPath = path.dirname(require.main.filename) + "/purls/";
+       }
 
        console.log("main purl path is ", mainPath);
        
        if(purl.pathname == "/") {
                if(defaultMainPurl == null) {
                        console.log("set purl class to layouts");
-                       purlClass = layouts;
+
+                       var mainPurlClassPath = mainPath+"/main.js";
+                       console.log("attempting to load main.js as the main purl class");
+                       try {
+                               var mainPurlClass = require(mainPurlClassPath);
+                               purlClass = mainPurlClass;
+                               console.log("main.js exists");
+                       } catch(err) {
+                               console.log("main.js doesnt exist, using default");
+                       }
                } else {
-                       // find and load the purl
+                       // find and load the purl, we'll use main.js
+                       var mainPurlClassPath = mainPath+"/"+defaultMainPurl+".js";
+                       
+                       console.log("attempting to load main.js as the main purl class");
+                       try {
+                               var mainPurlClass = require(mainPurlClassPath);
+                               purlClass = mainPurlClass;
+                               console.log("main.js exists");
+                       } catch(err) {
+                               console.log("main.js doesnt exist, using default");
+                       }
                }
-               
        } else {
-               // handle the purls
-               var newPurlPath = mainPath+"/"+path.basename(purl.pathname.replace(/\/$/, ""))+".js";
+               // handle the purls... we taje the pathname, drop the beginning "/", then drop every after the next "/"
+               // so we end up with a purl of "abcd" from a url:
+               // http://host/abcd/qewr/asdf/asdf/asdf/asdf/asdf/
+               // and will load mainPath/abcd.js
+               var newPurlPath = mainPath+"/"+path.basename(purl.pathname.replace(/^\//, "").replace(/\/.*$/, ""))+".js";
                console.log("attempting to require: ", newPurlPath);
                try {
                        var newPurlClass = require(newPurlPath);
@@ -124,15 +152,14 @@ function urlServicer(request, response, purl) {
                        response.end();
                        return;
                }
-
        }
 
        if(typeof purlClass.layout == "undefined") {
                console.log("set via undefined");
-               purlLayout = layouts.standard();
+               purlLayout = layouts.standard(request, response);
        } else {
                // find and resolve the layout
-               purlLayout = purlClass.layout();
+               purlLayout = purlClass.layout(request, response);
        }
        
        // now we should have a layout and a class
@@ -212,7 +239,12 @@ function resolveAndCall(request, response, caller, purlClass, callback) {
 }
 
 function serveStatic(staticname, response) {
-       var pathName = "./res/"+staticname;
+       var pathName = "";
+       if(typeof global.njspurls.mainResPath != "undefined") {
+               pathName = global.njspurls.mainResPath + "/" + staticname;
+       } else {
+               pathName = "./res/"+staticname;
+       }
        console.log("Pathname for check is ", pathName);
        fs.exists(pathName, function(exists) {
                if(!exists) {