From b6082f4e1e2d6fdb43966123e85ea9dfd1640bdc Mon Sep 17 00:00:00 2001 From: paulr Date: Fri, 21 Dec 2012 19:43:01 +1100 Subject: [PATCH] first commit --- app.js | 10 +++++ lib/myparse.js | 21 ++++++++++ lib/purls/main.js | 31 +++++++++++++++ lib/purls/main_footer.js | 7 +++ lib/purls/main_head.js | 8 ++++ lib/purls/main_menu.js | 8 ++++ lib/wsrequest.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++ parts/footer | 5 ++ parts/header | 5 ++ parts/middle | 1 + res/favicon.ico | Bin 0 -> 3774 bytes res/test.html | 3 + 12 files changed, 196 insertions(+), 0 deletions(-) create mode 100644 app.js create mode 100644 lib/myparse.js create mode 100644 lib/purls/main.js create mode 100644 lib/purls/main_footer.js create mode 100644 lib/purls/main_head.js create mode 100644 lib/purls/main_menu.js create mode 100644 lib/wsrequest.js create mode 100644 parts/footer create mode 100644 parts/header create mode 100644 parts/middle create mode 100644 res/favicon.ico create mode 100644 res/test.html diff --git a/app.js b/app.js new file mode 100644 index 0000000..e357dab --- /dev/null +++ b/app.js @@ -0,0 +1,10 @@ +var http = require("http"), + url = require("url"), + path = require("path"), + fs = require("fs"), + req = require("./lib/wsrequest.js"), + port = process.argv[2] || 8888; + +http.createServer(req.wsRequest).listen(port); + +console.log("Server running on port: ", port); \ No newline at end of file diff --git a/lib/myparse.js b/lib/myparse.js new file mode 100644 index 0000000..088dd7a --- /dev/null +++ b/lib/myparse.js @@ -0,0 +1,21 @@ +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/purls/main.js b/lib/purls/main.js new file mode 100644 index 0000000..c5bf240 --- /dev/null +++ b/lib/purls/main.js @@ -0,0 +1,31 @@ +function serveMain(request, response, callback) { + response.writeHead(200); + + serveBody(request, response, function(request, response, callback) { + response.write("server main body"); + callback(request, response); + }); + return; +} + +function serveBody(request, response, bodyFunction) { + response.writeHead(200); + require("./main_head.js").go(request, response, function(request, response) { + response.write(""); + response.end(); + }); + }); + }); + }); + return; +} + + +exports.serveMain = serveMain; +exports.serveBody = serveBody; diff --git a/lib/purls/main_footer.js b/lib/purls/main_footer.js new file mode 100644 index 0000000..19f275e --- /dev/null +++ b/lib/purls/main_footer.js @@ -0,0 +1,7 @@ +function go(request, response, callback) { + response.write("

Copyright PJR.cc 2012"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/purls/main_head.js b/lib/purls/main_head.js new file mode 100644 index 0000000..463d8dd --- /dev/null +++ b/lib/purls/main_head.js @@ -0,0 +1,8 @@ +function go(request, response, callback) { + console.log("in main_head.go"); + response.write("im a head"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/purls/main_menu.js b/lib/purls/main_menu.js new file mode 100644 index 0000000..042b8a8 --- /dev/null +++ b/lib/purls/main_menu.js @@ -0,0 +1,8 @@ +function go(request, response, callback) { + console.log("in main_head.go"); + response.write("

Title

"); + require("./main_menu.js").go(request, response, function(request, response) { + response.write("
"); + bodyFunction(request, response, function(request, response) { + response.write("
"); + require("./main_footer.js").go(request, response, function(request, response) { + response.write("
menuitem1item2item3
"); + callback(request, response); + return; +} + +exports.go = go; \ No newline at end of file diff --git a/lib/wsrequest.js b/lib/wsrequest.js new file mode 100644 index 0000000..c7797fc --- /dev/null +++ b/lib/wsrequest.js @@ -0,0 +1,97 @@ +var url = require("url"); +var path = require("path"); +var fs = require("fs"); +var myparse = require("./myparse.js"); +var webmain = require("./purls/main.js"); + +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) { + console.log("testing url: ", request.url); + console.log("against: ", purl.pathname); + var chk = purl.pathname.split("."); + console.log("chk: ", chk); + var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase()); + console.log("chkid is ", chkid); + if(chkid != -1) { + isStatic = 1; + } + }); + + if(isStatic == 1) { + // do the static + var servethis = purl.pathname.split("/").pop(); + serveStatic(servethis, response); + return; + } + + //console.log("request: ", request); + console.log("purl: ", purl); + + // 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) { + response.end(); + }); + return; + } + + var thispurl = purl.pathname.split("/")[1]; + fs.stat("./lib/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.write("404 Not Found\n"); + response.end(); + return; + } + var thiserv = require("./purls/web_"+thispurl+".js"); + if(thiserv.requireBody()) { + console.log("yubber is true"); + webmain.serveBody(request, response, thiserv.process); + } else { + console.log("yubber is false"); + thiserv.process(request, response, function(request, response) { + response.end(); + }); + } + return; + }); + + + return; +} + +function serveStatic(staticname, response) { + var pathName = "./res/"+staticname; + console.log("Pathname for check is ", pathName); + fs.exists(pathName, function(exists) { + if(!exists) { + response.writeHead(404, {"Content-Type": "text/plain"}); + response.write("404 Not Found\n"); + response.end(); + return; + } + + fs.readFile(pathName, "binary", function(err, file) { + if(err) { + response.writeHead(500, {"Content-Type": "text/plain"}); + response.write(err + "\n"); + response.end(); + return; + } + + response.writeHead(200); + response.write(file, "binary"); + response.end(); + }); + }); +} + +exports.wsRequest = wsRequest; diff --git a/parts/footer b/parts/footer new file mode 100644 index 0000000..f85901a --- /dev/null +++ b/parts/footer @@ -0,0 +1,5 @@ + +

