diff --git a/striker-ui-api/src/app.ts b/striker-ui-api/src/app.ts index 1bb6396d..617bb09e 100644 --- a/striker-ui-api/src/app.ts +++ b/striker-ui-api/src/app.ts @@ -1,18 +1,15 @@ import cors from 'cors'; import express from 'express'; -import path from 'path'; - -import API_ROOT_PATH from './lib/consts/API_ROOT_PATH'; import routes from './routes'; +import { rrouters } from './lib/rrouters'; const app = express(); app.use(express.json()); app.use(cors()); -Object.entries(routes).forEach(([route, router]) => { - app.use(path.join(API_ROOT_PATH, route), router); -}); +rrouters(app, routes, { key: 'api' }); +rrouters(app, routes, { key: 'echo' }); export default app; diff --git a/striker-ui-api/src/lib/consts/API_ROOT_PATH.ts b/striker-ui-api/src/lib/consts/API_ROOT_PATH.ts deleted file mode 100644 index fd320c2d..00000000 --- a/striker-ui-api/src/lib/consts/API_ROOT_PATH.ts +++ /dev/null @@ -1,3 +0,0 @@ -const API_ROOT_PATH = '/api'; - -export default API_ROOT_PATH; diff --git a/striker-ui-api/src/lib/rrouters.ts b/striker-ui-api/src/lib/rrouters.ts new file mode 100644 index 00000000..deb76955 --- /dev/null +++ b/striker-ui-api/src/lib/rrouters.ts @@ -0,0 +1,25 @@ +import { Application, Router } from 'express'; +import path from 'path'; + +import { stdout } from './shell'; + +export const rrouters = < + A extends Application, + M extends MapToRouter, + R extends Router, +>( + app: A, + union: Readonly | R, + { key, route = '/' }: { key?: string; route?: string } = {}, +) => { + if ('route' in union) { + stdout(`Setting up route ${route}`); + app.use(route, union as R); + } else if (key) { + rrouters(app, union[key], { route: path.posix.join(route, key) }); + } else { + Object.entries(union).forEach(([extend, subunion]) => { + rrouters(app, subunion, { route: path.posix.join(route, extend) }); + }); + } +}; diff --git a/striker-ui-api/src/routes/index.ts b/striker-ui-api/src/routes/index.ts index ef703777..6566265e 100644 --- a/striker-ui-api/src/routes/index.ts +++ b/striker-ui-api/src/routes/index.ts @@ -1,5 +1,3 @@ -import { Router } from 'express'; - import anvilRouter from './anvil'; import commandRouter from './command'; import echoRouter from './echo'; @@ -14,20 +12,22 @@ import sshKeyRouter from './ssh-key'; import upsRouter from './ups'; import userRouter from './user'; -const routes: Readonly> = { - anvil: anvilRouter, - command: commandRouter, +const routes = { + api: { + anvil: anvilRouter, + command: commandRouter, + fence: fenceRouter, + file: fileRouter, + host: hostRouter, + job: jobRouter, + manifest: manifestRouter, + 'network-interface': networkInterfaceRouter, + server: serverRouter, + 'ssh-key': sshKeyRouter, + ups: upsRouter, + user: userRouter, + }, echo: echoRouter, - fence: fenceRouter, - file: fileRouter, - host: hostRouter, - job: jobRouter, - manifest: manifestRouter, - 'network-interface': networkInterfaceRouter, - server: serverRouter, - 'ssh-key': sshKeyRouter, - ups: upsRouter, - user: userRouter, }; export default routes; diff --git a/striker-ui-api/src/types/MapToRouter.d.ts b/striker-ui-api/src/types/MapToRouter.d.ts new file mode 100644 index 00000000..83137d60 --- /dev/null +++ b/striker-ui-api/src/types/MapToRouter.d.ts @@ -0,0 +1,3 @@ +type MapToRouter = { + [uri: string]: MapToRouter | import('express').Router; +};