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 API_ROOT_PATH from './lib/consts/API_ROOT_PATH';
import { echoRouter, filesRouter, serversRouter } from './routes'; import routes from './routes';
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
app.use(cors()); app.use(cors());
app.use(path.join(API_ROOT_PATH, 'echo'), echoRouter); Object.entries(routes).forEach(([route, router]) => {
app.use(path.join(API_ROOT_PATH, 'files'), filesRouter); app.use(path.join(API_ROOT_PATH, route), router);
app.use(path.join(API_ROOT_PATH, 'servers'), serversRouter); });
export default app; 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 = ( const join = (
elements: string[] | undefined, elements: string[] | string | undefined,
{ beforeReturn, elementWrapper = '', separator = '' }: JoinOptions, { beforeReturn, elementWrapper = '', separator = '' }: JoinOptions = {},
) => { ) => {
const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`; const joinSeparator = `${elementWrapper}${separator}${elementWrapper}`;
@ -9,7 +11,10 @@ const join = (
? `${elementWrapper}${elements.join(joinSeparator)}${elementWrapper}` ? `${elementWrapper}${elements.join(joinSeparator)}${elementWrapper}`
: undefined; : undefined;
return typeof beforeReturn === 'function' ? beforeReturn(toReturn) : toReturn; return call<string | undefined>(beforeReturn, {
parameters: [toReturn],
notCallableReturn: toReturn,
});
}; };
export default join; 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 { Request, Response } from 'express';
import { dbQuery } from '../accessDB'; import { dbQuery } from '../accessDB';
import call from '../call';
const buildGetRequestHandler = const buildGetRequestHandler =
(query: string | ((request: Request) => string)) => (
query: string | BuildQueryFunction,
{ beforeRespond }: BuildGetRequestHandlerOptions = {},
) =>
(request: Request, response: Response) => { (request: Request, response: Response) => {
console.log('Calling CLI script to get data.'); console.log('Calling CLI script to get data.');
const buildQueryOptions: BuildQueryOptions = {};
let queryStdout; let queryStdout;
try { try {
({ stdout: queryStdout } = dbQuery( ({ stdout: queryStdout } = dbQuery(
typeof query === 'function' ? query(request) : query, call<string>(query, {
parameters: [request, buildQueryOptions],
notCallableReturn: query,
}),
)); ));
} catch (queryError) { } catch (queryError) {
console.log(`Query error: ${queryError}`); console.log(`Query error: ${queryError}`);
@ -19,6 +28,18 @@ const buildGetRequestHandler =
response.status(500).send(); response.status(500).send();
} }
const { afterQueryReturn } = buildQueryOptions;
queryStdout = call(afterQueryReturn, {
parameters: [queryStdout],
notCallableReturn: queryStdout,
});
queryStdout = call(beforeRespond, {
parameters: [queryStdout],
notCallableReturn: queryStdout,
});
console.log( console.log(
`Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify( `Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify(
queryStdout, queryStdout,

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

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

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

@ -1,7 +1,7 @@
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import join from '../../join'; import join from '../../join';
const getServers = buildGetRequestHandler((request) => { const getServer = buildGetRequestHandler((request) => {
const { anvilsUUID } = request.body; const { anvilsUUID } = request.body;
const condAnvilsUUID = join(anvilsUUID, { const condAnvilsUUID = join(anvilsUUID, {
@ -24,4 +24,4 @@ const getServers = buildGetRequestHandler((request) => {
${condAnvilsUUID};`; ${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, dbSubRefreshTimestamp,
dbWrite, dbWrite,
} from '../lib/accessDB'; } from '../lib/accessDB';
import getFiles from '../lib/request_handlers/files/getFiles'; import getFile from '../lib/request_handlers/file/getFile';
import getFileDetail from '../lib/request_handlers/files/getFileDetail'; import getFileDetail from '../lib/request_handlers/file/getFileDetail';
import uploadSharedFiles from '../middlewares/uploadSharedFiles'; import uploadSharedFiles from '../middlewares/uploadSharedFiles';
const router = express.Router(); const router = express.Router();
@ -37,7 +37,7 @@ router
response.status(204).send(); response.status(204).send();
}) })
.get('/', getFiles) .get('/', getFile)
.get('/:fileUUID', getFileDetail) .get('/:fileUUID', getFileDetail)
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => { .post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => {
console.log('Receiving shared file.'); console.log('Receiving shared file.');

@ -1,5 +1,15 @@
import { Router } from 'express';
import anvilRouter from './anvil';
import echoRouter from './echo'; import echoRouter from './echo';
import filesRouter from './files'; import fileRouter from './file';
import serversRouter from './servers'; 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