+Copyright PJR.cc 2012 + + diff --git a/parts/header b/parts/header new file mode 100644 index 0000000..f1c3ea6 --- /dev/null +++ b/parts/header @@ -0,0 +1,5 @@ + + +Title! + + diff --git a/parts/middle b/parts/middle new file mode 100644 index 0000000..6530b63 --- /dev/null +++ b/parts/middle @@ -0,0 +1 @@ +Hi there diff --git a/res/favicon.ico b/res/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..3a79aca38242deb816822839feb954b155c9b842 GIT binary patch literal 3774 zcmeH|KS*m=9LK+RD;@F%A8mMGI@Cf((Jd5w2*oL&vx6x%jZTh&hzM#C63FhNL)B+W z;l(i!$)=m>5QAhgy^9&VLi2`D!G6xU_vW9zN9@uLJ@U!<^ZTCf?{`k_jgrIP__+A| zOz$Gn7m~gOmXrjuZrbMOwtMpN`1mNn{a=qVe@xVPcu?a5c+h-MrQKG=0~hLTwJU9T z;6fdKhsMJPB*>l6%*+g)Q}|8E zb>k{R4pI;Yw+Oj79`q@Q$MK5eHG$Uz@83AFG1Y1@oxmC-pmqXlkbqcBg+f#rNWl~o zGLV8Ph(@{Z5v{;1j6ik;W?^DP>2ySMFb3&4h{IUKyt?L<s#<^?ipM!v8;|=d3TNr_AwQqIaB0b0Le1og8LrTK5|aN890IpTM7l zp-^l3=_6w1N$3lHT#s^t=*A25)|@>@Nk%5oHu%jhgdaT>*!%1e^$bI`vn$`v(ek#{ z^UpPG&*?k%Wunuj{*8^H^rmFf=!ez^dv^KCrrG2hbSkh$-C5bxVg1p~n;cK{{s9S? zsBON!Gx2vYwTe#APh!4f9q`2k@;-l+O+IrW)6TT7cb3hD&g~4;gOjgc?C8mAo(dg3 zt;cb;HE(PWv}=rfMW>QrYBtllGsm${yT6p2w2|&6UmfakC1h(igQSgalWZ|4#|Uf- z=61Ymw1I_GwrkUz!f5P-k3c^vjoR~tOMc|Q$d;ec+k)nGz%}{~eveIHXf~P_Or0{r zGVetg4*O7G#eqKQPdM)Pp+4;Whlg2LACBlpL)p_lR0R=!MEY8Ja7P&Ju5*CV#j;%w zAA)YMr=D=Jd-iapw}0wyqWlW>ih9QVdS<3#v)H5Q9ro)T$G#vRmp@y-|FhZa+8q!2 z`v%?k;Al{9O8G=NgY|SD!a?I;KIm_k&Em`S_P6P|EzkNtCk)p9iT>VfdL{ke|2y~a EH|O#cX#fBK literal 0 HcmV?d00001 diff --git a/res/test.html b/res/test.html new file mode 100644 index 0000000..732eed5 --- /dev/null +++ b/res/test.html @@ -0,0 +1,3 @@ +

this is a test

+ +this is a journey, into sound... -- 1.7.0.4