瀏覽代碼

milvus server

nameczz 4 年之前
父節點
當前提交
3ee750623d

+ 2 - 0
server/package.json

@@ -24,6 +24,8 @@
     "@nestjs/common": "^7.6.15",
     "@nestjs/core": "^7.6.15",
     "@nestjs/platform-express": "^7.6.15",
+    "class-transformer": "^0.4.0",
+    "class-validator": "^0.13.1",
     "reflect-metadata": "^0.1.13",
     "rimraf": "^3.0.2",
     "rxjs": "^6.6.6"

+ 16 - 2
server/src/app.module.ts

@@ -1,11 +1,25 @@
 import { Module } from '@nestjs/common';
+import { APP_INTERCEPTOR } from '@nestjs/core';
 import { AppController } from './app.controller';
 import { AppService } from './app.service';
 import { CollectionModule } from './collection/collection.module';
+import { ErrorInterceptor, TransformResInterceptor } from './interceptors';
+import { MilvusModule } from './milvus/milvus.module';
 
 @Module({
-  imports: [CollectionModule],
+  imports: [CollectionModule, MilvusModule],
   controllers: [AppController],
-  providers: [AppService],
+  providers: [
+    AppService,
+    // global interceptors
+    {
+      provide: APP_INTERCEPTOR,
+      useClass: ErrorInterceptor,
+    },
+    {
+      provide: APP_INTERCEPTOR,
+      useClass: TransformResInterceptor,
+    },
+  ],
 })
 export class AppModule {}

+ 9 - 0
server/src/milvus/dto.ts

@@ -0,0 +1,9 @@
+import { IsNotEmpty, IsString } from 'class-validator';
+
+export class ConnectMilvus {
+  @IsString()
+  @IsNotEmpty({
+    message: 'address is empty',
+  })
+  readonly address: string;
+}

+ 18 - 0
server/src/milvus/milvus.controller.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { MilvusController } from './milvus.controller';
+
+describe('MilvusController', () => {
+  let controller: MilvusController;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      controllers: [MilvusController],
+    }).compile();
+
+    controller = module.get<MilvusController>(MilvusController);
+  });
+
+  it('should be defined', () => {
+    expect(controller).toBeDefined();
+  });
+});

+ 23 - 0
server/src/milvus/milvus.controller.ts

@@ -0,0 +1,23 @@
+import {
+  Body,
+  Controller,
+  Post,
+  UseInterceptors,
+  UsePipes,
+} from '@nestjs/common';
+import { ErrorInterceptor } from 'src/interceptors';
+import { ValidationPipe } from 'src/pipe/validation.pipe';
+import { ConnectMilvus } from './dto';
+import { MilvusService } from './milvus.service';
+
+@Controller('milvus')
+export class MilvusController {
+  constructor(private milvusService: MilvusService) {}
+
+  @Post('connect')
+  @UsePipes(new ValidationPipe())
+  @UseInterceptors(new ErrorInterceptor())
+  async connect(@Body() body: ConnectMilvus): Promise<any> {
+    return await this.milvusService.connectMilvus(body.address);
+  }
+}

+ 10 - 0
server/src/milvus/milvus.module.ts

@@ -0,0 +1,10 @@
+import { Module } from '@nestjs/common';
+import { MilvusController } from './milvus.controller';
+import { MilvusService } from './milvus.service';
+
+@Module({
+  controllers: [MilvusController],
+  providers: [MilvusService],
+  exports: [MilvusService],
+})
+export class MilvusModule {}

+ 18 - 0
server/src/milvus/milvus.service.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { MilvusService } from './milvus.service';
+
+describe('MilvusService', () => {
+  let service: MilvusService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [MilvusService],
+    }).compile();
+
+    service = module.get<MilvusService>(MilvusService);
+  });
+
+  it('should be defined', () => {
+    expect(service).toBeDefined();
+  });
+});

+ 19 - 0
server/src/milvus/milvus.service.ts

@@ -0,0 +1,19 @@
+import { Injectable } from '@nestjs/common';
+
+@Injectable()
+export class MilvusService {
+  private milvusAddress: string;
+
+  constructor() {
+    this.milvusAddress = '';
+  }
+
+  get milvusAddressGetter() {
+    return this.milvusAddress;
+  }
+
+  connectMilvus(address: string) {
+    this.milvusAddress = address;
+    return { address: this.milvusAddress };
+  }
+}

+ 60 - 0
server/src/pipe/validation.pipe.ts

