app.ts 3.7 KB

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