浏览代码

refine milvus controller;

Signed-off-by: Gitea <zizhao.chen@zilliz.com>
Gitea 3 年之前
父节点
当前提交
88a49cdf9b
共有 3 个文件被更改,包括 102 次插入41 次删除
  1. 17 0
      express/src/milvus/dto.ts
  2. 4 41
      express/src/milvus/index.ts
  3. 81 0
      express/src/milvus/milvus.controller.ts

+ 17 - 0
express/src/milvus/dto.ts

@@ -0,0 +1,17 @@
+import { ArrayMinSize, IsArray, IsString } from "class-validator";
+
+export class ConnectMilvusDto {
+  @IsString()
+  readonly address: string;
+}
+
+export class CheckMilvusDto {
+  @IsString()
+  readonly address: string;
+}
+
+export class FlushDto {
+  @IsArray()
+  @ArrayMinSize(1, { message: "At least need one collection name." })
+  readonly collection_names: string[];
+}

+ 4 - 41
express/src/milvus/index.ts

@@ -1,44 +1,7 @@
-import express from "express";
-import { MilvusService } from "./milvus.service";
+import { MilvusController } from "./milvus.controller";
 
-const router = express.Router();
-
-const milvusService = new MilvusService();
-
-router.post("/connect", async (req, res, next) => {
-  const address = req.body?.address;
-  try {
-    const result = await milvusService.connectMilvus(address);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.get("/check", async (req, res, next) => {
-  const address = "" + req.query?.address;
-  try {
-    const result = await milvusService.checkConnect(address);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.put("/flush", async (req, res, next) => {
-  const collectionNames = req.body;
-  try {
-    const result = await milvusService.flush(collectionNames);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.get("/metrics", async (req, res, next) => {
-  try {
-    const result = await milvusService.getMetrics();
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
+const MilvusManager = new MilvusController();
+const router = MilvusManager.generateRoutes();
+const milvusService = MilvusManager.milvusServiceGetter;
 
 export { router, milvusService };

+ 81 - 0
express/src/milvus/milvus.controller.ts

@@ -0,0 +1,81 @@
+import { NextFunction, Request, Response, Router } from "express";
+import { dtoValidationMiddleware } from "../middlewares/validation";
+import { MilvusService } from "./milvus.service";
+import { CheckMilvusDto, ConnectMilvusDto, FlushDto } from "./dto";
+
+export class MilvusController {
+  private router: Router;
+  private milvusService: MilvusService;
+
+  constructor() {
+    this.milvusService = new MilvusService();
+    this.router = Router();
+  }
+
+  get milvusServiceGetter() {
+    return this.milvusService;
+  }
+
+  generateRoutes() {
+    this.router.post(
+      "/connect",
+      dtoValidationMiddleware(ConnectMilvusDto),
+      this.connectMilvus.bind(this)
+    );
+
+    this.router.get(
+      "/check",
+      dtoValidationMiddleware(CheckMilvusDto),
+      this.checkConnect.bind(this)
+    );
+
+    this.router.put(
+      "/flush",
+      dtoValidationMiddleware(FlushDto),
+      this.flush.bind(this)
+    );
+
+    this.router.get("/metrics", this.getMetrics.bind(this));
+
+    return this.router;
+  }
+
+  async connectMilvus(req: Request, res: Response, next: NextFunction) {
+    const address = req.body?.address;
+    try {
+      const result = await this.milvusService.connectMilvus(address);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async checkConnect(req: Request, res: Response, next: NextFunction) {
+    const address = "" + req.query?.address;
+    try {
+      const result = await this.milvusService.checkConnect(address);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async flush(req: Request, res: Response, next: NextFunction) {
+    const collectionNames = req.body;
+    try {
+      const result = await this.milvusService.flush(collectionNames);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async getMetrics(req: Request, res: Response, next: NextFunction) {
+    try {
+      const result = await this.milvusService.getMetrics();
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+}