Merge pull request #387 from thelounge/xpaw/config

Cache loaded config and merge it with defaults
This commit is contained in:
Alistair McKinlay 2016-07-04 08:18:55 +01:00 committed by GitHub
commit 9c07f2b0f2
9 changed files with 55 additions and 61 deletions

View File

@ -134,7 +134,7 @@ Client.prototype.find = function(id) {
}; };
Client.prototype.connect = function(args) { Client.prototype.connect = function(args) {
var config = Helper.getConfig(); var config = Helper.config;
var client = this; var client = this;
var nick = args.nick || "lounge-user"; var nick = args.nick || "lounge-user";
@ -426,9 +426,8 @@ Client.prototype.quit = function() {
var timer; var timer;
Client.prototype.save = function(force) { Client.prototype.save = function(force) {
var client = this; var client = this;
var config = Helper.getConfig();
if (config.public) { if (Helper.config.public) {
return; return;
} }

View File

@ -7,12 +7,10 @@ var Oidentd = require("./oidentd");
module.exports = ClientManager; module.exports = ClientManager;
function ClientManager() { function ClientManager() {
var config = Helper.getConfig();
this.clients = []; this.clients = [];
if (typeof config.oidentd === "string") { if (typeof Helper.config.oidentd === "string") {
this.identHandler = new Oidentd(config.oidentd); this.identHandler = new Oidentd(Helper.config.oidentd);
} }
} }

View File

@ -13,22 +13,25 @@ program
.description("Start the server") .description("Start the server")
.action(function() { .action(function() {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
var config = Helper.getConfig();
var mode = config.public; var mode = Helper.config.public;
if (program.public) { if (program.public) {
mode = true; mode = true;
} else if (program.private) { } else if (program.private) {
mode = false; mode = false;
} }
if (!mode && !users.length) { if (!mode && !users.length) {
log.warn("No users found!"); log.warn("No users found!");
log.info("Create a new user with 'lounge add <name>'."); log.info("Create a new user with 'lounge add <name>'.");
} else {
server({ return;
host: program.host || process.env.IP || config.host,
port: program.port || process.env.PORT || config.port,
bind: program.bind || config.bind,
public: mode
});
} }
Helper.config.host = program.host || Helper.config.host;
Helper.config.port = program.port || Helper.config.port;
Helper.config.bind = program.bind || Helper.config.bind;
Helper.config.public = mode;
server();
}); });

View File

@ -1,9 +1,10 @@
var _ = require("lodash");
var path = require("path"); var path = require("path");
var os = require("os"); var os = require("os");
var Helper = { var Helper = {
config: null,
expandHome: expandHome, expandHome: expandHome,
getConfig: getConfig,
getUserConfigPath: getUserConfigPath, getUserConfigPath: getUserConfigPath,
getUserLogsPath: getUserLogsPath, getUserLogsPath: getUserLogsPath,
setHome: setHome, setHome: setHome,
@ -11,10 +12,21 @@ var Helper = {
module.exports = Helper; module.exports = Helper;
Helper.config = require(path.resolve(path.join(
__dirname,
"..",
"defaults",
"config.js"
)));
function setHome(homePath) { function setHome(homePath) {
this.HOME = expandHome(homePath || "~/.lounge"); this.HOME = expandHome(homePath || "~/.lounge");
this.CONFIG_PATH = path.join(this.HOME, "config.js"); this.CONFIG_PATH = path.join(this.HOME, "config.js");
this.USERS_PATH = path.join(this.HOME, "users"); this.USERS_PATH = path.join(this.HOME, "users");
// Reload config from new home location
var userConfig = require(this.CONFIG_PATH);
this.config = _.extend(this.config, userConfig);
} }
function getUserConfigPath(name) { function getUserConfigPath(name) {
@ -25,10 +37,6 @@ function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network); return path.join(this.HOME, "logs", name, network);
} }
function getConfig() {
return require(this.CONFIG_PATH);
}
function expandHome(shortenedPath) { function expandHome(shortenedPath) {
var home; var home;

View File

@ -3,9 +3,8 @@ var moment = require("moment");
var Helper = require("./helper"); var Helper = require("./helper");
function timestamp(type, messageArgs) { function timestamp(type, messageArgs) {
var config = Helper.getConfig(); var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss";
var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; var tz = Helper.config.logs.timezone || "UTC+00:00";
var tz = (config.logs || {}).timezone || "UTC+00:00";
var time = moment().utcOffset(tz).format(format); var time = moment().utcOffset(tz).format(format);

View File

@ -10,7 +10,6 @@ Chan.Type = {
}; };
var id = 0; var id = 0;
var config = Helper.getConfig();
function Chan(attr) { function Chan(attr) {
_.merge(this, _.extend({ _.merge(this, _.extend({
@ -33,14 +32,14 @@ Chan.prototype.pushMessage = function(client, msg) {
// Never store messages in public mode as the session // Never store messages in public mode as the session
// is completely destroyed when the page gets closed // is completely destroyed when the page gets closed
if (config.public) { if (Helper.config.public) {
return; return;
} }
this.messages.push(msg); this.messages.push(msg);
if (config.maxHistory >= 0 && this.messages.length > config.maxHistory) { if (Helper.config.maxHistory >= 0 && this.messages.length > Helper.config.maxHistory) {
this.messages.splice(0, this.messages.length - config.maxHistory); this.messages.splice(0, this.messages.length - Helper.config.maxHistory);
} }
}; };

View File

@ -10,8 +10,7 @@ process.setMaxListeners(0);
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("privmsg", function(data) { irc.on("privmsg", function(data) {
var config = Helper.getConfig(); if (!Helper.config.prefetch) {
if (!config.prefetch) {
return; return;
} }
@ -45,7 +44,6 @@ module.exports = function(irc, network) {
}; };
function parse(msg, url, res, client) { function parse(msg, url, res, client) {
var config = Helper.getConfig();
var toggle = msg.toggle = { var toggle = msg.toggle = {
id: msg.id, id: msg.id,
type: "", type: "",
@ -55,9 +53,6 @@ function parse(msg, url, res, client) {
link: url link: url
}; };
if (!config.prefetchMaxImageSize) {
config.prefetchMaxImageSize = 512;
}
switch (res.type) { switch (res.type) {
case "text/html": case "text/html":
var $ = cheerio.load(res.text); var $ = cheerio.load(res.text);
@ -77,7 +72,7 @@ function parse(msg, url, res, client) {
case "image/gif": case "image/gif":
case "image/jpg": case "image/jpg":
case "image/jpeg": case "image/jpeg":
if (res.size < (config.prefetchMaxImageSize * 1024)) { if (res.size < (Helper.config.prefetchMaxImageSize * 1024)) {
toggle.type = "image"; toggle.type = "image";
} else { } else {
return; return;

View File

@ -8,39 +8,32 @@ var fs = require("fs");
var io = require("socket.io"); var io = require("socket.io");
var dns = require("dns"); var dns = require("dns");
var Helper = require("./helper"); var Helper = require("./helper");
var config = {};
var manager = null; var manager = null;
module.exports = function(options) { module.exports = function() {
manager = new ClientManager(); manager = new ClientManager();
config = Helper.getConfig();
config = _.extend(config, options);
var app = express() var app = express()
.use(allRequests) .use(allRequests)
.use(index) .use(index)
.use(express.static("client")); .use(express.static("client"));
var config = Helper.config;
var server = null; var server = null;
var https = config.https || {};
var protocol = https.enable ? "https" : "http";
var port = config.port;
var host = config.host;
var transports = config.transports || ["polling", "websocket"];
if (!https.enable) { if (!config.https.enable) {
server = require("http"); server = require("http");
server = server.createServer(app).listen(port, host); server = server.createServer(app).listen(config.port, config.host);
} else { } else {
server = require("spdy"); server = require("spdy");
server = server.createServer({ server = server.createServer({
key: fs.readFileSync(Helper.expandHome(https.key)), key: fs.readFileSync(Helper.expandHome(config.https.key)),
cert: fs.readFileSync(Helper.expandHome(https.certificate)) cert: fs.readFileSync(Helper.expandHome(config.https.certificate))
}, app).listen(port, host); }, app).listen(config.port, config.host);
} }
if ((config.identd || {}).enable) { if (config.identd.enable) {
if (manager.identHandler) { if (manager.identHandler) {
log.warn("Using both identd and oidentd at the same time!"); log.warn("Using both identd and oidentd at the same time!");
} }
@ -49,7 +42,7 @@ module.exports = function(options) {
} }
var sockets = io(server, { var sockets = io(server, {
transports: transports transports: config.transports
}); });
sockets.on("connect", function(socket) { sockets.on("connect", function(socket) {
@ -62,7 +55,8 @@ module.exports = function(options) {
manager.sockets = sockets; manager.sockets = sockets;
log.info("The Lounge v" + pkg.version + " is now running on", protocol + "://" + (config.host || "*") + ":" + config.port + "/"); var protocol = config.https.enable ? "https" : "http";
log.info("The Lounge v" + pkg.version + " is now running on", protocol + "://" + (config.host || "*") + ":" + config.port + "/", (config.public ? "in public mode" : "in private mode"));
log.info("Press ctrl-c to stop\n"); log.info("Press ctrl-c to stop\n");
if (!config.public) { if (!config.public) {
@ -74,7 +68,7 @@ module.exports = function(options) {
}; };
function getClientIp(req) { function getClientIp(req) {
if (!config.reverseProxy) { if (!Helper.config.reverseProxy) {
return req.connection.remoteAddress; return req.connection.remoteAddress;
} else { } else {
return req.headers["x-forwarded-for"] || req.connection.remoteAddress; return req.headers["x-forwarded-for"] || req.connection.remoteAddress;
@ -94,7 +88,7 @@ function index(req, res, next) {
return fs.readFile("client/index.html", "utf-8", function(err, file) { return fs.readFile("client/index.html", "utf-8", function(err, file) {
var data = _.merge( var data = _.merge(
pkg, pkg,
config Helper.config
); );
var template = _.template(file); var template = _.template(file);
res.setHeader("Content-Security-Policy", "default-src *; style-src * 'unsafe-inline'; script-src 'self'; child-src 'none'; object-src 'none'; form-action 'none'; referrer no-referrer;"); res.setHeader("Content-Security-Policy", "default-src *; style-src * 'unsafe-inline'; script-src 'self'; child-src 'none'; object-src 'none'; form-action 'none'; referrer no-referrer;");
@ -130,7 +124,7 @@ function init(socket, client) {
client.connect(data); client.connect(data);
} }
); );
if (!config.public) { if (!Helper.config.public) {
socket.on( socket.on(
"change-password", "change-password",
function(data) { function(data) {
@ -217,14 +211,14 @@ function reverseDnsLookup(socket, client) {
function auth(data) { function auth(data) {
var socket = this; var socket = this;
if (config.public) { if (Helper.config.public) {
var client = new Client(manager); var client = new Client(manager);
manager.clients.push(client); manager.clients.push(client);
socket.on("disconnect", function() { socket.on("disconnect", function() {
manager.clients = _.without(manager.clients, client); manager.clients = _.without(manager.clients, client);
client.quit(); client.quit();
}); });
if (config.webirc) { if (Helper.config.webirc) {
reverseDnsLookup(socket, client); reverseDnsLookup(socket, client);
} else { } else {
init(socket, client); init(socket, client);
@ -242,7 +236,7 @@ function auth(data) {
} }
} }
if (success) { if (success) {
if (config.webirc !== null && !client.config["ip"]) { if (Helper.config.webirc !== null && !client.config["ip"]) {
reverseDnsLookup(socket, client); reverseDnsLookup(socket, client);
} else { } else {
init(socket, client); init(socket, client);

View File

@ -12,9 +12,8 @@ module.exports.write = function(user, network, chan, msg) {
return; return;
} }
var config = Helper.getConfig(); var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss";
var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; var tz = Helper.config.logs.timezone || "UTC+00:00";
var tz = (config.logs || {}).timezone || "UTC+00:00";
var time = moment().utcOffset(tz).format(format); var time = moment().utcOffset(tz).format(format);
var line = "[" + time + "] "; var line = "[" + time + "] ";