app.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import express from 'express';
  2. import cors from 'cors';
  3. import helmet from 'helmet';
  4. import * as http from 'http';
  5. import { LRUCache } from 'lru-cache';
  6. import * as path from 'path';
  7. import chalk from 'chalk';
  8. import { router as connectRouter } from './milvus';
  9. import { router as collectionsRouter } from './collections';
  10. import { router as databasesRouter } from './database';
  11. import { router as partitionsRouter } from './partitions';
  12. import { router as cronsRouter } from './crons';
  13. import { router as userRouter } from './users';
  14. import { router as prometheusRouter } from './prometheus';
  15. import {
  16. TransformResMiddleware,
  17. LoggingMiddleware,
  18. ErrorMiddleware,
  19. ReqHeaderMiddleware,
  20. } from './middleware';
  21. import { CLIENT_TTL, SimpleQueue } from './utils';
  22. import { getIp } from './utils/Network';
  23. import { DescribeIndexRes, MilvusClient } from './types';
  24. import { initWebSocket } from './socket';
  25. // initialize express app
  26. export const app = express();
  27. // initialize cache store
  28. export const clientCache = new LRUCache<
  29. string,
  30. {
  31. milvusClient: MilvusClient;
  32. address: string;
  33. indexCache: LRUCache<string, DescribeIndexRes>;
  34. database: string;
  35. collectionsQueue: SimpleQueue<string>;
  36. }
  37. >({
  38. ttl: CLIENT_TTL,
  39. ttlAutopurge: true,
  40. });
  41. // initialize express router
  42. const router = express.Router();
  43. // define routers
  44. router.use('/milvus', connectRouter);
  45. router.use('/databases', databasesRouter);
  46. router.use('/collections', collectionsRouter);
  47. router.use('/partitions', partitionsRouter);
  48. router.use('/crons', cronsRouter);
  49. router.use('/users', userRouter);
  50. router.use('/prometheus', prometheusRouter);
  51. router.get('/healthy', (req, res, next) => {
  52. res.json({ status: 200 });
  53. next();
  54. });
  55. // initialize a simple http server
  56. const server = http.createServer(app);
  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. // init websocket server
  86. initWebSocket(server);
  87. // use port 3000 unless there exists a preconfigured port
  88. const PORT = process.env.SERVER_PORT || 3000;
  89. // start server
  90. server.listen(PORT, () => {
  91. const ips = getIp();
  92. ips.forEach(ip => {
  93. console.info(chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`));
  94. });
  95. });