2
0

app.ts 3.7 KB

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