From 2ca7834ba02bdedce2071ee407a12203bde84591 Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 10 Feb 2022 20:52:02 +0100 Subject: [PATCH] fixed stuff, implemented new endpoint '/info' --- README.md | 5 ++++ libs/api.js | 2 ++ libs/commands.js | 27 ----------------- libs/constants.js | 1 + libs/info.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++ libs/modep.js | 7 ++--- 6 files changed, 85 insertions(+), 32 deletions(-) delete mode 100644 libs/commands.js create mode 100644 libs/info.js diff --git a/README.md b/README.md index 22f513b..4271c7b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,11 @@ control your MODEP pedalboard(s) via http requests ## api +### info +| url | GET | POST | +| - | - | - | +| /info | get information about the host | | + ### banks | url | GET | POST | | - | - | - | diff --git a/libs/api.js b/libs/api.js index 52b6682..532f0bf 100644 --- a/libs/api.js +++ b/libs/api.js @@ -2,6 +2,7 @@ const logger = require('./logger.js'); const util = require('./util.js'); const constants = require('./constants.js'); const modep = require('./modep.js'); +const info = require('./info.js'); const endpoints = new Map(); @@ -78,6 +79,7 @@ function setupEndpoints() { ); } }) + .then(setEndpoint(constants.API_INFO, { method: info.getHostInfo })) .then(function () { logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms'); resolve(); diff --git a/libs/commands.js b/libs/commands.js deleted file mode 100644 index 2631a9c..0000000 --- a/libs/commands.js +++ /dev/null @@ -1,27 +0,0 @@ -const logger = require('../libs/logger.js'); -const { spawn } = require('child_process') - -function execute(endpoint) { - if (!endpoint || !endpoint.command) { - logger.warn('no command defined'); - return; - } - logger.debug('executing command \'' + endpoint.command + '\' with args \'' + endpoint.args + '\'...'); - var cmd = spawn(endpoint.command, endpoint.args); - cmd.stdout.on('data', function(data) { - logger.debug(data); - }); - cmd.stderr.on('data', function(data) { - logger.error(data); - }); - cmd.on('close', function(code) { - logger.debug('command \'' + endpoint.command + '\' with args \'' + endpoint.args + '\' finished with exit code ' + code); - }); - cmd.on('error', function(err) { - logger.error('command \'' + endpoint.command + '\' with args \'' + endpoint.args + '\' encountered an error >>> ' + err); - }); -} - -module.exports = { - execute -} \ No newline at end of file diff --git a/libs/constants.js b/libs/constants.js index 8b40d4f..7606088 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -1,6 +1,7 @@ exports.HTTP_GET = 'GET'; exports.HTTP_POST = 'POST'; +exports.API_INFO = '/info' exports.API_BANKS = '/banks'; exports.API_PEDALBOARDS = '/pedalboards'; exports.API_PEDALBOARDS_DEFAULT = '/pedalboards/default'; diff --git a/libs/info.js b/libs/info.js new file mode 100644 index 0000000..2d6e053 --- /dev/null +++ b/libs/info.js @@ -0,0 +1,75 @@ +const logger = require('./logger.js'); +const os = require('os'); + +function getHostname() { + return os.hostname(); +} + +function getUptime() { + return os.uptime(); +} + +function getLoadAverage(index) { + var loadAverage = os.loadavg(); + if (index <= loadAverage.length) { + return os[index]; + } + return os.loadavg(); +} + +function getArchitecture() { + return os.arch; +} + +function getOperatingSystem() { + return os.release(); +} + +function getPlatform() { + return os.platform(); +} + +function getKernelVersion() { + return os.version(); +} + +function getCpuInfo() { + return os.cpus(); +} + +function getMemoryInfo() { + var free = os.freemem(); + var total = os.totalmem(); + return { + total: total, + free: free, + used: total - free + } +} + +function getHostInfo() { + return { + hostname: getHostname(), + uptime: getUptime(), + load: getLoadAverage(), + arch: getArchitecture(), + os: getOperatingSystem(), + platform: getPlatform(), + kernel: getKernelVersion(), + cpu: getCpuInfo(), + memory: getMemoryInfo() + } +} + +module.exports = { + getHostname, + getUptime, + getLoadAverage, + getArchitecture, + getOperatingSystem, + getPlatform, + getKernelVersion, + getCpuInfo, + getMemoryInfo, + getHostInfo +} \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index 0644df4..c31422c 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -5,12 +5,9 @@ const logger = require('./logger.js'); const cache = require('./cache.js'); const path = require('path'); const fs = require('fs'); -const { spawn } = require('child_process') +const { spawn } = require('child_process'); const ttl2jsonld = require('@frogcat/ttl2jsonld').parse; -// TODO: MOVE TO CACHE.JS - - function reset() { return new Promise(function (resolve, reject) { util.httpGET(config.modep.host, config.modep.port, '/reset') @@ -247,7 +244,7 @@ function parseCurrentPedalboard(currentPedalboard) { reject('error: could not determine current pedalboard config file'); } // FAKE DATA - var file = path.resolve('./dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1)); + var file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1)); // var file = path.resolve(currentPedalboard.uri.replace('file://', '')); fs.readFile(file, function (err, data) { if (err) {