BSD and expact license modified
[KiwiIRC.git] / server / configuration.js
CommitLineData
cbe80532
JA
1var fs = require('fs'),
2 events = require('events'),
60e7704b 3 util = require('util'),
94b00b2d
JA
4 path = require('path'),
5 winston = require('winston');
1360a454
D
6
7var config_filename = 'config.js',
11dbb00f 8 config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
1360a454 9 environment = 'production',
11dbb00f 10 loaded_config = Object.create(null);
1360a454 11
cbe80532
JA
12var Config = function () {
13 events.EventEmitter.call(this);
14};
15util.inherits(Config, events.EventEmitter);
1360a454 16
da0ec4f7 17Config.prototype.loadConfig = function (manual_config_file) {
1360a454 18 var new_config,
33edb4a0
T
19 conf_filepath,
20 i;
1360a454 21
60e7704b
JA
22 if ((manual_config_file) || (this.manual_config_file)) {
23 manual_config_file = path.resolve(path.normalize(manual_config_file || this.manual_config_file));
da0ec4f7
JA
24 if (fs.existsSync(manual_config_file)) {
25 try {
26 if (fs.lstatSync(manual_config_file).isFile() === true) {
27 // Clear the loaded config cache
28 delete require.cache[require.resolve(manual_config_file)];
1360a454 29
da0ec4f7
JA
30 // Try load the new config file
31 new_config = require(manual_config_file);
3f5e22c6
JA
32
33 // Save location of configuration file so that we can re-load it later
34 this.manual_config_file = manual_config_file;
da0ec4f7
JA
35 }
36 } catch (e) {
94b00b2d 37 winston.error('An error occured parsing the config file %s: %s', manual_config_file, e.message);
da0ec4f7 38 process.exit(1);
1360a454 39 }
da0ec4f7 40 } else {
94b00b2d 41 winston.error('Could not find config file %s', manual_config_file);
da0ec4f7
JA
42 process.exit(1);
43 }
44 } else {
45 // Loop through the possible config paths and find a usable one
46 for (i = 0; i < config_dirs.length; i++) {
47 conf_filepath = config_dirs[i] + config_filename;
48
49 try {
50 if (fs.lstatSync(conf_filepath).isFile() === true) {
51 // Clear the loaded config cache
52 delete require.cache[require.resolve(conf_filepath)];
53
54 // Try load the new config file
55 new_config = require(conf_filepath);
56 break;
57 }
58 } catch (e) {
59 switch (e.code) {
60 case 'ENOENT': // No file/dir
61 break;
62 default:
94b00b2d 63 winston.warn('An error occured parsing the config file %s%s: %s', config_dirs[i], config_filename, e.message);
da0ec4f7
JA
64 return false;
65 }
66 continue;
1360a454 67 }
1360a454
D
68 }
69 }
70
71 if (new_config) {
11dbb00f 72 loaded_config = new_config;
0a8a61ad 73 global.config = new_config[environment] || {};
cbe80532 74 this.emit('loaded');
11dbb00f 75 return loaded_config;
1360a454 76 } else {
11dbb00f 77 return false;
1360a454 78 }
cbe80532 79};
1360a454
D
80
81
82
cbe80532 83Config.prototype.setEnvironment = function (new_environment) {
11dbb00f 84 environment = new_environment;
1360a454
D
85};
86
87// Get the current config. Optionally for a different environment than currently set
cbe80532 88Config.prototype.get = function (specific_environment) {
11dbb00f 89 specific_environment = specific_environment || environment;
61428716 90
0a8a61ad 91 return loaded_config[specific_environment] || {};
1360a454
D
92};
93
cbe80532 94module.exports = new Config();