X-Git-Url: http://git.pjr.cc/?p=nodejsws.git;a=blobdiff_plain;f=lib%2Fwsrequest.js;fp=lib%2Fwsrequest.js;h=c0bf4d10d5ef0f8d09e8af1c757e72c8daf91217;hp=80e4d3c4f442bc7fa8656b745e8533e08ceac024;hb=6beaec1f162befb123cc29c60566cd15da515750;hpb=f3610858364946faf944c86362b40b2644e4050a diff --git a/lib/wsrequest.js b/lib/wsrequest.js index 80e4d3c..c0bf4d1 100644 --- a/lib/wsrequest.js +++ b/lib/wsrequest.js @@ -99,6 +99,9 @@ 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/"; + + console.log("main purl path is ", mainPath); if(purl.pathname == "/") { if(defaultMainPurl == null) { @@ -108,14 +111,28 @@ function urlServicer(request, response, purl) { // find and load the purl } - if(typeof purlClass.layout == "undefined") { - console.log("set via undefined"); - purlLayout = layouts.standard(); - } else { - // find and resolve the layout - } } 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 +160,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 +197,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("

Undefined layout section

"); + response.write(""); callback(); } //callback();