From f3610858364946faf944c86362b40b2644e4050a Mon Sep 17 00:00:00 2001 From: Paul J R Date: Sun, 30 Dec 2012 07:54:58 +1100 Subject: [PATCH] semi-functional --- lib/app.js | 45 ++++++++++++++++----- lib/layouts.js | 51 +++++++++++++++++++++-- lib/myparse.js | 21 ---------- lib/wsrequest.js | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++- myapp.js | 7 +++ 5 files changed, 202 insertions(+), 37 deletions(-) delete mode 100644 lib/myparse.js create mode 100644 myapp.js diff --git a/lib/app.js b/lib/app.js index 07718dc..0f4348d 100644 --- a/lib/app.js +++ b/lib/app.js @@ -6,7 +6,9 @@ var http = require("http"), req = require("./wsrequest.js"), localPort = 8888, defaultLayout = "standard", - defaultMain = null; + defaultMain = null, + addCss = null, + addJs = null; @@ -16,21 +18,44 @@ exports.setPort = function(port) { localPort = port; } -exports.start = function() { - // initialise req - req.setDefaultLayout(defaultLayout); - req.setDefaultMain(defaultMain); - +exports.addCss = function(cssname) { + if(addCss == null) { + addCss = new Array(); + // turn it into an array; + addCss.push(cssname); + } else { + addCss.push(cssname); + } - http.createServer(req.wsRequest).listen(localPort); +} - console.log("Server running on port: ", localPort); +exports.addJS = function(js) { + if(addJs == null) { + addJs = new Array(); + addJs.push(js); + } else { + addJs.push(js); + } } exports.setDefaultLayout = function(layout) { defaultLayout = layout; } -exports.setDefaultMain = function(main) { +exports.setDefaultMainPurl = function(main) { defaultMain = main; -} \ No newline at end of file +} + + +exports.start = function() { + // initialise req + //req.setDefaultLayout(defaultLayout); + //req.setDefaultMain(defaultMain); + //req.setExtraCss(addCss); + //req.setExtraJs(assJs); + + + http.createServer(req.wsRequest).listen(localPort); + + console.log("Server running on port: ", localPort); +} diff --git a/lib/layouts.js b/lib/layouts.js index a6c0a81..2679987 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -1,8 +1,8 @@ exports.standard = function () { - var lay = ""; + var lay = ""; lay += ""; - lay += ""; - lay += "
"; + lay += ""; + lay += ""; return lay; } @@ -12,7 +12,48 @@ exports.straight = function () { var lay = ""; lay += ""; lay += ""; - lay += "
"; + lay += ""; return lay; -} \ No newline at end of file +} + +exports.header = function(request, response, callback) { + // this function is the default header builder. + response.write("mainname"); + callback(request, response); +} + +exports.title = function(request, response, callback) { + // this is the default title builder + response.write("

mainname

"); + callback(request, response); +} + +exports.menu = function(request, response, callback) { + // this is the default title builder + response.write("
Menuitem 1item2
"); + callback(request, response); +} + +exports.sidebar = function(request, response, callback) { + // this is the default title builder + response.write("Sidebar!"); + callback(request, response); +} + +exports.body = function(request, response, callback) { + // this is the default title builder + response.write("Body!"); + callback(request, response); +} + +exports.footer = function(request, response, callback) { + // this is the default title builder + response.write("

Copyright PJR.cc"); + callback(request, response); +} + +exports.preResponse = function(request, response, callback) { + console.log("in preresponse - doing nothing"); + callback(); +} diff --git a/lib/myparse.js b/lib/myparse.js deleted file mode 100644 index 088dd7a..0000000 --- a/lib/myparse.js +++ /dev/null @@ -1,21 +0,0 @@ -var path = require("fs"); - -function useBody() { - return; -} - -function parse(base, purl, response, servicer) { - var pathCheck = "./lib/web_"+base+".js"; - console.log("path check on ", pathCheck); - fs.exists(pathCheck, function (exists) { - if(!exists) console.log("not exists: ", base); - else { - servicer(purl, response, parse); - console.log("base exists: ", base); - } - }); -} - -exports.useBody = useBody; -exports.parse = parse; -exports.process = process; diff --git a/lib/wsrequest.js b/lib/wsrequest.js index a7ad086..80e4d3c 100644 --- a/lib/wsrequest.js +++ b/lib/wsrequest.js @@ -1,8 +1,15 @@ var url = require("url"); var path = require("path"); var fs = require("fs"); -var myparse = require("./myparse.js"); +//var myparse = require("./myparse.js"); var webmain = require("./webmain.js"); +var layouts = require("./layouts.js"); + +// global stuff +var extraCss = new Array(); +var extraJs = new Array(); +//var defaultLayout = null; +var defaultMainPurl = null; //var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"]; @@ -45,6 +52,10 @@ function wsRequest(request, response) { // 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) { @@ -74,8 +85,110 @@ function wsRequest(request, response) { } 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(); + + 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(); + } else { + // find and resolve the layout + } + } else { + // handle the purls + } + + // 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("= 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 + "\""); + if(typeof purlClass[caller] != "undefined") { + purlClass[caller](request, response, callback); + } else if(typeof purlClass[caller] != "undefined"){ + layouts[caller](request, response, callback); + } else { + response.write("

Undefined layout section

"); + callback(); + } + //callback(); return; } diff --git a/myapp.js b/myapp.js new file mode 100644 index 0000000..dfc44a0 --- /dev/null +++ b/myapp.js @@ -0,0 +1,7 @@ +var app = require("./lib/app.js"); + +app.setPort(8888); +//app.setDefaultLayout("standard"); +app.setDefaultMainPurl("main.js"); + +app.start(); -- 1.7.0.4