1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Make registration of back-end routes happen using injectable

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-02-10 12:58:24 +02:00
parent 772e879b81
commit fc9e9ee2b7
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 28 additions and 5 deletions

View File

@ -145,6 +145,12 @@ export class Router {
} }
} }
addRoute = (route: Route) => {
this.router.add({ method: route.method, path: route.path }, (request: LensApiRequest) => {
route.handler(request);
});
};
protected addRoutes() { protected addRoutes() {
// Static assets // Static assets
if (this.dependencies.httpProxy) { if (this.dependencies.httpProxy) {
@ -195,3 +201,9 @@ export class Router {
this.router.add({ method: "patch", path: `${apiPrefix}/stack` }, ResourceApplierApiRoute.patchResource); this.router.add({ method: "patch", path: `${apiPrefix}/stack` }, ResourceApplierApiRoute.patchResource);
} }
} }
export interface Route {
path: string;
method: "get" | "post" | "put" | "patch" | "delete";
handler: (request: LensApiRequest) => Promise<void>;
}

View File

@ -2,12 +2,15 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import { Router, Route } from "../router";
import routePortForwardInjectable from "../routes/port-forward/route-port-forward/route-port-forward.injectable";
import isDevelopmentInjectable from "../../common/vars/is-development.injectable"; import isDevelopmentInjectable from "../../common/vars/is-development.injectable";
import httpProxy from "http-proxy"; import httpProxy from "http-proxy";
import { Router } from "../router";
import routePortForwardInjectable export const routeInjectionToken = getInjectionToken<Route>({
from "../routes/port-forward/route-port-forward/route-port-forward.injectable"; id: "route-injection-token",
});
const routerInjectable = getInjectable({ const routerInjectable = getInjectable({
id: "router", id: "router",
@ -16,10 +19,18 @@ const routerInjectable = getInjectable({
const isDevelopment = di.inject(isDevelopmentInjectable); const isDevelopment = di.inject(isDevelopmentInjectable);
const proxy = isDevelopment ? httpProxy.createProxy() : undefined; const proxy = isDevelopment ? httpProxy.createProxy() : undefined;
return new Router({ const router = new Router({
routePortForward: di.inject(routePortForwardInjectable), routePortForward: di.inject(routePortForwardInjectable),
httpProxy: proxy, httpProxy: proxy,
}); });
const routes = di.injectMany(routeInjectionToken);
routes.forEach(route => {
router.addRoute(route);
});
return router;
}, },
}); });