1 var url = require("url");
2 var path = require("path");
3 var fs = require("fs");
4 //var myparse = require("./myparse.js");
5 var webmain = require("./webmain.js");
6 var layouts = require("./layouts.js");
9 var extraCss = new Array();
10 var extraJs = new Array();
11 //var defaultLayout = null;
12 var defaultMainPurl = null;
14 //var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
16 function wsRequest(request, response) {
18 var purl = url.parse(request.url);
20 /*staticExtensions.forEach(function testExtn(setest) {
21 console.log("testing url: ", request.url);
22 console.log("against: ", purl.pathname);
23 var chk = purl.pathname.split(".");
24 console.log("chk: ", chk);
25 var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase());
26 console.log("chkid is ", chkid);
32 // if the end of the pathname is something.something, we assume static
33 var lpath = purl.pathname.split("/").pop();
34 var idx = lpath.indexOf(".");
35 console.log("testing url: ", request.url);
36 console.log("against: ", purl.pathname);
37 console.log("lpath is: '%s'", lpath);
38 console.log("type is: '%s'", typeof lpath);
39 console.log("idx is: ", idx);
41 if(idx > 0) isStatic = 1;
45 console.log("Service as static");
46 serveStatic(lpath, response);
50 //console.log("request: ", request);
51 console.log("purl: ", purl);
53 // now we need to find the extension used
54 // to serve the request based no the first purl
59 if(purl.pathname == "/") {
60 console.log("Serv main");
61 webmain.serveMain(request, response, function (request, response) {
67 var thispurl = purl.pathname.split("/")[1];
68 fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
69 console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
71 response.writeHead(404, {"Content-Type": "text/plain"});
72 response.write("404 Not Found\n");
76 var thiserv = require("../purls/web_"+thispurl+".js");
77 if(thiserv.requireBody()) {
78 console.log("yubber is true");
79 webmain.serveBody(request, response, thiserv.process);
81 console.log("yubber is false");
82 thiserv.process(request, response, function(request, response) {
90 urlServicer(request, response, purl);
96 function urlServicer(request, response, purl) {
97 console.log("url servicer called for ", purl);
99 // first resolve the module making the call and determine layout/purl
100 var purlClass = layouts;
101 var purlLayout = layouts.standard();
104 if(typeof global.njspurls.mainPath != "undefined") {
105 mainPath = global.njspurls.mainPath;
106 console.log("gettingmainpath from global: ", mainPath);
108 mainPath = path.dirname(require.main.filename) + "/purls/";
111 console.log("main purl path is ", mainPath);
113 if(purl.pathname == "/") {
114 if(defaultMainPurl == null) {
115 console.log("set purl class to layouts");
117 var mainPurlClassPath = mainPath+"/main.js";
118 console.log("attempting to load main.js as the main purl class");
120 var mainPurlClass = require(mainPurlClassPath);
121 purlClass = mainPurlClass;
122 console.log("main.js exists");
124 console.log("main.js doesnt exist, using default");
127 // find and load the purl, we'll use main.js
128 var mainPurlClassPath = mainPath+"/"+defaultMainPurl+".js";
130 console.log("attempting to load main.js as the main purl class");
132 var mainPurlClass = require(mainPurlClassPath);
133 purlClass = mainPurlClass;
134 console.log("main.js exists");
136 console.log("main.js doesnt exist, using default");
141 var newPurlPath = mainPath+"/"+path.basename(purl.pathname.replace(/\/$/, ""))+".js";
142 console.log("attempting to require: ", newPurlPath);
144 var newPurlClass = require(newPurlPath);
145 purlClass = newPurlClass;
147 console.log("tried to load '%s' for request '%s', but this has failed, returning 302 to /", newPurlPath, purl.pathname);
148 response.writeHead("302", { 'Location': '/' });
154 if(typeof purlClass.layout == "undefined") {
155 console.log("set via undefined");
156 purlLayout = layouts.standard();
158 // find and resolve the layout
159 purlLayout = purlClass.layout();
162 // now we should have a layout and a class
163 if(typeof purlClass.preResponse != "undefined") {
164 purlClass.preResponse(request, response, function() {
165 serviceLayout(request, response, purlClass, purlLayout);
168 serviceLayout(request, response, purlClass, purlLayout);
174 function serviceLayout(request, response, purlClass, purlLayout) {
175 var reallay = "start:"+purlLayout+":end";
176 var splitup = reallay.split("<?njs");
179 console.log("inservicer: ", purlLayout);
181 function processLayout() {
184 console.log("begin process layout");
186 if(offset >= splitup.length) {
187 //response.write("end of caller");
190 } else if(offset == 0) {
191 console.log("write for offset 0");
192 output = splitup[0].replace(/^start:/g, "");
193 if(splitup.length == 1) output = output.replace(/:end$/g, "");
194 console.log("did write: ", output);
195 response.write(output);
199 var thispart = splitup[offset].split("?>");
200 var caller = thispart[0].trim();
202 console.log("in parts for bits: ", thispart);
203 console.log("and caller is: ", caller);
205 // here we resolve and call the caller....
206 //response.write("calling: " + caller);
207 resolveAndCall(request, response, caller, purlClass, function () {
208 output = thispart[1].replace(/:end$/g, "");
209 response.write(output);
218 //response.write("servicer");
222 function resolveAndCall(request, response, caller, purlClass, callback) {
223 //response.write("resolving: \"" + caller + "\"");
225 // TODO: do this properly.
226 if(typeof purlClass[caller] != "undefined") {
227 purlClass[caller](request, response, callback);
228 } else if(typeof layouts[caller] != "undefined"){
229 layouts[caller](request, response, callback);
231 response.write("<!-- ERROR:Undefined layout section -->");
238 function serveStatic(staticname, response) {
240 if(typeof global.njspurls.mainResPath != "undefined") {
241 pathName = global.njspurls.mainResPath + "/" + staticname;
243 pathName = "./res/"+staticname;
245 console.log("Pathname for check is ", pathName);
246 fs.exists(pathName, function(exists) {
248 response.writeHead(404, {"Content-Type": "text/plain"});
249 response.write("404 Not Found\n");
254 fs.readFile(pathName, "binary", function(err, file) {
256 response.writeHead(500, {"Content-Type": "text/plain"});
257 response.write(err + "\n");
262 response.writeHead(200);
263 response.write(file, "binary");
269 exports.wsRequest = wsRequest;