2
0

app.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 schemaRouter } from './schema';
  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 } from './utils';
  23. import { getIp } from './utils/Network';
  24. import { DescribeIndexResponse, MilvusClient } from './types';
  25. import { initWebSocket } from './socket';
  26. // initialize express app
  27. export const app = express();
  28. // initialize cache store
  29. export const clientCache = new LRUCache<
  30. string,
  31. {
  32. milvusClient: MilvusClient;
  33. address: string;
  34. indexCache: LRUCache<string, DescribeIndexResponse>;
  35. }
  36. >({
  37. ttl: CLIENT_TTL,
  38. ttlAutopurge: true,
  39. });
  40. // initialize express router
  41. const router = express.Router();
  42. // define routers
  43. router.use('/milvus', connectRouter);
  44. router.use('/databases', databasesRouter);
  45. router.use('/collections', collectionsRouter);
  46. router.use('/partitions', partitionsRouter);
  47. router.use('/schema', schemaRouter);
  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. // default port 3000
  58. const PORT = 3000;
  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. app.use(LoggingMiddleware);
  74. // All headers operations
  75. app.use(ReqHeaderMiddleware);
  76. // use router
  77. app.use('/api/v1', router);
  78. // Return client build files
  79. app.use(express.static('build'));
  80. // handle every other route with index.html, which will contain
  81. // a script tag to your application's JavaScript file(s).
  82. app.get('*', (request, response) => {
  83. response.sendFile(path.join(__dirname, '../build/index.html'));
  84. });
  85. // ErrorInterceptor
  86. app.use(ErrorMiddleware);
  87. // init websocket server
  88. initWebSocket(server);
  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. });