Browse Source

show ip in console

ruiyi.jiang 2 years ago
parent
commit
b14d7da6dc
2 changed files with 69 additions and 36 deletions
  1. 49 36
      server/src/app.ts
  2. 20 0
      server/src/utils/Network.ts

+ 49 - 36
server/src/app.ts

@@ -3,13 +3,16 @@ import cors from 'cors';
 import helmet from 'helmet';
 import * as http from 'http';
 import { Server, Socket } from 'socket.io';
+import swaggerUi from 'swagger-ui-express';
+import LruCache from 'lru-cache';
+import * as path from 'path';
+import chalk from 'chalk';
 import { router as connectRouter } from './milvus';
 import { router as collectionsRouter } from './collections';
 import { router as partitionsRouter } from './partitions';
 import { router as schemaRouter } from './schema';
 import { router as cronsRouter } from './crons';
 import { router as userRouter } from './users';
-
 import { pubSub } from './events';
 import {
   TransformResMiddlerware,
@@ -17,73 +20,70 @@ import {
   ErrorMiddleware,
   ReqHeaderMiddleware,
 } from './middlewares';
-
-// import { getDirectories, getDirectoriesSync, generateCfgs } from './utils';
-import * as path from 'path';
-import chalk from 'chalk';
 import { surveSwaggerSpecification } from './swagger';
-import swaggerUi from 'swagger-ui-express';
-import LruCache from 'lru-cache';
 import { EXPIRED_TIME, INSIGHT_CACHE } from './utils/Const';
+import { getIp } from './utils/Network';
+// initialize express app
+export const app = express();
 
+// initialize cache store
 const insightCache = new LruCache({
   maxAge: EXPIRED_TIME,
   updateAgeOnGet: true,
 });
 
-export const app = express();
-const PORT = 3000;
+// initialize express router
+const router = express.Router();
+// define routers
+router.use('/milvus', connectRouter);
+router.use('/collections', collectionsRouter);
+router.use('/partitions', partitionsRouter);
+router.use('/schema', schemaRouter);
+router.use('/crons', cronsRouter);
+router.use('/users', userRouter);
+router.get('/healthy', (req, res, next) => {
+  res.json({ status: 200 });
+  next();
+});
+
 // initialize a simple http server
 const server = http.createServer(app);
+// default port 3000
+const PORT = 3000;
+// swagger
+const swaggerSpecs = surveSwaggerSpecification();
 
+// setup middlewares
+// use cache
 app.set(INSIGHT_CACHE, insightCache);
-// https://expressjs.com/en/resources/middleware/cors.html
+// use cors https://expressjs.com/en/resources/middleware/cors.html
 app.use(cors());
-// https://github.com/helmetjs/helmet
+// use helmet https://github.com/helmetjs/helmet
 app.use(
   helmet({
     contentSecurityPolicy: false,
   })
 );
+// limit json file size
 app.use(express.json({ limit: '150MB' }));
 // TransformResInterceptor
 app.use(TransformResMiddlerware);
 // LoggingInterceptor
 app.use(LoggingMiddleware);
-
 // All headers operations
 app.use(ReqHeaderMiddleware);
-
-const router = express.Router();
-const pluginsRouter = express.Router();
-
-router.use('/milvus', connectRouter);
-router.use('/collections', collectionsRouter);
-router.use('/partitions', partitionsRouter);
-router.use('/schema', schemaRouter);
-router.use('/crons', cronsRouter);
-router.use('/users', userRouter);
-
-router.get('/healthy', (req, res, next) => {
-  res.json({ status: 200 });
-  next();
-});
-
+// use router
 app.use('/api/v1', router);
-app.use('/api/plugins', pluginsRouter);
-
 // Return client build files
 app.use(express.static('build'));
-
-const data = surveSwaggerSpecification();
-app.use('/api/v1/swagger', swaggerUi.serve, swaggerUi.setup(data));
+// use swagger
+app.use('/api/v1/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
 
 // handle every other route with index.html, which will contain
 // a script tag to your application's JavaScript file(s).
 app.get('*', (request, response) => {
   response.sendFile(path.join(__dirname, '../build/index.html'));
 });
-
 // ErrorInterceptor
 app.use(ErrorMiddleware);
 
@@ -96,9 +96,12 @@ server.listen(PORT, () => {
       methods: ['GET', 'POST'],
     },
   });
+
   // Init WebSocket server event listener
   io.on('connection', (socket: Socket) => {
-    console.info('ws connected');
+    console.info(
+      chalk.green(`ws client connected ${socket.client.conn.remoteAddress}`)
+    );
     socket.on('COLLECTION', (message: any) => {
       socket.emit('COLLECTION', { data: message });
     });
@@ -109,5 +112,15 @@ server.listen(PORT, () => {
       console.info('ws disconnected');
     });
   });
-  console.info(chalk.green.bold(`Attu Server started on port ${PORT} :)`));
+
+  server.on('disconnect', () => {
+    io.removeAllListeners();
+  });
+
+  const ips = getIp();
+  ips.forEach(ip => {
+    console.info(
+      chalk.cyanBright(`Attu server started: http://${ip}:${PORT}/api/v1/swagger/`)
+    );
+  });
 });

+ 20 - 0
server/src/utils/Network.ts

@@ -0,0 +1,20 @@
+import os from 'os';
+
+export const getIp = () => {
+  const interfaces = os.networkInterfaces();
+  const addresses = [];
+  for (const k in interfaces) {
+    if (interfaces.hasOwnProperty(k)) {
+      for (const k2 in interfaces[k]) {
+        if (interfaces[k].hasOwnProperty(k2)) {
+          const address = interfaces[k][k2];
+          if (address.family === 'IPv4' && !address.internal) {
+            addresses.push(address.address);
+          }
+        }
+      }
+    }
+  }
+
+  return addresses;
+};