added a method for setting resource path
[nodejsws.git] / lib / wsrequest.js
index 80e4d3c..6f7cb52 100644 (file)
@@ -99,23 +99,64 @@ 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 = "";
+       
+       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;
-               } else {
-                       // find and load the purl
-               }
-               
-               if(typeof purlClass.layout == "undefined") {
-                       console.log("set via undefined");
-                       purlLayout = layouts.standard();
+
+                       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 resolve the layout
+                       // 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";
+               console.log("attempting to require: ", newPurlPath);
+               try {
+                       var newPurlClass = require(newPurlPath);
+                       purlClass = newPurlClass;
+               } catch(err) {
+                       console.log("tried to load '%s' for request '%s', but this has failed, returning 302 to /", newPurlPath, purl.pathname);
+                       response.writeHead("302", { 'Location': '/' });
+                       response.end();
+                       return;
+               }
+       }
+
+       if(typeof purlClass.layout == "undefined") {
+               console.log("set via undefined");
+               purlLayout = layouts.standard();
+       } else {
+               // find and resolve the layout
+               purlLayout = purlClass.layout();
        }
        
        // now we should have a layout and a class
@@ -143,7 +184,7 @@ function serviceLayout(request, response, purlClass, purlLayout) {
                console.log("begin process layout");
                
                if(offset >= splitup.length) {
-                       response.write("end of caller");
+                       //response.write("end of caller");
                        response.end();
                        return;
                } else if(offset == 0) {
@@ -180,12 +221,14 @@ function serviceLayout(request, response, purlClass, purlLayout) {
 
 function resolveAndCall(request, response, caller, purlClass, callback) {
        //response.write("resolving: \"" + caller + "\"");
+       
+       // TODO: do this properly.
        if(typeof purlClass[caller] != "undefined") {
                purlClass[caller](request, response, callback);
-       } else if(typeof purlClass[caller] != "undefined"){
+       } else if(typeof layouts[caller] != "undefined"){
                layouts[caller](request, response, callback);
        } else {
-               response.write("<hr><br>Undefined layout section<br><hr>");
+               response.write("<!-- ERROR:Undefined layout section -->");
                callback();
        }
        //callback();
@@ -193,7 +236,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) {