From 186d69dfc63f1a536b750864d3717d4d1fc34129 Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 11 Jul 2020 16:42:36 +0300 Subject: [PATCH] router refactoring / added more types Signed-off-by: Roman --- src/main/router.ts | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/router.ts b/src/main/router.ts index 2bab4253fc..f6dd26cd48 100644 --- a/src/main/router.ts +++ b/src/main/router.ts @@ -2,25 +2,35 @@ import Call from "@hapi/call" import Subtext from "@hapi/subtext" import http from "http" import path from "path" -import { readFile } from "fs-extra" +import { readFile, stat } from "fs-extra" import { Cluster } from "./cluster" import { helmApi } from "./helm-api" import { resourceApplierApi } from "./resource-applier-api" import { apiPrefix, appName, outDir } from "../common/vars"; import { configRoute, kubeconfigRoute, metricsRoute, portForwardRoute, watchRoute } from "./routes"; -export interface RouterRequestOpts

= any> { +export interface RouterRequestOpts { req: http.IncomingMessage; res: http.ServerResponse; cluster: Cluster; + params: RouteParams; url: URL; - params: P; // https://hapi.dev/module/call/api } -export interface LensApiRequest { +export interface RouteParams extends Record { + path?: string; // *-route + namespace?: string; + service?: string; + account?: string; + release?: string; + repo?: string; + chart?: string; +} + +export interface LensApiRequest

{ path: string; - payload: D; - params: P; + payload: P; + params: RouteParams; cluster: Cluster; response: http.ServerResponse; query: URLSearchParams; @@ -89,22 +99,21 @@ export class Router { protected async handleStaticFile(filePath: string, response: http.ServerResponse) { const asset = path.resolve(outDir, filePath); - try { + const info = await stat(asset); + if (info.isFile()) { const data = await readFile(asset); response.setHeader("Content-Type", this.getMimeType(asset)); response.write(data) response.end() - } catch (err) { - this.handleStaticFile(`${appName}.html`, response) + } else { + this.handleStaticFile(`${appName}.html`, response); } } protected addRoutes() { // Static assets - this.router.add({ method: 'get', path: '/{path*}' }, (request: LensApiRequest) => { - const { response, params } = request - const file = params.path || "/index.html" - this.handleStaticFile(file, response) + this.router.add({ method: 'get', path: '/{path*}' }, ({ params, response }: LensApiRequest) => { + this.handleStaticFile(params.path, response); }) this.router.add({ method: "get", path: `${apiPrefix}/config` }, configRoute.routeConfig.bind(configRoute))