Add canvas to this.canvas (favicon.js)
[KiwiIRC.git] / server / configuration.js
1 var fs = require('fs');
2
3 var config_filename = 'config.js',
4 config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
5 environment = 'production',
6 loaded_config = Object.create(null);
7
8
9 function loadConfig() {
10 var new_config,
11 conf_filepath,
12 i;
13
14 // Loop through the possible config paths and find a usable one
15 for (i = 0; i < config_dirs.length; i++) {
16 conf_filepath = config_dirs[i] + config_filename;
17
18 try {
19 if (fs.lstatSync(conf_filepath).isFile() === true) {
20 // Clear the loaded config cache
21 delete require.cache[require.resolve(conf_filepath)];
22
23 // Try load the new config file
24 new_config = require(conf_filepath);
25 break;
26 }
27 } catch (e) {
28 switch (e.code) {
29 case 'ENOENT': // No file/dir
30 break;
31 default:
32 console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message);
33 return false;
34 }
35 continue;
36 }
37 }
38
39 if (new_config) {
40 loaded_config = new_config;
41 global.config = new_config[environment] || {};
42 return loaded_config;
43 } else {
44 return false;
45 }
46 }
47
48
49
50 module.exports.setEnvironment = function (new_environment) {
51 environment = new_environment;
52 };
53
54 // Get the current config. Optionally for a different environment than currently set
55 module.exports.get = function (specific_environment) {
56 specific_environment = specific_environment || environment;
57
58 return loaded_config[specific_environment] || {};
59 };
60
61 module.exports.loadConfig = loadConfig;