Skip to content

Commit

Permalink
fix: 处理path
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Jan 7, 2024
1 parent feb8d74 commit 3a81df5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/router/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* @Usage:
* @Author: richen
* @Date: 2021-06-29 14:10:30
* @LastEditTime: 2024-01-04 23:09:48
* @LastEditTime: 2024-01-07 22:38:51
*/
import * as Helper from "koatty_lib";
import { RouterOptions } from "./router";
import { parsePath } from "../utils/path";
import { IOCContainer } from "koatty_container";
import { ListServices, LoadProto } from "koatty_proto";
import { DefaultLogger as Logger } from "koatty_logger";
Expand Down Expand Up @@ -155,10 +156,7 @@ export class GrpcRouter implements KoattyRouter {
}
const impl: { [key: string]: UntypedHandleCall } = {};
for (const handler of si.handlers) {
let path = handler.path || "/";
if (path.length > 1 && path.endsWith("/")) {
path = path.slice(0, path.length - 1);
}
const path = parsePath(handler.path);
if (ctls[path]) {
const ctlItem = ctls[path];
Logger.Debug(`Register request mapping: ["${path}" => ${ctlItem.name}.${ctlItem.method}]`);
Expand Down
8 changes: 3 additions & 5 deletions src/router/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* @Usage:
* @Author: richen
* @Date: 2021-06-28 19:02:06
* @LastEditTime: 2024-01-04 23:09:35
* @LastEditTime: 2024-01-07 22:39:54
*/
import KoaRouter from "@koa/router";
import * as Helper from "koatty_lib";
import { RouterOptions } from "./router";
import { parsePath } from "../utils/path";
import { IOCContainer } from "koatty_container";
import { DefaultLogger as Logger } from "koatty_logger";
import { Handler, injectParamMetaData, injectRouter } from "./inject";
Expand Down Expand Up @@ -75,10 +76,7 @@ export class HttpRouter implements KoattyRouter {
for (const it in ctlRouters) {
const router = ctlRouters[it];
const method = router.method;
let path = router.path || "/";
if (path.length > 1 && path.endsWith("/")) {
path = path.slice(0, path.length - 1);
}
const path = parsePath(router.path);
const requestMethod = <RequestMethod>router.requestMethod;
const params = ctlParams[method];
Logger.Debug(`Register request mapping: ["${path}" => ${n}.${method}]`);
Expand Down
8 changes: 3 additions & 5 deletions src/router/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2021-06-29 14:16:44
* @LastEditTime: 2024-01-04 23:09:52
* @LastEditTime: 2024-01-07 22:40:30
*/

import KoaRouter from "@koa/router";
Expand All @@ -14,6 +14,7 @@ import { DefaultLogger as Logger } from "koatty_logger";
import { Handler, injectParamMetaData, injectRouter } from "./inject";
import { Koatty, KoattyContext, KoattyNext, KoattyRouter } from "koatty_core";
import { Helper } from "koatty_lib";
import { parsePath } from "../utils/path";

/**
* WebsocketRouter Options
Expand Down Expand Up @@ -84,10 +85,7 @@ export class WebsocketRouter implements KoattyRouter {
for (const it in ctlRouters) {
const router = ctlRouters[it];
const method = router.method;
let path = router.path || "/";
if (path.length > 1 && path.endsWith("/")) {
path = path.slice(0, path.length - 1);
}
const path = parsePath(router.path);
const requestMethod = <RequestMethod>router.requestMethod;
const params = ctlParams[method];
// websocket only handler get request
Expand Down
21 changes: 21 additions & 0 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @Description:
* @Usage:
* @Author: richen
* @Date: 2024-01-07 22:33:25
* @LastEditTime: 2024-01-07 22:39:31
* @License: BSD (3-Clause)
* @Copyright (c): <richenlin(at)gmail.com>
*/
/**
* @description:
* @param {string} path
* @return {*}
*/
export function parsePath(opath: string): string {
let path = opath || "/";
if (path.length > 1 && path.endsWith("/")) {
path = path.slice(0, path.length - 1);
}
return path.replace('//', '/');
}

0 comments on commit 3a81df5

Please sign in to comment.