Browse Source

support import server modules in client

Signed-off-by: ruiyi.jiang <ruiyi.jiang@zilliz.com>
ruiyi.jiang 1 year ago
parent
commit
c60aed7d26

+ 1 - 1
client/src/context/WebSocket.tsx

@@ -1,11 +1,11 @@
 import { createContext, useContext, useEffect, useState, useRef } from 'react';
 import { io, Socket } from 'socket.io-client';
-import { WS_EVENTS, WS_EVENTS_TYPE } from '@/consts';
 import { authContext } from '@/context';
 import { url, CollectionHttp, MilvusHttp } from '@/http';
 import { CollectionView } from '@/pages/collections/Types';
 import { checkIndexBuilding, checkLoading } from '@/utils';
 import { WebSocketType } from './Types';
+import { WS_EVENTS, WS_EVENTS_TYPE } from '@server/utils/Const';
 
 export const webSocketContext = createContext<WebSocketType>({
   collections: [],

+ 2 - 1
client/tsconfig.json

@@ -10,7 +10,8 @@
     ],
     "baseUrl": "./",
     "paths": {
-      "@/*": ["src/*"]
+      "@/*": ["src/*"],
+      "@server/*": ["../server/src/*"]
     },
     "allowJs": true,
     "skipLibCheck": true,

+ 2 - 0
client/vite.config.ts

@@ -27,6 +27,8 @@ export default defineConfig({
     // extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
     alias: {
       '@': path.resolve(__dirname, './src'),
+      // be careful to include server modules in the client
+      '@server': path.resolve(__dirname, './../server/src'),
     },
   },
 });

+ 3 - 3
server/src/app.ts

@@ -21,13 +21,13 @@ import {
   ErrorMiddleware,
   ReqHeaderMiddleware,
 } from './middleware';
-import { EXPIRED_TIME, INSIGHT_CACHE } from './utils/Const';
+import { EXPIRED_TIME, CACHE_KEY } from './utils';
 import { getIp } from './utils/Network';
 // initialize express app
 export const app = express();
 
 // initialize cache store
