app.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 LruCache from 'lru-cache';
  7. import * as path from 'path';
  8. import chalk from 'chalk';
  9. import { router as connectRouter } from './milvus';
  10. import { router as collectionsRouter } from './collections';
  11. import { router as databasesRouter } from './database';
  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. TransformResMiddleware,
  20. LoggingMiddleware,
  21. ErrorMiddleware,
  22. ReqHeaderMiddleware,
  23. } from './middleware';
  24. import { EXPIRED_TIME, CACHE_KEY } from './utils';
  25. import { getIp } from './utils/Network';
  26. // initialize express app
  27. export const app = express();
  28. // initialize cache store
  29. const cache = 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('/databases', databasesRouter);
  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. // setup middlewares
  53. // use cache
  54. app.set(CACHE_KEY, cache);
  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(TransformResMiddleware);
  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. // handle every other route with index.html, which will contain
  76. // a script tag to your application's JavaScript file(s).
  77. app.get('*', (request, response) => {
  78. response.sendFile(path.join(__dirname, '../build/index.html'));
  79. });
  80. // ErrorInterceptor
  81. app.use(ErrorMiddleware);
  82. // start server
  83. server.listen(PORT, () => {
  84. // initialize the WebSocket server instance
  85. const io = new Server(server, {
  86. cors: {
  87. origin: '*',
  88. methods: ['GET', 'POST'],
  89. },
  90. });
  91. // Init WebSocket server event listener
  92. io.on('connection', (socket: Socket) => {
  93. console.info(
  94. chalk.green(`ws client connected ${socket.client.conn.remoteAddress}`)
  95. );
  96. socket.on('COLLECTION', (message: any) => {
  97. socket.emit('COLLECTION', { data: message });
  98. });
  99. pubSub.on('ws_pubsub', (msg: any) => {
  100. socket.emit(msg.event, msg.data);
  101. });
  102. socket.on('disconnect', () => {
  103. console.info(
  104. chalk.green(
  105. `ws client disconnected ${socket.client.conn.remoteAddress}`
  106. )
  107. );
  108. });
  109. });
  110. server.on('disconnect', (socket: Socket) => {
  111. io.removeAllListeners();
  112. });
  113. const ips = getIp();
  114. ips.forEach(ip => {
  115. console.info(chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`));
  116. });
  117. });