adding cache control
authorPaul J R <me@pjr.cc>
Sat, 19 Jan 2013 04:38:59 +0000 (15:38 +1100)
committerPaul J R <me@pjr.cc>
Sat, 19 Jan 2013 04:38:59 +0000 (15:38 +1100)
.gitignore [new file with mode: 0644]
lib/cache.js [new file with mode: 0644]
lib/config.js
lib/router.js
proxy.js
repos.conf

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..14d86ad
--- /dev/null
@@ -0,0 +1 @@
+/cache
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;
index 8d5de4b..dd13be4 100644 (file)
@@ -6,6 +6,7 @@ exports.loadConfig = function (conffile) {
        global.repoproxy.listenPort = 8008;
        global.repoproxy.cacheDir = "./cache";
        global.repoproxy.repo = new Object();
+       global.repoproxy.scancache = 1; 
        
        var confFileData = fs.readFileSync(conffile, "utf8");
        
@@ -40,9 +41,17 @@ exports.loadConfig = function (conffile) {
                        console.log("Port set to: ", line_real[1]);
                        global.repoproxy.listenPort = line_real[1];
                        break;
+               case "cachescan":
+                       console.log("Set cache scan rate to: '%s' hours", line_real[1]);
+                       global.repoproxy.scancache = parseInt(line_real[1]);
+                       if(global.repoproxy.scancache == 0) {
+                               console.log("Cache scan rate didnt make sense, it was 0, and should be at least 1 - it is set to 24, but you should check this setting");
+                               global.repoproxy.scancache = 24;
+                       }
+                       break;
                default:
                        if(line_real[0] != "") {
-                               console.log("Invalid line in configuration file ignored: '%s'", line_one);
+                               console.log("WARNING Invalid line in configuration file ignored: '%s'", line_one);
                        }
                }
        }
index ae8408c..96ca844 100644 (file)
@@ -1,9 +1,11 @@
 var url = require("url");
 var fs = require("fs");
+var cache = require("./cache.js");
 
 exports.routeRequest = function(req, res) {
        // first, strip a /pub/ off the front if it exists
        var originalurl = url.parse(req.url);
+       var range = 0;
                
        thisurl = originalurl.pathname.replace(/^\/pub/, "");
        
@@ -32,16 +34,12 @@ exports.routeRequest = function(req, res) {
                                }
                        } else {
                                if(stat.isFile()) {
-                                       fs.readFile(reqpath, "utf8", function(err, data) {
-                                               res.write(data);
-                                               res.end();
-                                       });
+                                       cache.serviceFile(reqpath, res, range);
                                }
                        }
                } else {
                        // go upstream..
-                       res.write("here we need to go upstream");
-                       res.end();
+                       cache.serviceFile(reqpath, res, range);
                }
        });     
 }
index 770e7dc..65cf726 100644 (file)
--- a/proxy.js
+++ b/proxy.js
@@ -1,12 +1,16 @@
 var http = require("http");
 var config = require("./lib/config.js");
 var router = require("./lib/router.js");
+var cache = require("./lib/cache.js");
 
 
 // first we load the config...
+console.log("Loading configuration");
 config.loadConfig("./repos.conf");
 
-console.log("globals: ", global.repoproxy);
+
+console.log("Starting cache maintenance timer");
+cache.startTimer();
 
 // next we start our main request loop
 http.createServer(router.routeRequest).listen(global.repoproxy.listenPort);
index 90bd646..e98a603 100644 (file)
@@ -1,6 +1,9 @@
 # cachedir is where it'll store files
 cachedir:./cache
 
+# how often we run thru the cache directory and refresh files, delete old files or perform maintenance on repo data (hours)
+cachescan:24
+
 # port to listen on for requests
 listenport:8008