fixed stuff, implemented new endpoint '/info'

This commit is contained in:
Daniel Sommer 2022-02-10 20:52:02 +01:00
parent c1151602b4
commit 2ca7834ba0
6 changed files with 85 additions and 32 deletions

View file

@ -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 |
| - | - | - |

View file

@ -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();

View file

@ -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
}

View file

@ -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';

75
libs/info.js Normal file
View file

@ -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
}

View file

@ -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) {