app.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import express from 'express';
  2. import cors from 'cors';
  3. import helmet from 'helmet';
  4. import * as http from 'http';
  5. import { Server, Socket } from 'socket.io';
  6. import swaggerUi from 'swagger-ui-express';
  7. import LruCache from 'lru-cache';
  8. import * as path from 'path';
  9. import chalk from 'chalk';
  10. import { router as connectRouter } from './milvus';
  11. import { router as collectionsRouter } from './collections';
  12. import { router as partitionsRouter } from './partitions';
  13. import { router as schemaRouter } from './schema';
  14. import { router as cronsRouter } from './crons';
  15. import { router as userRouter } from './users';
  16. import { pubSub } from './events';
  17. import {
  18. TransformResMiddlerware,
  19. LoggingMiddleware,
  20. ErrorMiddleware,
  21. ReqHeaderMiddleware,
  22. } from './middlewares';
  23. import { surveSwaggerSpecification } from './swagger';
  24. import { EXPIRED_TIME, INSIGHT_CACHE } from './utils/Const';
  25. import { getIp } from './utils/Network';
  26. // initialize express app
  27. export const app = express();
  28. // initialize cache store
  29. const insightCache = new LruCache({
  30. maxAge: EXPIRED_TIME,
  31. updateAgeOnGet: true,
  32. });
  33. // initialize express router
  34. const router = express.Router();
  35. // define routers
  36. router.use('/milvus', connectRouter);
  37. router.use('/collections', collectionsRouter);
  38. router.use('/partitions', partitionsRouter);
  39. router.use('/schema', schemaRouter);
  40. router.use('/crons', cronsRouter);
  41. router.use('/users', userRouter);
  42. router.get('/healthy', (req, res, next) => {
  43. res.json({ status: 200 });
  44. next();
  45. });
  46. // initialize a simple http server
  47. const server = http.createServer(app);
  48. // default port 3000
  49. const PORT = 3000;
  50. // swagger
  51. const swaggerSpecs = surveSwaggerSpecification();
  52. // setup middlewares
  53. // use cache
  54. app.set(INSIGHT_CACHE, insightCache);
  55. // use cors https://expressjs.com/en/resources/middleware/cors.html
  56. app.use(cors());
  57. // use helmet https://github.com/helmetjs/helmet
  58. app.use(
  59. helmet({
  60. contentSecurityPolicy: false,
  61. })
  62. );
  63. // limit json file size
  64. app.use(express.json({ limit: '150MB' }));
  65. // TransformResInterceptor
  66. app.use(TransformResMiddlerware);
  67. // LoggingInterceptor
  68. app.use(LoggingMiddleware);
  69. // All headers operations
  70. app.use(ReqHeaderMiddleware);
  71. // use router
  72. app.use('/api/v1', router);
  73. // Return client build files
  74. app.use(express.static('build'));
  75. // use swagger
  76. app.use('/api/v1/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
  77. // handle every other route with index.html, which will contain
  78. // a script tag to your application's JavaScript file(s).
  79. app.get('*', (request, response) => {
  80. response.sendFile(path.join(__dirname, '../build/index.html'));
  81. });
  82. // ErrorInterceptor
  83. app.use(ErrorMiddleware);
  84. // start server
  85. server.listen(PORT, () => {
  86. // initialize the WebSocket server instance
  87. const io = new Server(server, {
  88. cors: {
  89. origin: '*',
  90. methods: ['GET', 'POST'],
  91. },
  92. });
  93. // Init WebSocket server event listener
  94. io.on('connection', (socket: Socket) => {
  95. console.info(
  96. chalk.green(`ws client connected ${socket.client.conn.remoteAddress}`)
  97. );
  98. socket.on('COLLECTION', (message: any) => {
  99. socket.emit('COLLECTION', { data: message });
  100. });
  101. pubSub.on('ws_pubsub', (msg: any) => {
  102. socket.emit(msg.event, msg.data);
  103. });
  104. io.on('disconnect', () => {
  105. console.info('ws disconnected');
  106. });
  107. });
  108. server.on('disconnect', () => {
  109. io.removeAllListeners();
  110. });
  111. const ips = getIp();
  112. ips.forEach(ip => {
  113. console.info(
  114. chalk.cyanBright(`Attu server started: http://${ip}:${PORT}/api/v1/swagger/`)
  115. );
  116. });
  117. });