feat(striker-ui-api): add /anvil route and migrate to singular routes

main
Tsu-ba-me 3 years ago
parent c352fc5f0e
commit de8d2ddd50
  1. 8
      striker-ui-api/src/app.ts
  2. 9
      striker-ui-api/src/lib/call.ts
  3. 11
      striker-ui-api/src/lib/join.ts
  4. 54
      striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts
  5. 25
      striker-ui-api/src/lib/request_handlers/buildGetRequestHandler.ts
  6. 8
      striker-ui-api/src/lib/request_handlers/file/buildQueryFileDetail.ts
  7. 8
      striker-ui-api/src/lib/request_handlers/file/getFile.ts
  8. 4
      striker-ui-api/src/lib/request_handlers/file/getFileDetail.ts
  9. 4
      striker-ui-api/src/lib/request_handlers/server/getServer.ts
  10. 9
      striker-ui-api/src/routes/anvil.ts
  11. 6
      striker-ui-api/src/routes/file.ts
  12. 16
      striker-ui-api/src/routes/index.ts
  13. 9
      striker-ui-api/src/routes/server.ts
  14. 9
      striker-ui-api/src/routes/servers.ts
  15. 8
      striker-ui-api/src/types/AnvilOverview.d.ts
  16. 3
      striker-ui-api/src/types/BuildGetRequestHandlerOptions.d.ts
  17. 8
      striker-ui-api/src/types/BuildQueryFunction.d.ts
  18. 4
      striker-ui-api/src/types/CallOptions.d.ts

@ -4,15 +4,15 @@ import path from 'path';
import API_ROOT_PATH from './lib/consts/API_ROOT_PATH';
import { echoRouter, filesRouter, serversRouter } from './routes';
import routes from './routes';
const app = express();
app.use(express.json());
app.use(cors());
app.use(path.join(API_ROOT_PATH, 'echo'), echoRouter);
app.use(path.join(API_ROOT_PATH, 'files'), filesRouter);
app.use(path.join(API_ROOT_PATH, 'servers'), serversRouter);
Object.entries(routes).forEach(([route, router]) => {
app.use(path.join(API_ROOT_PATH, route), router);
});
export default app;

@ -0,0 +1,9 @@
const call = <T = unknown>(
toCall: unknown,
{ parameters = [], notCallableReturn }: CallOptions = {},
) =>
(typeof toCall === 'function'
? toCall(...parameters)
: notCallableReturn) as T;
export default call;

