18d0045aa4759ac27fe51d47cdf52ceafa6ea67d
[nodejs-repoproxy.git] / lib / cache.js
1 var fs = require("fs");
2
3
4 function maintainCache() {
5         // TODO i should check that im already running here and exit if i am
6         console.log("Cache maintainence routine starting...");
7         console.log("Cache maintainence routine ended...");
8 }
9
10 exports.startTimer = function() {
11         // our once-a-day cache maintainer
12         var cacheTimer = global.repoproxy.scancache*3600*1000;
13         //var cacheTimer = global.repoproxy.scancache*100;
14         setInterval(maintainCache, cacheTimer);
15 }
16
17
18 // the service file routine .... PLEASE KILL ME!
19 function serviceFile(reqpath, res, range) {
20         
21         // for now, ignore range.
22         
23         fs.exists(reqpath, function(exists) {
24                 if(exists) {
25                                 var inp = fs.createReadStream(reqpath);
26                                 inp.setEncoding("utf8");
27                                 inp.on("data", function(data) {
28                                         res.write(data);
29                                 });
30                                 
31                                 inp.on("end", function(closed) {
32                                         res.end();
33                                 });
34                 } else {
35                         
36                         // TODO, we need to send this upstream, if its upstream we go up.
37                         res.writeHead(404, {"Content-Type": "text/plain"});
38                         res.write("404 Not Found\n");
39                         res.end();
40                 }
41         });
42 }
43
44 exports.serviceFile = serviceFile;