From 6b169fd0ef97d29508089f20584b5860b8c06781 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sun, 1 Dec 2019 19:09:45 +0000 Subject: [configloader] add api to get a config by id --- server/api/configloader.js | 6 ++++++ server/lib/confighelper.js | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'server') diff --git a/server/api/configloader.js b/server/api/configloader.js index f32bfa2..1ce711c 100644 --- a/server/api/configloader.js +++ b/server/api/configloader.js @@ -7,6 +7,12 @@ var noAuthRouter = decorateApp(express.Router()) const configHelper = require(path.join(__appdir, 'lib', 'confighelper')) const log = require(path.join(__appdir, 'lib', 'log')) +noAuthRouter.getAsync('/configs/:id', async (req, res) => { + const config = await configHelper.prepareConfig({ id: req.params.id }) + res.set('Content-Type', 'text/plain') + res.send(config.script) +}) + noAuthRouter.getAsync('/groups/:id', async (req, res) => { const list = req.query.list !== undefined && req.query.list !== 'false' const group = await db.group.findOne({ where: { id: req.params.id } }) diff --git a/server/lib/confighelper.js b/server/lib/confighelper.js index e2535c8..b322ab2 100644 --- a/server/lib/confighelper.js +++ b/server/lib/confighelper.js @@ -47,7 +47,7 @@ async function getConfig (client, list) { } // No config found, use default config - let config = await _prepareConfig({ id: 'DEFAULT', source: { type: 'DEFAULT' } }) + let config = await prepareConfig({ id: 'DEFAULT', source: { type: 'DEFAULT' } }) if (config === null) config = { id: 'DEFAULT', name: 'Fallback Default', script: getDefaultConfig(client), source: { type: 'DEFAULT' } } if (!list) return config @@ -70,7 +70,7 @@ async function getGroupConfig (group, list) { } // No config found, use default config - let config = await _prepareConfig({ id: 'DEFAULT', source: { type: 'DEFAULT' } }) + let config = await prepareConfig({ id: 'DEFAULT', source: { type: 'DEFAULT' } }) if (config === null) config = { id: 'DEFAULT', name: 'Fallback Default', script: getDefaultConfig({}), source: { type: 'DEFAULT' } } if (!list) return config @@ -133,15 +133,15 @@ async function _checkParentConfigs (id, breakOnImportant, isClient = true) { }) if (importants.length > 1) importantList.push(await _createDynamicMenu(importants)) - else if (importants.length === 1) importantList.push(await _prepareConfig(importants[0])) + else if (importants.length === 1) importantList.push(await prepareConfig(importants[0])) if (breakOnImportant && importantList.length) break if (nonImportants.length > 1) eventList.push(await _createDynamicMenu(nonImportants)) - else if (nonImportants.length === 1) eventList.push(await _prepareConfig(nonImportants[0])) + else if (nonImportants.length === 1) eventList.push(await prepareConfig(nonImportants[0])) if (configs.length > 1) configList.push(await _createDynamicMenu(configs)) - else if (configs.length === 1) configList.push(await _prepareConfig(configs[0])) + else if (configs.length === 1) configList.push(await prepareConfig(configs[0])) if (isClient) { parents = (await db.group.findAll({ where: { '$clients.id$': id }, include: ['clients'] })).map(x => x.id) @@ -161,7 +161,7 @@ async function _checkParentConfigs (id, breakOnImportant, isClient = true) { } // create the config script from database -async function _prepareConfig (configInfo, noScript) { +async function prepareConfig (configInfo, noScript) { let id = configInfo.id // If no id is given, find the default config @@ -227,7 +227,7 @@ async function _createDynamicMenu (configInfos, noScript) { } if (ids.length === 1) { - return _prepareConfig({ + return prepareConfig({ id: ids[0], source: idSourceMap[ids[0]] }) @@ -251,7 +251,7 @@ async function _createDynamicMenu (configInfos, noScript) { script += 'menu ' + 'Choose one configuration to boot' + '\r\n' configs.forEach(config => { script += 'item ' - script += ' menuentry' + config.id + ' ' + script += 'menuentry' + config.id + ' ' script += config.name script += '\r\n' @@ -259,10 +259,10 @@ async function _createDynamicMenu (configInfos, noScript) { defaultentry = 'menuentry' + config.id menuscript += ':' + 'menuentry' + config.id + '\r\n' - menuscript += 'chain ' + 'https://' + url + '/api/configloader/getconfig/' + config.id + '\r\n\r\n' + menuscript += 'chain ' + 'https://' + url + '/api/configloader/configs/' + config.id + '\r\n\r\n' }) script += 'choose --default ' + defaultentry + ' ' - script += '--timeout 15 ' + script += '--timeout 15000 ' script += `target && goto \${target}\r\n\r\n` script += menuscript @@ -301,4 +301,4 @@ function getDefaultConfig (client) { return script } -module.exports = { getConfig, getGroupConfig, getDefaultConfig } +module.exports = { getConfig, getGroupConfig, getDefaultConfig, prepareConfig } -- cgit v1.2.3-55-g7522