Make adding routes private for router

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen committed 2022-03-11 09:11:11 +02:00
1 parent 711516cccf
commit afa9c10969
2 files changed
+10 -14

No files matched your search

+8 -6
View File
@@ -44,6 +44,14 @@ export class Router {
protected router = new Call.Router(); protected router = new Call.Router();
static rootPath = path.resolve(__static); static rootPath = path.resolve(__static);
constructor(routes: Route[]) {
routes.forEach(route => {
this.router.add({ method: route.method, path: route.path }, (request: LensApiRequest) => {
route.handler(request);
});
});
}
public async route(cluster: Cluster, req: http.IncomingMessage, res: http.ServerResponse): Promise<boolean> { public async route(cluster: Cluster, req: http.IncomingMessage, res: http.ServerResponse): Promise<boolean> {
const url = new URL(req.url, "http://localhost"); const url = new URL(req.url, "http://localhost");
const path = url.pathname; const path = url.pathname;
@@ -81,12 +89,6 @@ export class Router {
params, params,
}; };
} }
addRoute = (route: Route) => {
this.router.add({ method: route.method, path: route.path }, (request: LensApiRequest) => {
route.handler(request);
});
};
} }
export interface Route { export interface Route {
+2 -8
View File
@@ -3,7 +3,7 @@
* 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, getInjectionToken } from "@ogre-tools/injectable"; import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import { Router, Route } from "../router"; import { Route, Router } from "../router";
export const routeInjectionToken = getInjectionToken<Route>({ export const routeInjectionToken = getInjectionToken<Route>({
id: "route-injection-token", id: "route-injection-token",
@@ -13,15 +13,9 @@ const routerInjectable = getInjectable({
id: "router", id: "router",
instantiate: (di) => { instantiate: (di) => {
const router = new Router();
const routes = di.injectMany(routeInjectionToken); const routes = di.injectMany(routeInjectionToken);
routes.forEach(route => { return new Router(routes);
router.addRoute(route);
});
return router;
}, },
}); });