first commit
authorpaulr <paulr@tv.pjr.cc>
Fri, 21 Dec 2012 08:43:01 +0000 (19:43 +1100)
committerpaulr <paulr@tv.pjr.cc>
Fri, 21 Dec 2012 08:43:01 +0000 (19:43 +1100)
12 files changed:
app.js [new file with mode: 0644]
lib/myparse.js [new file with mode: 0644]
lib/purls/main.js [new file with mode: 0644]
lib/purls/main_footer.js [new file with mode: 0644]
lib/purls/main_head.js [new file with mode: 0644]
lib/purls/main_menu.js [new file with mode: 0644]
lib/wsrequest.js [new file with mode: 0644]
parts/footer [new file with mode: 0644]
parts/header [new file with mode: 0644]
parts/middle [new file with mode: 0644]
res/favicon.ico [new file with mode: 0644]
res/test.html [new file with mode: 0644]

diff --git a/app.js b/app.js
new file mode 100644 (file)
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 (file)
index 0000000..088dd7a
--- /dev/null
@@ -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 (file)
index 0000000..c5bf240
--- /dev/null
@@ -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("<table border=\"1\" width=\"100%\"><tr><td><h1>Title</h1></td></tr><tr><td>");
+               require("./main_menu.js").go(request, response, function(request, response) {
+                       response.write("</td></tr><tr><td>");
+                       bodyFunction(request, response, function(request, response) {
+                               response.write("</td></tr><tr><td>");
+                               require("./main_footer.js").go(request, response, function(request, response) {
+                                       response.write("</td></tr></body></html>");
+                                       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 (file)
index 0000000..19f275e
--- /dev/null
@@ -0,0 +1,7 @@
+function go(request, response, callback) {
+       response.write("<br><br><font size=\"-1\"><i>Copyright PJR.cc 2012</i></font>");
+       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 (file)
index 0000000..463d8dd
--- /dev/null
@@ -0,0 +1,8 @@
+function go(request, response, callback) {
+       console.log("in main_head.go");
+       response.write("<html><head><title>im a head</title></head><body>");
+       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 (file)
index 0000000..042b8a8
--- /dev/null
@@ -0,0 +1,8 @@
+function go(request, response, callback) {
+       console.log("in main_head.go");
+       response.write("<table><tr><th>menu</th><td>item1</td><td>item2</td><td>item3</td></tr></table>");
+       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 (file)
index 0000000..c7797fc
--- /dev/null
@@ -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 (file)
index 0000000..f85901a
--- /dev/null
@@ -0,0 +1,5 @@
+
+<br><br>
+<font size="-1">Copyright PJR.cc 2012</font>
+</body>
+</html>
diff --git a/parts/header b/parts/header
new file mode 100644 (file)
index 0000000..f1c3ea6
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<head>
+<title>Title!</title>
+</head>
+<body>
diff --git a/parts/middle b/parts/middle
new file mode 100644 (file)
index 0000000..6530b63
--- /dev/null
@@ -0,0 +1 @@
+Hi there
diff --git a/res/favicon.ico b/res/favicon.ico
new file mode 100644 (file)
index 0000000..3a79aca
Binary files /dev/null and b/res/favicon.ico differ
diff --git a/res/test.html b/res/test.html
new file mode 100644 (file)
index 0000000..732eed5
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>this is a test</h1>
+
+this is a journey, into sound...