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