441caedc6a427a9757932ed321be9cfd2a66b34c
[nodejs-repoproxy.git] / lib / config.js
1 var fs = require("fs");
2
3 exports.loadConfig = function (conffile) {
4         
5         global.repoproxy = new Object();
6         global.repoproxy.listenPort = 8008;
7         global.repoproxy.cacheDir = "./cache";
8         global.repoproxy.repo = new Object();
9         global.repoproxy.scancache = 1; 
10         
11         var confFileData = fs.readFileSync(conffile, "utf8");
12         
13         // first split the file on carriage returns;
14         var confLines = confFileData.split("\n");
15         
16         // go thru each line looking for config vars
17         for(var i=0; i<confLines.length; i++) {
18                 
19                 // trim a line down
20                 var line_one = confLines[i].trim();
21                 
22                 // split it up with :'s
23                 var line_real = line_one.replace(/#.*/,"").split(":");
24                 
25                 // parse the line
26                 switch(line_real[0]) {
27                 case "repo":
28                         
29                         // TODO: VALIDATE!
30                         console.log("Adding repo: '/%s' type '%s' from '%s', with update interval of '%s' days, and expire time of '%s' days.", line_real[1], line_real[2], line_real[3]+":"+line_real[4], line_real[5], line_real[6]);
31                         var thisrepo = { type : line_real[2], url: line_real[3]+":"+line_real[4], updateinterval: line_real[5], expiretime: line_real[6] };
32                         global.repoproxy.repo[line_real[1]] = thisrepo;
33                         
34                         break;
35                 case "cachedir":
36                         var tmppath = line_real[1].replace(/\/+/g, "/");
37                         console.log("Cache dir set to: ", tmppath);
38                         global.repoproxy.cacheDir = tmppath;
39                         break;
40                 case "listenport":
41                         console.log("Port set to: ", line_real[1]);
42                         global.repoproxy.listenPort = line_real[1];
43                         break;
44                 case "cachescan":
45                         console.log("Set cache scan rate to: '%s' hours", line_real[1]);
46                         global.repoproxy.scancache = parseInt(line_real[1]);
47                         if(global.repoproxy.scancache == 0) {
48                                 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");
49                                 global.repoproxy.scancache = 24;
50                         }
51                         break;
52                 default:
53                         if(line_real[0] != "") {
54                                 console.log("WARNING Invalid line in configuration file ignored: '%s'", line_one);
55                         }
56                 }
57         }
58         
59         createCacheStructure();
60 }
61
62
63 function createCacheStructure() {
64         try {
65                 var state = fs.statSync(global.repoproxy.cacheDir);
66                 //console.log("state is:", state);
67         } catch(e) {
68                 try {
69                         fs.mkdirSync(global.repoproxy.cacheDir);
70                         fs.mkdirSync(global.repoproxy.cacheDir + "/.cleanup");
71                 } catch(ex) {
72                         console.log("ERROR: failure to create cache directory, '%s'", global.repoproxy.cacheDir);
73                 }
74         }
75         
76         try {
77                 fs.mkdirSync(global.repoproxy.cacheDir + "/.cleanup");
78         } catch(ex) {
79                 console.log("ERROR: cant create cleanup directory, '%s'", global.repoproxy.cacheDir + "/.cleanup");
80         }
81         
82         for(var index in global.repoproxy.repo) {
83                 var fullDir = global.repoproxy.cacheDir + "/" + index;
84                 try {
85                         var state = fs.statSync(global.repoproxy.cacheDir);
86                         //console.log("state is:", state);
87                 } catch(e) {
88                         try {
89                                 fs.mkdirSync(fullDir);
90                         } catch(ex) {
91                                 console.log("ERROR: failed to create cache directory, '%s' for '%s'", fullDir, index);
92                         }
93                 }
94         }
95 }