mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
router refactoring / added more types
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
5eca8bc01c
commit
186d69dfc6
@ -2,25 +2,35 @@ import Call from "@hapi/call"
|
|||||||
import Subtext from "@hapi/subtext"
|
import Subtext from "@hapi/subtext"
|
||||||
import http from "http"
|
import http from "http"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { readFile } from "fs-extra"
|
import { readFile, stat } from "fs-extra"
|
||||||
import { Cluster } from "./cluster"
|
import { Cluster } from "./cluster"
|
||||||
import { helmApi } from "./helm-api"
|
import { helmApi } from "./helm-api"
|
||||||
import { resourceApplierApi } from "./resource-applier-api"
|
import { resourceApplierApi } from "./resource-applier-api"
|
||||||
import { apiPrefix, appName, outDir } from "../common/vars";
|
import { apiPrefix, appName, outDir } from "../common/vars";
|
||||||
import { configRoute, kubeconfigRoute, metricsRoute, portForwardRoute, watchRoute } from "./routes";
|
import { configRoute, kubeconfigRoute, metricsRoute, portForwardRoute, watchRoute } from "./routes";
|
||||||
|
|
||||||
export interface RouterRequestOpts<P extends Record<string, string> = any> {
|
export interface RouterRequestOpts {
|
||||||
req: http.IncomingMessage;
|
req: http.IncomingMessage;
|
||||||
res: http.ServerResponse;
|
res: http.ServerResponse;
|
||||||
cluster: Cluster;
|
cluster: Cluster;
|
||||||
|
params: RouteParams;
|
||||||
url: URL;
|
url: URL;
|
||||||
params: P; // https://hapi.dev/module/call/api
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LensApiRequest<D = any, P = any> {
|
export interface RouteParams extends Record<string, string> {
|
||||||
|
path?: string; // *-route
|
||||||
|
namespace?: string;
|
||||||
|
service?: string;
|
||||||
|
account?: string;
|
||||||
|
release?: string;
|
||||||
|
repo?: string;
|
||||||
|
chart?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LensApiRequest<P = any> {
|
||||||
path: string;
|
path: string;
|
||||||
payload: D;
|
payload: P;
|
||||||
params: P;
|
params: RouteParams;
|
||||||
cluster: Cluster;
|
cluster: Cluster;
|
||||||
response: http.ServerResponse;
|
response: http.ServerResponse;
|
||||||
query: URLSearchParams;
|
query: URLSearchParams;
|
||||||
@ -89,22 +99,21 @@ export class Router {
|
|||||||
|
|
||||||
protected async handleStaticFile(filePath: string, response: http.ServerResponse) {
|
protected async handleStaticFile(filePath: string, response: http.ServerResponse) {
|
||||||
const asset = path.resolve(outDir, filePath);
|
const asset = path.resolve(outDir, filePath);
|
||||||
try {
|
const info = await stat(asset);
|
||||||
|
if (info.isFile()) {
|
||||||
const data = await readFile(asset);
|
const data = await readFile(asset);
|
||||||
response.setHeader("Content-Type", this.getMimeType(asset));
|
response.setHeader("Content-Type", this.getMimeType(asset));
|
||||||
response.write(data)
|
response.write(data)
|
||||||
response.end()
|
response.end()
|
||||||
} catch (err) {
|
} else {
|
||||||
this.handleStaticFile(`${appName}.html`, response)
|
this.handleStaticFile(`${appName}.html`, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected addRoutes() {
|
protected addRoutes() {
|
||||||
// Static assets
|
// Static assets
|
||||||
this.router.add({ method: 'get', path: '/{path*}' }, (request: LensApiRequest) => {
|
this.router.add({ method: 'get', path: '/{path*}' }, ({ params, response }: LensApiRequest) => {
|
||||||
const { response, params } = request
|
this.handleStaticFile(params.path, response);
|
||||||
const file = params.path || "/index.html"
|
|
||||||
this.handleStaticFile(file, response)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.router.add({ method: "get", path: `${apiPrefix}/config` }, configRoute.routeConfig.bind(configRoute))
|
this.router.add({ method: "get", path: `${apiPrefix}/config` }, configRoute.routeConfig.bind(configRoute))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user