rearranging project layout
[nodejsws.git] / lib / wsrequest.js
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
7 var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
8
9 function wsRequest(request, response) {
10         var isStatic = 0;
11         var purl = url.parse(request.url);
12         
13         staticExtensions.forEach(function testExtn(setest) {
14                 console.log("testing url: ", request.url);
15                 console.log("against: ", purl.pathname);
16                 var chk = purl.pathname.split(".");
17                 console.log("chk: ", chk);
18                 var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase());
19                 console.log("chkid is ", chkid);
20                 if(chkid != -1) {
21                         isStatic = 1;
22                 }
23         });
24         
25         if(isStatic == 1) {
26                 // do the static
27                 var servethis = purl.pathname.split("/").pop();
28                 serveStatic(servethis, response);
29                 return;
30         }
31         
32         //console.log("request: ", request);
33         console.log("purl: ", purl);
34
35         // now we need to find the extension used
36         // to serve the request based no the first purl
37         if(purl.pathname == "/") {
38                 console.log("Serv main");
39                 webmain.serveMain(request, response, function (request, response) {
40                         response.end();
41                 });
42                 return;
43         }
44
45         var thispurl = purl.pathname.split("/")[1];
46         fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
47                 console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
48                 if(err) {
49                         response.writeHead(404, {"Content-Type": "text/plain"});
50                         response.write("404 Not Found\n");
51                         response.end();
52                         return;
53                 }
54                 var thiserv = require("../purls/web_"+thispurl+".js");
55                 if(thiserv.requireBody()) {
56                         console.log("yubber is true");
57                         webmain.serveBody(request, response, thiserv.process);
58                 } else {
59                         console.log("yubber is false");
60                         thiserv.process(request, response, function(request, response) {
61                                 response.end();
62                         });
63                 }
64                 return;
65         });
66         
67         
68         return;
69 }
70
71 function serveStatic(staticname, response) {
72         var pathName = "./res/"+staticname;
73         console.log("Pathname for check is ", pathName);
74         fs.exists(pathName, function(exists) {
75                 if(!exists) {
76                         response.writeHead(404, {"Content-Type": "text/plain"});
77                         response.write("404 Not Found\n");
78                         response.end();
79                         return;
80                 }
81                 
82                 fs.readFile(pathName, "binary", function(err, file) {
83                         if(err) {        
84                                 response.writeHead(500, {"Content-Type": "text/plain"});
85                                 response.write(err + "\n");
86                                 response.end();
87                                 return;
88                         }
89
90                         response.writeHead(200);
91                         response.write(file, "binary");
92                         response.end();                 
93                 });
94         });
95 }
96
97 exports.wsRequest = wsRequest;