app.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 compression from 'compression';
  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 cronsRouter } from './crons';
  14. import { router as userRouter } from './users';
  15. import { router as prometheusRouter } from './prometheus';
  16. import {
  17. TransformResMiddleware,
  18. LoggingMiddleware,
  19. ErrorMiddleware,
  20. ReqHeaderMiddleware,
  21. } from './middleware';
  22. import { CLIENT_TTL, SimpleQueue, isElectron } from './utils';
  23. import { getIp } from './utils/Network';
  24. import { MilvusClient } from './types';
  25. import { initWebSocket } from './socket';
  26. // initialize express app
  27. export const app = express();
  28. // use compression
  29. app.use(compression());
  30. // initialize cache store
  31. export const clientCache = new LRUCache<
  32. string,
  33. {
  34. milvusClient: MilvusClient;
  35. address: string;
  36. database: string;
  37. collectionsQueue: SimpleQueue<string>;
  38. }
  39. >({
  40. ttl: CLIENT_TTL,
  41. ttlAutopurge: true,
  42. });
  43. // initialize express router
  44. const router = express.Router();
  45. // define routers
  46. router.use('/milvus', connectRouter);
  47. router.use('/databases', databasesRouter);
  48. router.use('/collections', collectionsRouter);
  49. router.use('/partitions', partitionsRouter);
  50. router.use('/crons', cronsRouter);
  51. router.use('/users', userRouter);
  52. router.use('/prometheus', prometheusRouter);
  53. router.get('/healthy', (req, res, next) => {
  54. res.json({ status: 200 });
  55. next();
  56. });
  57. // initialize a simple http server
  58. const server = http.createServer(app);
  59. // setup middlewares
  60. // use cors https://expressjs.com/en/resources/middleware/cors.html
  61. app.use(cors());
  62. // use helmet https://github.com/helmetjs/helmet
  63. app.use(
  64. helmet({
  65. contentSecurityPolicy: false,
  66. })
  67. );
  68. // limit json file size
  69. app.use(express.json({ limit: '150MB' }));
  70. // TransformResInterceptor
  71. app.use(TransformResMiddleware);
  72. // LoggingInterceptor
  73. if (!isElectron()) {
  74. app.use(LoggingMiddleware);
  75. }
  76. // All headers operations
  77. app.use(ReqHeaderMiddleware);
  78. // use router
  79. app.use('/api/v1', router);
  80. // Return client build files
  81. app.use(express.static('build'));
  82. // handle every other route with index.html, which will contain
  83. // a script tag to your application's JavaScript file(s).
  84. app.get('*', (request, response) => {
  85. response.sendFile(path.join(__dirname, '../build/index.html'));
  86. });
  87. // ErrorInterceptor
  88. app.use(ErrorMiddleware);
  89. // init websocket server
  90. initWebSocket(server);
  91. // use port 3000 unless there exists a preconfigured port
  92. const PORT = process.env.SERVER_PORT || 3000;
  93. // start server
  94. server.listen(PORT, () => {
  95. if (!isElectron()) {
  96. const ips = getIp();
  97. ips.forEach(ip => {
  98. console.info(
  99. chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`)
  100. );
  101. });
  102. }
  103. });