Bläddra i källkod

fix build

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 8 månader sedan
förälder
incheckning
7727b8702a
4 ändrade filer med 40 tillägg och 20 borttagningar
  1. 12 6
      server/src/app.ts
  2. 8 6
      server/src/middleware/index.ts
  3. 15 7
      server/src/socket.ts
  4. 5 1
      server/src/utils/Helper.ts

+ 12 - 6
server/src/app.ts

@@ -18,7 +18,7 @@ import {
   ErrorMiddleware,
   ErrorMiddleware,
   ReqHeaderMiddleware,
   ReqHeaderMiddleware,
 } from './middleware';
 } from './middleware';
-import { CLIENT_TTL, SimpleQueue } from './utils';
+import { CLIENT_TTL, SimpleQueue, isElectron } from './utils';
 import { getIp } from './utils/Network';
 import { getIp } from './utils/Network';
 import { DescribeIndexRes, MilvusClient } from './types';
 import { DescribeIndexRes, MilvusClient } from './types';
 import { initWebSocket } from './socket';
 import { initWebSocket } from './socket';
@@ -73,7 +73,9 @@ app.use(express.json({ limit: '150MB' }));
 // TransformResInterceptor
 // TransformResInterceptor
 app.use(TransformResMiddleware);
 app.use(TransformResMiddleware);
 // LoggingInterceptor
 // LoggingInterceptor
-app.use(LoggingMiddleware);
+if (!isElectron()) {
+  app.use(LoggingMiddleware);
+}
 // All headers operations
 // All headers operations
 app.use(ReqHeaderMiddleware);
 app.use(ReqHeaderMiddleware);
 // use router
 // use router
@@ -96,8 +98,12 @@ const PORT = process.env.SERVER_PORT || 3000;
 
 
 // start server
 // start server
 server.listen(PORT, () => {
 server.listen(PORT, () => {
-  const ips = getIp();
-  ips.forEach(ip => {
-    console.info(chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`));
-  });
+  if (!isElectron()) {
+    const ips = getIp();
+    ips.forEach(ip => {
+      console.info(
+        chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`)
+      );
+    });
+  }
 });
 });

+ 8 - 6
server/src/middleware/index.ts

@@ -1,7 +1,7 @@
 import { Request, Response, NextFunction } from 'express';
 import { Request, Response, NextFunction } from 'express';
 import morgan from 'morgan';
 import morgan from 'morgan';
 import chalk from 'chalk';
 import chalk from 'chalk';
-import { MILVUS_CLIENT_ID, HTTP_STATUS_CODE } from '../utils';
+import { MILVUS_CLIENT_ID, HTTP_STATUS_CODE, isElectron } from '../utils';
 import { HttpError } from 'http-errors';
 import { HttpError } from 'http-errors';
 import HttpErrors from 'http-errors';
 import HttpErrors from 'http-errors';
 import { clientCache } from '../app';
 import { clientCache } from '../app';
@@ -77,11 +77,13 @@ export const ErrorMiddleware = (
   next: NextFunction
   next: NextFunction
 ) => {
 ) => {
   const statusCode = err.statusCode || 500;
   const statusCode = err.statusCode || 500;
-  console.log(
-    chalk.blue.bold(req.method, req.url),
-    chalk.magenta.bold(statusCode),
-    chalk.red.bold(err)
-  );
+  if (!isElectron()) {
+    console.log(
+      chalk.blue.bold(req.method, req.url),
+      chalk.magenta.bold(statusCode),
+      chalk.red.bold(err)
+    );
+  }
   // Boolean property that indicates if the app sent HTTP headers for the response.
   // Boolean property that indicates if the app sent HTTP headers for the response.
   // Here to prevent sending response after header has been sent.
   // Here to prevent sending response after header has been sent.
   if (res.headersSent) {
   if (res.headersSent) {

+ 15 - 7
server/src/socket.ts

@@ -2,7 +2,7 @@
 import { Server, Socket } from 'socket.io';
 import { Server, Socket } from 'socket.io';
 import * as http from 'http';
 import * as http from 'http';
 import chalk from 'chalk';
 import chalk from 'chalk';
-import { WS_EVENTS } from './utils';
+import { WS_EVENTS, isElectron } from './utils';
 
 
 export let io: Server;
 export let io: Server;
 export let clients = new Map<string, Socket>();
 export let clients = new Map<string, Socket>();
@@ -19,23 +19,31 @@ export function initWebSocket(server: http.Server) {
     // register client
     // register client
     socket.on(WS_EVENTS.REGISTER, (clientId: string) => {
     socket.on(WS_EVENTS.REGISTER, (clientId: string) => {
       clients.set(clientId, socket);
       clients.set(clientId, socket);
-      console.info(chalk.green(`ws client connected ${clientId}`));
+      if (!isElectron()) {
+        console.info(chalk.green(`ws client connected ${clientId}`));
+      }
 
 
       socket.on('disconnect', () => {
       socket.on('disconnect', () => {
-        console.info(chalk.green(`ws client disconnected ${clientId}`));
+        if (!isElectron()) {
+          console.info(chalk.green(`ws client disconnected ${clientId}`));
+        }
         clients.delete(clientId);
         clients.delete(clientId);
       });
       });
 
 
       socket.on('error', (error: Error) => {
       socket.on('error', (error: Error) => {
-        console.error(
-          chalk.red(`ws client error ${clientId}: ${error.message}`)
-        );
+        if (!isElectron()) {
+          console.error(
+            chalk.red(`ws client error ${clientId}: ${error.message}`)
+          );
+        }
       });
       });
     });
     });
   });
   });
 
 
   // Handle server-level errors
   // Handle server-level errors
   io.on('error', (error: Error) => {
   io.on('error', (error: Error) => {
-    console.error(chalk.red(`ws server error: ${error.message}`));
+    if (!isElectron()) {
+      console.error(chalk.red(`ws server error: ${error.message}`));
+    }
   });
   });
 }
 }

+ 5 - 1
server/src/utils/Helper.ts

@@ -169,4 +169,8 @@ export const getKeyValueListFromJsonString = (json: string): KeyValuePair[] => {
   }
   }
 };
 };
 
 
-export const cloneObj = (obj: any) => JSON.parse(JSON.stringify(obj));
+export const cloneObj = (obj: any) => JSON.parse(JSON.stringify(obj));
+
+export const isElectron = () => {
+  return process.versions && !!process.versions.electron;
+};