var url = require("url");
var path = require("path");
var fs = require("fs");
-var myparse = require("./myparse.js");
-var webmain = require("./purls/main.js");
+//var myparse = require("./myparse.js");
+var webmain = require("./webmain.js");
+var layouts = require("./layouts.js");
-var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
+// global stuff
+var extraCss = new Array();
+var extraJs = new Array();
+//var defaultLayout = null;
+var defaultMainPurl = null;
+
+//var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
function wsRequest(request, response) {
var isStatic = 0;
var purl = url.parse(request.url);
- staticExtensions.forEach(function testExtn(setest) {
+ /*staticExtensions.forEach(function testExtn(setest) {
console.log("testing url: ", request.url);
console.log("against: ", purl.pathname);
var chk = purl.pathname.split(".");
if(chkid != -1) {
isStatic = 1;
}
- });
+ });*/
+
+ // if the end of the pathname is something.something, we assume static
+ var lpath = purl.pathname.split("/").pop();
+ var idx = lpath.indexOf(".");
+ console.log("testing url: ", request.url);
+ console.log("against: ", purl.pathname);
+ console.log("lpath is: '%s'", lpath);
+ console.log("type is: '%s'", typeof lpath);
+ console.log("idx is: ", idx);
+
+ if(idx > 0) isStatic = 1;
if(isStatic == 1) {
// do the static
- var servethis = purl.pathname.split("/").pop();
- serveStatic(servethis, response);
+ console.log("Service as static");
+ serveStatic(lpath, response);
return;
}
// now we need to find the extension used
// to serve the request based no the first purl
+
+
+ /*
+ *
if(purl.pathname == "/") {
console.log("Serv main");
webmain.serveMain(request, response, function (request, response) {
}
var thispurl = purl.pathname.split("/")[1];
- fs.stat("./lib/purls/web_"+thispurl+".js", function (err, stats) {
+ fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
if(err) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end();
return;
}
- var thiserv = require("./purls/web_"+thispurl+".js");
+ var thiserv = require("../purls/web_"+thispurl+".js");
if(thiserv.requireBody()) {
console.log("yubber is true");
webmain.serveBody(request, response, thiserv.process);
}
return;
});
+ */
+
+ urlServicer(request, response, purl);
+
+
+ return;
+}
+
+function urlServicer(request, response, purl) {
+ console.log("url servicer called for ", 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");
+
+ 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, 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
+ if(typeof purlClass.preResponse != "undefined") {
+ purlClass.preResponse(request, response, function() {
+ serviceLayout(request, response, purlClass, purlLayout);
+ });
+ } else {
+ serviceLayout(request, response, purlClass, purlLayout);
+ }
return;
}
+function serviceLayout(request, response, purlClass, purlLayout) {
+ var reallay = "start:"+purlLayout+":end";
+ var splitup = reallay.split("<?njs");
+ var offset=0;
+
+ console.log("inservicer: ", purlLayout);
+
+ function processLayout() {
+ var output = "";
+
+ console.log("begin process layout");
+
+ if(offset >= splitup.length) {
+ //response.write("end of caller");
+ response.end();
+ return;
+ } else if(offset == 0) {
+ console.log("write for offset 0");
+ output = splitup[0].replace(/^start:/g, "");
+ if(splitup.length == 1) output = output.replace(/:end$/g, "");
+ console.log("did write: ", output);
+ response.write(output);
+ offset++;
+ processLayout();
+ } else {
+ var thispart = splitup[offset].split("?>");
+ var caller = thispart[0].trim();
+
+ console.log("in parts for bits: ", thispart);
+ console.log("and caller is: ", caller);
+
+ // here we resolve and call the caller....
+ //response.write("calling: " + caller);
+ resolveAndCall(request, response, caller, purlClass, function () {
+ output = thispart[1].replace(/:end$/g, "");
+ response.write(output);
+ offset++;
+ processLayout();
+ });
+ }
+ }
+
+ processLayout();
+
+ //response.write("servicer");
+ //response.end();
+}
+
+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 layouts[caller] != "undefined"){
+ layouts[caller](request, response, callback);
+ } else {
+ response.write("<!-- ERROR:Undefined layout section -->");
+ callback();
+ }
+ //callback();
+ return;
+}
+
function serveStatic(staticname, response) {
var pathName = "./res/"+staticname;
console.log("Pathname for check is ", pathName);