@@ -0,0 +1,60 @@
+import {
+  PipeTransform,
+  Injectable,
+  ArgumentMetadata,
+  BadRequestException,
+} from '@nestjs/common';
+import { validate } from 'class-validator';
+import { plainToClass } from 'class-transformer';
+
+@Injectable()
+export class ValidationPipe implements PipeTransform<any> {
+  async transform(value, { metatype }: ArgumentMetadata) {
+    if (!metatype || !this.toValidate(metatype)) {
+      return value;
+    }
+    const object = plainToClass(metatype, value);
+    // when value is null, metatype exist, object will be null
+    if (object === null) {
+      throw new BadRequestException('params should not be empty');
+    }
+
+    // interface ValidatorOptions {
+    //   skipMissingProperties?: boolean;
+    //   whitelist?: boolean;
+    //   forbidNonWhitelisted?: boolean;
+    //   groups?: string[];
+    //   dismissDefaultMessages?: boolean;
+    //   validationError?: {
+    //     target?: boolean;
+    //     value?: boolean;
+    //   };
+
+    //   forbidUnknownValues?: boolean;
+    // }
+    const errors = await validate(object, {
+      skipMissingProperties: false,
+      whitelist: true,
+      forbidNonWhitelisted: true,
+    });
+
+    errors.forEach((error) => {
+      let constraints = error.constraints;
+      let currentError = error;
+      while (!constraints && currentError) {
+        constraints = currentError.constraints;
+        currentError = currentError.children[0];
+      }
+
+      for (const i in constraints) {
+        throw new BadRequestException(constraints[i]);
+      }
+    });
+    return value;
+  }
+
+  private toValidate(metatype): boolean {
+    const types = [String, Boolean, Number, Array, Object];
+    return !types.find((type) => metatype === type);
+  }
+}

+ 29 - 0
server/yarn.lock

@@ -904,6 +904,11 @@
   dependencies:
     "@types/superagent" "*"
 
+"@types/validator@^13.1.3":
+  version "13.1.3"
+  resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.1.3.tgz#366b394aa3fbeed2392bf0a20ded606fa4a3d35e"
+  integrity sha512-DaOWN1zf7j+8nHhqXhIgNmS+ltAC53NXqGxYuBhWqWgqolRhddKzfZU814lkHQSTG0IUfQxU7Cg0gb8fFWo2mA==
+
 "@types/yargs-parser@*":
   version "20.2.0"
   resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9"
@@ -1642,6 +1647,11 @@ cjs-module-lexer@^0.6.0:
   resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-0.6.0.tgz#4186fcca0eae175970aee870b9fe2d6cf8d5655f"
   integrity sha512-uc2Vix1frTfnuzxxu1Hp4ktSvM3QaI4oXl4ZUqL1wjTu/BGki9TrCWoqLTg/drR1KwAEarXuRFCG2Svr1GxPFw==
 
+class-transformer@^0.4.0:
+  version "0.4.0"
+  resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.4.0.tgz#b52144117b423c516afb44cc1c76dbad31c2165b"
+  integrity sha512-ETWD/H2TbWbKEi7m9N4Km5+cw1hNcqJSxlSYhsLsNjQzWWiZIYA1zafxpK9PwVfaZ6AqR5rrjPVUBGESm5tQUA==
+
 class-utils@^0.3.5:
   version "0.3.6"
   resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"
@@ -1652,6 +1662,15 @@ class-utils@^0.3.5:
     isobject "^3.0.0"
     static-extend "^0.1.1"
 
+class-validator@^0.13.1:
+  version "0.13.1"
+  resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.13.1.tgz#381b2001ee6b9e05afd133671fbdf760da7dec67"
+  integrity sha512-zWIeYFhUitvAHBwNhDdCRK09hWx+P0HUwFE8US8/CxFpMVzkUK8RJl7yOIE+BVu2lxyPNgeOaFv78tLE47jBIg==
+  dependencies:
+    "@types/validator" "^13.1.3"
+    libphonenumber-js "^1.9.7"
+    validator "^13.5.2"
+
 cli-cursor@^3.1.0:
   version "3.1.0"
   resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307"
@@ -3730,6 +3749,11 @@ levn@~0.3.0:
     prelude-ls "~1.1.2"
     type-check "~0.3.2"
 
+libphonenumber-js@^1.9.7:
+  version "1.9.19"
+  resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.9.19.tgz#2e49c86185ed1aa67f2906fb20d752c8837a5c29"
+  integrity sha512-RjStfSE63LvXQEBw7pgQHPkY35z8feiMjC9wLvL1Hbt8PbhxpRrACwMXmLQgabb+IpVdcEx+olh8ll7UDXXkfA==
+
 lines-and-columns@^1.1.6:
   version "1.1.6"
   resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
@@ -5583,6 +5607,11 @@ validate-npm-package-license@^3.0.1:
     spdx-correct "^3.0.0"
     spdx-expression-parse "^3.0.0"
 
+validator@^13.5.2:
+  version "13.6.0"
+  resolved "https://registry.yarnpkg.com/validator/-/validator-13.6.0.tgz#1e71899c14cdc7b2068463cb24c1cc16f6ec7059"
+  integrity sha512-gVgKbdbHgtxpRyR8K0O6oFZPhhB5tT1jeEHZR0Znr9Svg03U0+r9DXWMrnRAB+HtCStDQKlaIZm42tVsVjqtjg==
+
 vary@^1, vary@~1.1.2:
   version "1.1.2"
   resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"