replaced requiring config.json with a global config object

This commit is contained in:
Daniel Sommer 2022-03-24 12:50:16 +01:00
parent bc39bc1694
commit 5e6314eee9
5 changed files with 21 additions and 16 deletions

5
.vscode/launch.json vendored
View file

@ -9,7 +9,10 @@
"skipFiles": [ "skipFiles": [
"<node_internals>/**" "<node_internals>/**"
], ],
"program": "${workspaceFolder}/pbc.js" "program": "${workspaceFolder}/pbc.js",
"args": [
"${workspaceFolder}/example_config.json"
]
} }
] ]
} }

View file

@ -1,20 +1,25 @@
const config = require('../config.json');
const logger = require('./logger.js'); const logger = require('./logger.js');
const constants = require('./constants.js'); const constants = require('./constants.js');
const timeDiff = require('./util.js').timeDiff; const timeDiff = require('./util.js').timeDiff;
const active = config.cache.active; let active;
const lifetime = config.cache.lifetime * 60000; let lifetime;
let modep; let modep;
const cache = new Map(); const cache = new Map();
function isActive() { function isActive() {
if (active === undefined) {
active = global.config.cache.active;
}
return active; return active;
} }
function getLifetime() { function getLifetime() {
if (lifetime === undefined) {
lifetime = global.config.cache.lifetime * 60000
}
return lifetime; return lifetime;
} }

View file

@ -1,4 +1,3 @@
const config = require('../config.json');
const util = require('./util.js'); const util = require('./util.js');
const constants = require('./constants.js'); const constants = require('./constants.js');
const logger = require('./logger.js'); const logger = require('./logger.js');
@ -14,7 +13,7 @@ let pedalboardIdBypassOrigin;
function reset() { function reset() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
util.httpGET(config.modep.host, config.modep.port, '/reset') util.httpGET(global.config.modep.host, global.config.modep.port, '/reset')
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
}); });
@ -35,7 +34,7 @@ function getBanks() {
// banks = util.sortById(fake); // banks = util.sortById(fake);
// return resolve(fake); // return resolve(fake);
util.httpGET(config.modep.host, config.modep.port, '/banks') util.httpGET(global.config.modep.host, global.config.modep.port, '/banks')
.then(function (banks) { .then(function (banks) {
for (let index = 0; index < banks.length; index++) { for (let index = 0; index < banks.length; index++) {
let bank = banks[index]; let bank = banks[index];
@ -87,7 +86,7 @@ function getPedalboards() {
// pedalboards = util.sortById(fake); // pedalboards = util.sortById(fake);
// return resolve(fake); // return resolve(fake);
util.httpGET(config.modep.host, config.modep.port, '/pedalboard/list') util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list')
.then(function (pedalboards) { .then(function (pedalboards) {
let id = 1; let id = 1;
for (let index = 0; index < pedalboards.length; index++) { for (let index = 0; index < pedalboards.length; index++) {
@ -139,7 +138,7 @@ function getCurrentPedalboard() {
// .catch(reject); // .catch(reject);
// PRODUCTION // PRODUCTION
util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current')
.then(getPedalboardByBundle) .then(getPedalboardByBundle)
.then(function (currentPedalboard) { .then(function (currentPedalboard) {
cache.setCurrentPedalboard(currentPedalboard); cache.setCurrentPedalboard(currentPedalboard);
@ -359,7 +358,7 @@ function setPedalboard(pedalboard) {
return reset() return reset()
}) })
.then(function () { .then(function () {
return util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) return util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
}) })
.then(function () { .then(function () {
cache.setCurrentPedalboard(pedalboard); cache.setCurrentPedalboard(pedalboard);

View file

@ -1,7 +1,6 @@
const logger = require('./logger.js'); const logger = require('./logger.js');
const util = require('./util.js'); const util = require('./util.js');
const commands = require('./commands.js'); const commands = require('./commands.js');
const config = require('../config.json').osc;
const CMD = 'oscsend'; const CMD = 'oscsend';
@ -16,8 +15,8 @@ async function send(controller, channel, value) {
} }
logger.debug('sending value \'' + value + '\' to controller \'' + controller + '\' on channel \'' + channel + '\'...'); logger.debug('sending value \'' + value + '\' to controller \'' + controller + '\' on channel \'' + channel + '\'...');
let args = [ let args = [
config.host, config.port, global.config.osc.host, global.config.osc.port,
config.address, global.config.osc.address,
'm', 'm',
'00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel) '00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel)
]; ];

View file

@ -1,4 +1,3 @@
const config = require('../config.json');
const logger = require('./logger.js'); const logger = require('./logger.js');
const api = require('./api.js'); const api = require('./api.js');
const blinky = require('./blinky.js'); const blinky = require('./blinky.js');
@ -12,8 +11,8 @@ async function start() {
if (!server) { if (!server) {
server = http.createServer(); server = http.createServer();
} }
server.listen(config.server.port, config.server.listen).on('listening', function () { server.listen(global.config.server.port, global.config.server.listen).on('listening', function () {
logger.debug('server listening on ' + config.server.listen + ':' + config.server.port + '...'); logger.debug('server listening on ' + global.config.server.listen + ':' + global.config.server.port + '...');
blinky.setActive(true); blinky.setActive(true);
handleRequests(); handleRequests();
}); });