From 18a02a68ffeb1e6a623b0d16a4d5bc17895e7b9e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 1 Dec 2021 17:08:36 -0500 Subject: [PATCH] feat(striker-ui-api): add files route --- striker-ui-api/app.js | 6 ++++-- striker-ui-api/routes/files.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 striker-ui-api/routes/files.js diff --git a/striker-ui-api/app.js b/striker-ui-api/app.js index bac333f8..c7695c05 100644 --- a/striker-ui-api/app.js +++ b/striker-ui-api/app.js @@ -3,12 +3,14 @@ const path = require('path'); const API_ROOT_PATH = require('./lib/consts/API_ROOT_PATH'); -const echoRoute = require('./routes/echo'); +const echoRouter = require('./routes/echo'); +const filesRouter = require('./routes/files'); const app = express(); app.use(express.json()); -app.use(path.join(API_ROOT_PATH, 'echo'), echoRoute); +app.use(path.join(API_ROOT_PATH, 'echo'), echoRouter); +app.use(path.join(API_ROOT_PATH, 'files'), filesRouter); module.exports = app; diff --git a/striker-ui-api/routes/files.js b/striker-ui-api/routes/files.js new file mode 100644 index 00000000..c83cfa83 --- /dev/null +++ b/striker-ui-api/routes/files.js @@ -0,0 +1,30 @@ +const express = require('express'); +const { spawnSync } = require('child_process'); + +const router = express.Router(); + +router.get('/', (request, response) => { + console.log('Calling CLI script to get data.'); + + const childProcess = spawnSync( + 'striker-access-database', + ['--query', 'SELECT * FROM files;'], + { + timeout: 10000, + encoding: 'utf-8', + }, + ); + + if (childProcess.error) + { + response.status(500); + } + + console.log('error:', childProcess.error); + console.log('stdout:', childProcess.stdout); + console.log('stderr:', childProcess.stderr); + + response.status(200).send(childProcess.stdout); +}); + +module.exports = router;