Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Oct 18, 2024
1 parent 776b3d5 commit 7727b87
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
18 changes: 12 additions & 6 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ErrorMiddleware,
ReqHeaderMiddleware,
} from './middleware';
import { CLIENT_TTL, SimpleQueue } from './utils';
import { CLIENT_TTL, SimpleQueue, isElectron } from './utils';
import { getIp } from './utils/Network';
import { DescribeIndexRes, MilvusClient } from './types';
import { initWebSocket } from './socket';
Expand Down Expand Up @@ -73,7 +73,9 @@ app.use(express.json({ limit: '150MB' }));
// TransformResInterceptor
app.use(TransformResMiddleware);
// LoggingInterceptor
app.use(LoggingMiddleware);
if (!isElectron()) {
app.use(LoggingMiddleware);
}
// All headers operations
app.use(ReqHeaderMiddleware);
// use router
Expand All @@ -96,8 +98,12 @@ const PORT = process.env.SERVER_PORT || 3000;

// start server
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}`)
);
});
}
});
14 changes: 8 additions & 6 deletions server/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import morgan from 'morgan';
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 HttpErrors from 'http-errors';
import { clientCache } from '../app';
Expand Down Expand Up @@ -77,11 +77,13 @@ export const ErrorMiddleware = (
next: NextFunction
) => {
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.
// Here to prevent sending response after header has been sent.
if (res.headersSent) {
Expand Down
22 changes: 15 additions & 7 deletions server/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Server, Socket } from 'socket.io';
import * as http from 'http';
import chalk from 'chalk';
import { WS_EVENTS } from './utils';
import { WS_EVENTS, isElectron } from './utils';

export let io: Server;
export let clients = new Map<string, Socket>();
Expand All @@ -19,23 +19,31 @@ export function initWebSocket(server: http.Server) {
// register client
socket.on(WS_EVENTS.REGISTER, (clientId: string) => {
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', () => {
console.info(chalk.green(`ws client disconnected ${clientId}`));
if (!isElectron()) {
console.info(chalk.green(`ws client disconnected ${clientId}`));
}
clients.delete(clientId);
});

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
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}`));
}
});
}
6 changes: 5 additions & 1 deletion server/src/utils/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 7727b87

Please sign in to comment.