sync/lib/config.js

218 lines
6.6 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
2013-03-24 02:28:20 +00:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2013-03-24 02:28:20 +00:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2013-03-24 02:28:20 +00:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-02-16 17:19:59 +00:00
2013-07-28 15:49:29 +00:00
var fs = require("fs");
2014-01-22 23:11:26 +00:00
var path = require("path");
2013-07-28 15:49:29 +00:00
var Logger = require("./logger");
var nodemailer = require("nodemailer");
2014-01-22 23:11:26 +00:00
var YAML = require("yamljs");
2013-05-30 18:07:29 +00:00
2013-07-28 15:49:29 +00:00
var defaults = {
2014-01-22 23:11:26 +00:00
mysql: {
server: "localhost",
database: "cytube3",
user: "cytube3",
password: ""
2013-07-28 15:49:29 +00:00
},
2014-01-22 23:11:26 +00:00
http: {
host: "",
port: 8080,
domain: "http://localhost",
2014-02-26 16:50:59 +00:00
"root-domain": "localhost",
"alt-domains": ["127.0.0.1"],
2014-02-24 05:27:07 +00:00
minify: false,
"cache-ttl": 0
2014-01-22 23:11:26 +00:00
},
https: {
enabled: false,
port: 8443,
2014-01-26 06:01:36 +00:00
domain: "https://localhost",
2014-01-22 23:11:26 +00:00
keyfile: "localhost.key",
passphrase: "",
certfile: "localhost.cert"
},
io: {
2014-02-26 16:57:49 +00:00
domain: "http://localhost",
2014-01-22 23:11:26 +00:00
port: 1337,
"ip-connection-limit": 10
},
mail: {
enabled: false,
transport: "SMTP",
/* the key "config" is omitted because the format depends on the
service the owner is configuring for nodemailer */
"from-address": "some.user@gmail.com"
},
"youtube-v2-key": "",
"channel-save-interval": 5,
"max-channels-per-user": 5,
2014-02-10 01:52:24 +00:00
"max-accounts-per-ip": 5,
2014-01-22 23:11:26 +00:00
"guest-login-delay": 60,
"enable-tor-blocker": true,
stats: {
interval: 3600000,
"max-age": 86400000
},
aliases: {
"purge-interval": 3600000,
"max-age": 2592000000
},
"html-template": {
2014-02-06 00:05:52 +00:00
title: "CyTube Beta", description: "Free, open source synchtube"
},
"reserved-names": {
usernames: ["^(.*?[-_])?admin(istrator)?([-_].*)?$", "^(.*?[-_])?owner([-_].*)?$"],
channels: ["^(.*?[-_])?admin(istrator)?([-_].*)?$", "^(.*?[-_])?owner([-_].*)?$"],
pagetitles: []
2014-02-14 00:15:22 +00:00
},
"contacts": [
{
name: "calzoneman",
title: "Developer",
email: "cyzon@cytu.be"
}
]
2014-01-22 23:11:26 +00:00
};
2013-07-06 17:00:02 +00:00
2014-01-22 23:11:26 +00:00
/**
* Merges a config object with the defaults, warning about missing keys
*/
function merge(obj, def, path) {
for (var key in def) {
if (key in obj) {
if (typeof obj[key] === "object") {
merge(obj[key], def[key], path + "." + key);
2013-07-28 15:49:29 +00:00
}
2014-01-22 23:11:26 +00:00
} else {
Logger.syslog.log("[WARNING] Missing config key " + (path + "." + key) +
"; using default: " + JSON.stringify(def[key]));
obj[key] = def[key];
2013-07-28 15:49:29 +00:00
}
2014-01-22 23:11:26 +00:00
}
}
var cfg = defaults;
2013-07-28 15:49:29 +00:00
2014-01-22 23:11:26 +00:00
/**
* Initializes the configuration from the given YAML file
*/
exports.load = function (file) {
try {
cfg = YAML.load(path.join(__dirname, "..", file));
} catch (e) {
if (e.code === "ENOENT") {
Logger.syslog.log(file + " does not exist, assuming default configuration");
cfg = defaults;
return;
} else {
Logger.errlog.log("Error loading config file " + file + ": ");
if (e.stack) {
Logger.errlog.log(e.stack);
}
cfg = defaults;
2013-07-28 15:49:29 +00:00
return;
}
2014-01-22 23:11:26 +00:00
}
2013-07-28 15:49:29 +00:00
2014-01-22 23:11:26 +00:00
if (cfg == null) {
Logger.syslog.log(file + " is an Invalid configuration file, " +
"assuming default configuration");
cfg = defaults;
return;
}
2013-07-28 15:49:29 +00:00
2014-01-22 23:11:26 +00:00
var mailconfig = {};
if (cfg.mail && cfg.mail.config) {
mailconfig = cfg.mail.config;
delete cfg.mail.config;
}
merge(cfg, defaults, "config");
cfg.mail.config = mailconfig;
preprocessConfig(cfg);
Logger.syslog.log("Loaded configuration from " + file);
};
function preprocessConfig(cfg) {
// Root domain should start with a . for cookies
var root = cfg.http["root-domain"];
root = root.replace(/^\.*/, "");
cfg.http["root-domain"] = root;
// Setup nodemailer
2014-01-22 23:11:26 +00:00
cfg.mail.nodemailer = nodemailer.createTransport(
cfg.mail.transport,
cfg.mail.config
);
// Debug
2014-01-22 23:11:26 +00:00
if (process.env.DEBUG === "1" || process.env.DEBUG === "true") {
cfg.debug = true;
} else {
cfg.debug = false;
}
// Strip trailing slashes from domains
2014-01-26 06:01:36 +00:00
cfg.http.domain = cfg.http.domain.replace(/\/*$/, "");
cfg.https.domain = cfg.https.domain.replace(/\/*$/, "");
// HTTP/HTTPS domains with port numbers
2014-02-28 06:09:20 +00:00
if (!cfg.http["full-address"]) {
var httpfa = cfg.http.domain;
if (cfg.http.port !== 80) {
httpfa += ":" + cfg.http.port;
}
cfg.http["full-address"] = httpfa;
}
2014-02-28 06:09:20 +00:00
if (!cfg.https["full-address"]) {
var httpsfa = cfg.https.domain;
if (cfg.https.port !== 443) {
httpsfa += ":" + cfg.https.port;
}
cfg.https["full-address"] = httpsfa;
}
2014-02-06 00:05:52 +00:00
// Generate RegExps for reserved names
2014-02-06 00:05:52 +00:00
var reserved = cfg["reserved-names"];
for (var key in reserved) {
if (reserved[key] && reserved[key].length > 0) {
reserved[key] = new RegExp(reserved[key].join("|"), "i");
} else {
reserved[key] = false;
}
}
return cfg;
}
2014-01-22 23:11:26 +00:00
/**
* Retrieves a configuration value with the given key
*
* Accepts a dot-separated key for nested values, e.g. "http.port"
* Throws an error if a nonexistant key is requested
*/
exports.get = function (key) {
var obj = cfg;
var keylist = key.split(".");
var current = keylist.shift();
var path = current;
while (keylist.length > 0) {
if (!(current in obj)) {
throw new Error("Nonexistant config key '" + path + "." + current + "'");
}
obj = obj[current];
current = keylist.shift();
path += "." + current;
}
return obj[current];
};