adminsettings: Pay attention to --settings

This commit is contained in:
Richard Hansen 2021-11-10 18:41:53 -05:00
parent 46ad93024d
commit 19e9c2f114
2 changed files with 18 additions and 15 deletions

View file

@ -24,7 +24,7 @@ exports.socketio = (hookName, {io}) => {
socket.on('load', async (query) => { socket.on('load', async (query) => {
let data; let data;
try { try {
data = await fsp.readFile('settings.json', 'utf8'); data = await fsp.readFile(settings.settingsFilename, 'utf8');
} catch (err) { } catch (err) {
return console.log(err); return console.log(err);
} }
@ -36,8 +36,8 @@ exports.socketio = (hookName, {io}) => {
} }
}); });
socket.on('saveSettings', async (settings) => { socket.on('saveSettings', async (newSettings) => {
await fsp.writeFile('settings.json', settings); await fsp.writeFile(settings.settingsFilename, newSettings);
socket.emit('saveprogress', 'saved'); socket.emit('saveprogress', 'saved');
}); });

View file

@ -42,6 +42,12 @@ const _ = require('underscore');
const logger = log4js.getLogger('settings'); const logger = log4js.getLogger('settings');
// Exported values that settings.json and credentials.json cannot override.
const nonSettings = [
'credentialsFilename',
'settingsFilename',
];
// This is a function to make it easy to create a new instance. It is important to not reuse a // This is a function to make it easy to create a new instance. It is important to not reuse a
// config object after passing it to log4js.configure() because that method mutates the object. :( // config object after passing it to log4js.configure() because that method mutates the object. :(
const defaultLogConfig = () => ({appenders: [{type: 'console'}]}); const defaultLogConfig = () => ({appenders: [{type: 'console'}]});
@ -65,6 +71,8 @@ initLogging(defaultLogLevel, defaultLogConfig());
exports.root = absolutePaths.findEtherpadRoot(); exports.root = absolutePaths.findEtherpadRoot();
logger.info('All relative paths will be interpreted relative to the identified ' + logger.info('All relative paths will be interpreted relative to the identified ' +
`Etherpad base dir: ${exports.root}`); `Etherpad base dir: ${exports.root}`);
exports.settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json');
exports.credentialsFilename = absolutePaths.makeAbsolute(argv.credentials || 'credentials.json');
/** /**
* The app title, visible e.g. in the browser window * The app title, visible e.g. in the browser window
@ -489,6 +497,11 @@ exports.getEpVersion = () => require('../../package.json').version;
*/ */
const storeSettings = (settingsObj) => { const storeSettings = (settingsObj) => {
for (const i of Object.keys(settingsObj || {})) { for (const i of Object.keys(settingsObj || {})) {
if (nonSettings.includes(i)) {
logger.warn(`Ignoring setting: '${i}'`);
continue;
}
// test if the setting starts with a lowercase character // test if the setting starts with a lowercase character
if (i.charAt(0).search('[a-z]') !== 0) { if (i.charAt(0).search('[a-z]') !== 0) {
logger.warn(`Settings should start with a lowercase character: '${i}'`); logger.warn(`Settings should start with a lowercase character: '${i}'`);
@ -708,18 +721,8 @@ const parseSettings = (settingsFilename, isSettings) => {
}; };
exports.reloadSettings = () => { exports.reloadSettings = () => {
// Discover where the settings file lives const settings = parseSettings(exports.settingsFilename, true);
const settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json'); const credentials = parseSettings(exports.credentialsFilename, false);
// Discover if a credential file exists
const credentialsFilename = absolutePaths.makeAbsolute(argv.credentials || 'credentials.json');
// try to parse the settings
const settings = parseSettings(settingsFilename, true);
// try to parse the credentials
const credentials = parseSettings(credentialsFilename, false);
storeSettings(settings); storeSettings(settings);
storeSettings(credentials); storeSettings(credentials);