-const insightCache = new LruCache({
+const cache = new LruCache({
   maxAge: EXPIRED_TIME,
   updateAgeOnGet: true,
 });
@@ -54,7 +54,7 @@ const server = http.createServer(app);
 const PORT = 3000;
 // setup middlewares
 // use cache
-app.set(INSIGHT_CACHE, insightCache);
+app.set(CACHE_KEY, cache);
 // use cors https://expressjs.com/en/resources/middleware/cors.html
 app.use(cors());
 // use helmet https://github.com/helmetjs/helmet

+ 4 - 6
server/src/collections/collections.service.ts

@@ -21,13 +21,11 @@ import {
   GePersistentSegmentInfoReq,
   CompactReq,
 } from '@zilliz/milvus2-sdk-node';
-import { throwErrorFromSDK } from '../utils/Error';
-import { findKeyValue, genRows } from '../utils/Helper';
-import { ROW_COUNT } from '../utils/Const';
+import { throwErrorFromSDK, findKeyValue, genRows, ROW_COUNT } from '../utils';
 import { QueryDto, ImportSampleDto, GetReplicasDto } from './dto';
 
 export class CollectionsService {
-  constructor(private milvusService: MilvusService) {}
+  constructor(private milvusService: MilvusService) { }
 
   async getCollections(data?: ShowCollectionsReq) {
     const res = await this.milvusService.client.showCollections(data);
@@ -189,8 +187,8 @@ export class CollectionsService {
         try {
           replicas = loadCollection
             ? await this.getReplicas({
-                collectionID: collectionInfo.collectionID,
-              })
+              collectionID: collectionInfo.collectionID,
+            })
             : replicas;
         } catch (e) {
           console.log('ignore getReplica');

+ 1 - 1
server/src/crons/crons.controller.ts

@@ -3,7 +3,7 @@ import { dtoValidationMiddleware } from '../middleware/validation';
 import { CronsService, SchedulerRegistry } from './crons.service';
 import { collectionsService } from '../collections';
 import { ToggleCronJobByNameDto } from './dto';
-import { MILVUS_ADDRESS } from '../utils/Const';
+import { MILVUS_ADDRESS } from '../utils';
 
 export class CronsController {
   private router: Router;

+ 2 - 2
server/src/crons/crons.service.ts

@@ -1,6 +1,6 @@
-import { CollectionsService } from '../collections/collections.service';
-import { WS_EVENTS, WS_EVENTS_TYPE } from '../utils/Const';
 import { schedule, ScheduledTask } from 'node-cron';
+import { CollectionsService } from '../collections/collections.service';
+import { WS_EVENTS, WS_EVENTS_TYPE } from '../utils';
 import { pubSub } from '../events';
 
 export class CronsService {

+ 1 - 1
server/src/crons/dto.ts

@@ -1,5 +1,5 @@
 import { IsEnum, IsString } from "class-validator";
-import { WS_EVENTS_TYPE } from "../utils/Const";
+import { WS_EVENTS_TYPE } from "../utils";
 
 export class ToggleCronJobByNameDto {
   @IsString()

+ 4 - 5
server/src/middleware/index.ts

@@ -2,9 +2,8 @@ import { Request, Response, NextFunction } from 'express';
 import morgan from 'morgan';
 import chalk from 'chalk';
 import { MilvusService } from '../milvus/milvus.service';
-import { INSIGHT_CACHE, MILVUS_ADDRESS } from '../utils/Const';
+import { CACHE_KEY, MILVUS_ADDRESS, HTTP_STATUS_CODE } from '../utils';
 import { HttpError } from 'http-errors';
-import { HTTP_STATUS_CODE } from '../utils/Error';
 import HttpErrors from 'http-errors';
 
 export const ReqHeaderMiddleware = (
@@ -12,7 +11,7 @@ export const ReqHeaderMiddleware = (
   res: Response,
   next: NextFunction
 ) => {
-  const insightCache = req.app.get(INSIGHT_CACHE);
+  const cache = req.app.get(CACHE_KEY);
   // all ape requests need set milvus address in header.
   // server will set active address in milvus service.
   const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || '';
@@ -21,10 +20,10 @@ export const ReqHeaderMiddleware = (
   //  only api request has MILVUS_ADDRESS.
   //  When client run in express, we dont need static files like: xx.js run this logic.
   //  Otherwise will cause 401 error.
-  if (milvusAddress && insightCache.has(milvusAddress)) {
+  if (milvusAddress && cache.has(milvusAddress)) {
     MilvusService.activeAddress = milvusAddress;
     // insight cache will update expire time when use insightCache.get
-    MilvusService.activeMilvusClient = insightCache.get(milvusAddress);
+    MilvusService.activeMilvusClient = cache.get(milvusAddress);
   }
 
   const CONNECT_URL = `/api/v1/milvus/connect`;

+ 5 - 8
server/src/milvus/milvus.controller.ts

@@ -2,7 +2,7 @@ import { NextFunction, Request, Response, Router } from 'express';
 import { dtoValidationMiddleware } from '../middleware/validation';
 import { MilvusService } from './milvus.service';
 import { ConnectMilvusDto, FlushDto, UseDatabaseDto } from './dto';
-import { INSIGHT_CACHE } from '../utils/Const';
+import { CACHE_KEY } from '../utils';
 import packageJson from '../../package.json';
 
 export class MilvusController {
@@ -44,11 +44,11 @@ export class MilvusController {
 
   async connectMilvus(req: Request, res: Response, next: NextFunction) {
     const { address, username, password } = req.body;
-    const insightCache = req.app.get(INSIGHT_CACHE);
+    const cache = req.app.get(CACHE_KEY);
     try {
       const result = await this.milvusService.connectMilvus(
         { address, username, password },
-        insightCache
+        cache
       );
 
       res.send(result);
@@ -60,13 +60,10 @@ export class MilvusController {
 
   async checkConnect(req: Request, res: Response, next: NextFunction) {
     const address = '' + req.query?.address;
-    const insightCache = req.app.get(INSIGHT_CACHE);
+    const cache = req.app.get(CACHE_KEY);
 
     try {
-      const result = await this.milvusService.checkConnect(
-        address,
-        insightCache
-      );
+      const result = await this.milvusService.checkConnect(address, cache);
       res.send(result);
     } catch (error) {
       next(error);

+ 1 - 1
server/src/milvus/milvus.service.ts

@@ -6,7 +6,7 @@ import {
 import HttpErrors from 'http-errors';
 import LruCache from 'lru-cache';
 import { HTTP_STATUS_CODE } from '../utils/Error';
-import { DEFAULT_MILVUS_PORT } from '../utils/Const';
+import { DEFAULT_MILVUS_PORT } from '../utils';
 import { connectivityState } from '@grpc/grpc-js';
 
 export class MilvusService {

+ 1 - 1
server/src/partitions/partitions.service.ts

@@ -9,7 +9,7 @@ import {
 } from '@zilliz/milvus2-sdk-node';
 import { throwErrorFromSDK } from '../utils/Error';
 import { findKeyValue } from '../utils/Helper';
-import { ROW_COUNT } from '../utils/Const';
+import { ROW_COUNT } from '../utils';
 
 export class PartitionsService {
   constructor(private milvusService: MilvusService) {}

+ 1 - 1
server/src/utils/Const.ts

@@ -4,7 +4,7 @@ export const ROW_COUNT = 'row_count';
 export const MILVUS_ADDRESS = 'milvus-address';
 
 // for lru cache
-export const INSIGHT_CACHE = 'insight_cache';
+export const CACHE_KEY = 'insight_cache';
 export const EXPIRED_TIME = 1000 * 60 * 60 * 24;
 
 export enum LOADING_STATE {

+ 12 - 7
server/src/utils/index.ts

@@ -1,12 +1,12 @@
-import glob from "glob";
-import fs from "fs";
+import glob from 'glob';
+import fs from 'fs';
 
 // Utils: read files under specified directories
 export const getDirectories = (
   src: string,
   callback: (err: Error, res: string[]) => void
 ) => {
-  glob(src + "/**/*", callback);
+  glob(src + '/**/*', callback);
 };
 
 // sync: read files under specified directories
@@ -15,7 +15,7 @@ export const getDirectoriesSync = (
   callback: (err: Error, res: string[]) => void
 ) => {
   try {
-    const results = glob.sync(src + "/**/*");
+    const results = glob.sync(src + '/**/*');
     callback(undefined, results);
   } catch (error) {
     callback(error, []);
@@ -28,12 +28,12 @@ export const generateCfgs = (
   isSrcPlugin: boolean = true
 ) => {
   dirRes.forEach((item: string) => {
-    if (item.endsWith("/config.json")) {
+    if (item.endsWith('/config.json')) {
       const fileData = fs.readFileSync(item);
       const jsonData = JSON.parse(fileData.toString());
       const apiPath = jsonData?.server?.api;
-      const dirName = item.split("/config.json").shift().split("/").pop();
-      const dir = item.split("/config.json").shift();
+      const dirName = item.split('/config.json').shift().split('/').pop();
+      const dir = item.split('/config.json').shift();
       const cfg = {
         path: item,
         dir,
@@ -48,3 +48,8 @@ export const generateCfgs = (
     }
   });
 };
+
+export * from './Const';
+export * from './Error';
+export * from './Helper';
+export * from './Network';