adding cache control
[nodejs-repoproxy.git] / lib / cache.js
diff --git a/lib/cache.js b/lib/cache.js
new file mode 100644 (file)
index 0000000..18d0045
--- /dev/null
@@ -0,0 +1,44 @@
+var fs = require("fs");
+
+
+function maintainCache() {
+       // TODO i should check that im already running here and exit if i am
+       console.log("Cache maintainence routine starting...");
+       console.log("Cache maintainence routine ended...");
+}
+
+exports.startTimer = function() {
+       // our once-a-day cache maintainer
+       var cacheTimer = global.repoproxy.scancache*3600*1000;
+       //var cacheTimer = global.repoproxy.scancache*100;
+       setInterval(maintainCache, cacheTimer);
+}
+
+
+// the service file routine .... PLEASE KILL ME!
+function serviceFile(reqpath, res, range) {
+       
+       // for now, ignore range.
+       
+       fs.exists(reqpath, function(exists) {
+               if(exists) {
+                               var inp = fs.createReadStream(reqpath);
+                               inp.setEncoding("utf8");
+                               inp.on("data", function(data) {
+                                       res.write(data);
+                               });
+                               
+                               inp.on("end", function(closed) {
+                                       res.end();
+                               });
+               } else {
+                       
+                       // TODO, we need to send this upstream, if its upstream we go up.
+                       res.writeHead(404, {"Content-Type": "text/plain"});
+                       res.write("404 Not Found\n");
+                       res.end();
+               }
+       });
+}
+
+exports.serviceFile = serviceFile;