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