fix(striker-ui-api): simplify calls to striker-access-database and /files GETs

main
Tsu-ba-me 3 years ago
parent 0862706ae8
commit 33f783d6b6
  1. 68
      striker-ui-api/lib/accessDB.js
  2. 5
      striker-ui-api/lib/consts/SERVER_PATHS.js
  3. 29
      striker-ui-api/lib/request_handlers/files/buildGetFiles.js
  4. 24
      striker-ui-api/lib/request_handlers/files/getFileDetail.js
  5. 12
      striker-ui-api/lib/request_handlers/files/getFilesOverview.js

@ -0,0 +1,68 @@
const { spawnSync } = require('child_process');
const SERVER_PATHS = require('./consts/SERVER_PATHS');
const execStrikerAccessDatabase = (
args,
options = {
timeout: 10000,
encoding: 'utf-8',
},
) => {
const { error, stdout, stderr } = spawnSync(
SERVER_PATHS.usr.sbin['striker-access-database'].self,
args,
options,
);
if (error) {
throw error;
}
if (stderr) {
throw new Error(stderr);
}
let output;
try {
output = JSON.parse(stdout);
} catch (stdoutParseError) {
output = stdout;
console.warn(
`Failed to parse striker-access-database output [${output}]; error: [${stdoutParseError}]`,
);
}
return {
stdout: output,
};
};
const accessDB = {
query: (query, accessMode, options) => {
const args = ['--query', query];
if (accessMode) {
args.push('--mode', accessMode);
}
return execStrikerAccessDatabase(args, options);
},
sub: (subName, subParams, options) => {
const args = ['--sub', subName];
if (subParams) {
args.push('--sub-params', JSON.stringify(subParams));
}
const { stdout } = execStrikerAccessDatabase(args, options);
return {
stdout: stdout['sub_results'],
};
},
};
module.exports = accessDB;

@ -6,6 +6,11 @@ const SERVER_PATHS = {
incoming: {},
},
},
usr: {
sbin: {
'striker-access-database': {},
},
},
};
const generatePaths = (

@ -0,0 +1,29 @@
const accessDB = require('../../accessDB');
const buildGetFiles = (query) => (request, response) => {
console.log('Calling CLI script to get data.');
let queryStdout;
try {
({ stdout: queryStdout } = accessDB.query(
typeof query === 'function' ? query(request) : query,
));
} catch (queryError) {
console.log(`Query error: ${queryError}`);
response.status(500).send();
}
console.log(
`Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify(
queryStdout,
null,
2,
)}`,
);
response.json(queryStdout);
};
module.exports = buildGetFiles;

@ -0,0 +1,24 @@
const buildGetFiles = require('./buildGetFiles');
const getFileDetail = buildGetFiles(
(request) =>
`SELECT
fil.file_uuid,
fil.file_name,
fil.file_size,
fil.file_type,
fil.file_md5sum,
fil_loc.file_location_uuid,
fil_loc.file_location_active,
anv.anvil_uuid,
anv.anvil_name,
anv.anvil_description
FROM files AS fil
JOIN file_locations AS fil_loc
ON fil.file_uuid = fil_loc.file_location_file_uuid
JOIN anvils AS anv
ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid
WHERE fil.file_uuid = '${request.params.fileUUID}';`,
);
module.exports = getFileDetail;

@ -0,0 +1,12 @@
const buildGetFiles = require('./buildGetFiles');
const getFilesOverview = buildGetFiles(`
SELECT
file_uuid,
file_name,
file_size,
file_type,
file_md5sum
FROM files;`);
module.exports = getFilesOverview;
Loading…
Cancel
Save