--- /dev/null
+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;
global.repoproxy.listenPort = 8008;
global.repoproxy.cacheDir = "./cache";
global.repoproxy.repo = new Object();
+ global.repoproxy.scancache = 1;
var confFileData = fs.readFileSync(conffile, "utf8");
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);
}
}
}
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/, "");
}
} 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);
}
});
}
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);
# 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