@ -1,6 +1,8 @@
import call from './call';
const join = (
elements: string[] | undefined,
{ beforeReturn, elementWrapper = '', separator = '' }: JoinOptions,
elements: string[] | string | undefined,
{ beforeReturn, elementWrapper = '', separator = '' }: JoinOptions = {},
) => {
const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`;
@ -9,7 +11,10 @@ const join = (
? `${elementWrapper}${elements.join(joinSeparator)}${elementWrapper}`
: undefined;
return typeof beforeReturn === 'function' ? beforeReturn(toReturn) : toReturn;
return call<string | undefined>(beforeReturn, {
parameters: [toReturn],
notCallableReturn: toReturn,
});
};
export default join;

@ -0,0 +1,54 @@
import buildGetRequestHandler from '../buildGetRequestHandler';
const getAnvil = buildGetRequestHandler((request, options) => {
const { anvilsUUID } = request.body;
let query = `
SELECT
anv.anvil_name,
anv.anvil_uuid,
hos.host_name,
hos.host_uuid
FROM anvils AS anv
JOIN hosts AS hos
ON anv.anvil_uuid = hos.host_anvil_uuid;`;
if (options) {
options.afterQueryReturn = (queryStdout) => {
let results = queryStdout;
if (queryStdout instanceof Array) {
let rowStage: AnvilOverview;
results = queryStdout.reduce<AnvilOverview[]>(
(reducedRows, [anvilName, anvilUUID, hostName, hostUUID]) => {
if (rowStage && anvilUUID === rowStage.anvilUUID) {
rowStage.hosts.push({ hostName, hostUUID });
} else {
rowStage = {
anvilName,
anvilUUID,
hosts: [],
};
reducedRows.push(rowStage);
}
return reducedRows;
},
[],
);
}
return results;
};
}
if (anvilsUUID) {
query = 'SELECT * FROM anvils;';
}
return query;
});
export default getAnvil;

@ -1,17 +1,26 @@
import { Request, Response } from 'express';
import { dbQuery } from '../accessDB';
import call from '../call';
const buildGetRequestHandler =
(query: string | ((request: Request) => string)) =>
(
query: string | BuildQueryFunction,
{ beforeRespond }: BuildGetRequestHandlerOptions = {},
) =>
(request: Request, response: Response) => {
console.log('Calling CLI script to get data.');
const buildQueryOptions: BuildQueryOptions = {};
let queryStdout;
try {
({ stdout: queryStdout } = dbQuery(
typeof query === 'function' ? query(request) : query,
call<string>(query, {
parameters: [request, buildQueryOptions],
notCallableReturn: query,
}),
));
} catch (queryError) {
console.log(`Query error: ${queryError}`);
@ -19,6 +28,18 @@ const buildGetRequestHandler =
response.status(500).send();
}
const { afterQueryReturn } = buildQueryOptions;
queryStdout = call(afterQueryReturn, {
parameters: [queryStdout],
notCallableReturn: queryStdout,
});
queryStdout = call(beforeRespond, {
parameters: [queryStdout],
notCallableReturn: queryStdout,
});
console.log(
`Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify(
queryStdout,

@ -1,6 +1,10 @@
import join from '../../join';
const buildQueryFilesDetail = ({ filesUUID }: { filesUUID?: string[] }) => {
const buildQueryFileDetail = ({
filesUUID,
}: {
filesUUID?: string[] | '*';
}) => {
const condFilesUUID = join(filesUUID, {
beforeReturn: (toReturn) =>
toReturn ? `AND fil.file_uuid IN (${toReturn})` : '',
@ -31,4 +35,4 @@ const buildQueryFilesDetail = ({ filesUUID }: { filesUUID?: string[] }) => {
${condFilesUUID};`;
};
export default buildQueryFilesDetail;
export default buildQueryFileDetail;

@ -1,7 +1,7 @@
import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryFilesDetail from './buildQueryFilesDetail';
import buildQueryFileDetail from './buildQueryFileDetail';
const getFiles = buildGetRequestHandler((request) => {
const getFile = buildGetRequestHandler((request) => {
const { filesUUID } = request.body;
let query = `
@ -15,10 +15,10 @@ const getFiles = buildGetRequestHandler((request) => {
WHERE file_type != 'DELETED';`;
if (filesUUID) {
query = buildQueryFilesDetail({ filesUUID });
query = buildQueryFileDetail({ filesUUID });
}
return query;
});
export default getFiles;
export default getFile;

@ -1,8 +1,8 @@
import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryFilesDetail from './buildQueryFilesDetail';
import buildQueryFileDetail from './buildQueryFileDetail';
const getFileDetail = buildGetRequestHandler((request) =>
buildQueryFilesDetail({ filesUUID: [request.params.fileUUID] }),
buildQueryFileDetail({ filesUUID: [request.params.fileUUID] }),
);
export default getFileDetail;

@ -1,7 +1,7 @@
import buildGetRequestHandler from '../buildGetRequestHandler';
import join from '../../join';
const getServers = buildGetRequestHandler((request) => {
const getServer = buildGetRequestHandler((request) => {
const { anvilsUUID } = request.body;
const condAnvilsUUID = join(anvilsUUID, {
@ -24,4 +24,4 @@ const getServers = buildGetRequestHandler((request) => {
${condAnvilsUUID};`;
});
export default getServers;
export default getServer;

@ -0,0 +1,9 @@
import express from 'express';
import getAnvil from '../lib/request_handlers/anvil/getAnvil';
const router = express.Router();
router.get('/', getAnvil);
export default router;

@ -6,8 +6,8 @@ import {
dbSubRefreshTimestamp,
dbWrite,
} from '../lib/accessDB';
import getFiles from '../lib/request_handlers/files/getFiles';
import getFileDetail from '../lib/request_handlers/files/getFileDetail';
import getFile from '../lib/request_handlers/file/getFile';
import getFileDetail from '../lib/request_handlers/file/getFileDetail';
import uploadSharedFiles from '../middlewares/uploadSharedFiles';
const router = express.Router();
@ -37,7 +37,7 @@ router
response.status(204).send();
})
.get('/', getFiles)
.get('/', getFile)
.get('/:fileUUID', getFileDetail)
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => {
console.log('Receiving shared file.');

@ -1,5 +1,15 @@
import { Router } from 'express';
import anvilRouter from './anvil';
import echoRouter from './echo';
import filesRouter from './files';
import serversRouter from './servers';
import fileRouter from './file';
import serverRouter from './server';
const routes: Readonly<Record<string, Router>> = {
anvil: anvilRouter,
echo: echoRouter,
file: fileRouter,
server: serverRouter,
};
export { echoRouter, filesRouter, serversRouter };
export default routes;

@ -0,0 +1,9 @@
import express from 'express';
import getServer from '../lib/request_handlers/server/getServer';
const router = express.Router();
router.get('/', getServer);
export default router;

@ -1,9 +0,0 @@
import express from 'express';
import getServers from '../lib/request_handlers/servers/getServers';
const router = express.Router();
router.get('/', getServers);
export default router;

@ -0,0 +1,8 @@
type AnvilOverview = {
anvilName: string;
anvilUUID: string;
hosts: Array<{
hostName: string;
hostUUID: string;
}>;
};

@ -0,0 +1,3 @@
type BuildGetRequestHandlerOptions = {
beforeRespond?: (queryStdout: unknown) => unknown;
};

@ -0,0 +1,8 @@
type BuildQueryOptions = {
afterQueryReturn?: (queryStdout: unknown) => unknown;
};
type BuildQueryFunction = (
request: import('express').Request,
options?: BuildQueryOptions,
) => string;

@ -0,0 +1,4 @@
type CallOptions = {
parameters?: unknown[];
notCallableReturn?: unknown;
};
Loading…
Cancel
Save