app.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { router as connectRouter } from './milvus';
  7. import { router as collectionsRouter } from './collections';
  8. import { router as partitionsRouter } from './partitions';
  9. import { router as schemaRouter } from './schema';
  10. import { router as cronsRouter } from './crons';
  11. import { pubSub } from './events';
  12. import {
  13. TransformResMiddlerware,
  14. LoggingMiddleware,
  15. ErrorMiddleware,
  16. ReqHeaderMiddleware,
  17. } from './middlewares';
  18. import { getDirectories, getDirectoriesSync, generateCfgs } from './utils';
  19. import * as path from 'path';
  20. import chalk from 'chalk';
  21. import { surveSwaggerSpecification } from './swagger';
  22. import swaggerUi from 'swagger-ui-express';
  23. import LruCache from 'lru-cache';
  24. import { EXPIRED_TIME, INSIGHT_CACHE } from './utils/Const';
  25. const PLUGIN_DEV = process.env?.PLUGIN_DEV;
  26. const SRC_PLUGIN_DIR = 'src/plugins';
  27. const DEV_PLUGIN_DIR = '../../src/*/server';
  28. const insightCache = new LruCache({
  29. maxAge: EXPIRED_TIME,
  30. updateAgeOnGet: true,
  31. });
  32. export const app = express();
  33. const PORT = 3000;
  34. // initialize a simple http server
  35. const server = http.createServer(app);
  36. // initialize the WebSocket server instance
  37. const io = new Server(server, {
  38. cors: {
  39. origin: '*',
  40. methods: ['GET', 'POST'],
  41. },
  42. });
  43. app.set(INSIGHT_CACHE, insightCache);
  44. // https://expressjs.com/en/resources/middleware/cors.html
  45. app.use(cors());
  46. // https://github.com/helmetjs/helmet
  47. app.use(
  48. helmet({
  49. contentSecurityPolicy: false,
  50. })
  51. );
  52. app.use(express.json({ limit: '150MB' }));
  53. // TransformResInterceptor
  54. app.use(TransformResMiddlerware);
  55. // LoggingInterceptor
  56. app.use(LoggingMiddleware);
  57. // All headers operations
  58. app.use(ReqHeaderMiddleware);
  59. const router = express.Router();
  60. const pluginsRouter = express.Router();
  61. // Init WebSocket server event listener
  62. io.on('connection', (socket: Socket) => {
  63. console.log('socket.io connected');
  64. socket.on('COLLECTION', (message: any) => {
  65. socket.emit('COLLECTION', { data: message });
  66. });
  67. pubSub.on('ws_pubsub', msg => {
  68. const { event, data } = msg;
  69. socket.emit(event, data);
  70. });
  71. });
  72. // Read plugin files and start express server
  73. // Import all plguins under "src/plugins"
  74. getDirectories(SRC_PLUGIN_DIR, async (dirErr: Error, dirRes: string[]) => {
  75. const cfgs: any[] = [];
  76. if (dirErr) {
  77. console.log('Reading plugin directory Error', dirErr);
  78. } else {
  79. generateCfgs(cfgs, dirRes);
  80. }
  81. // If under plugin dev mode, import all plugins under "../../src/*/server"
  82. if (PLUGIN_DEV) {
  83. getDirectoriesSync(
  84. DEV_PLUGIN_DIR,
  85. (devDirErr: Error, devDirRes: string[]) => {
  86. if (devDirErr) {
  87. console.log('Reading dev plugin directory Error', devDirErr);
  88. } else {
  89. generateCfgs(cfgs, devDirRes, false);
  90. }
  91. }
  92. );
  93. }
  94. console.log('======/api/plugins configs======', cfgs);
  95. cfgs.forEach(async (cfg: any) => {
  96. const { api: pluginPath, componentPath } = cfg;
  97. if (!pluginPath) return;
  98. const {
  99. default: { router: pluginRouter },
  100. } = await import(componentPath);
  101. pluginsRouter.use(`/${pluginPath}`, pluginRouter);
  102. });
  103. router.use('/milvus', connectRouter);
  104. router.use('/collections', collectionsRouter);
  105. router.use('/partitions', partitionsRouter);
  106. router.use('/schema', schemaRouter);
  107. router.use('/crons', cronsRouter);
  108. router.get('/healthy', (req, res, next) => {
  109. res.json({ status: 200 });
  110. next();
  111. });
  112. app.use('/api/v1', router);
  113. app.use('/api/plugins', pluginsRouter);
  114. // Return client build files
  115. app.use(express.static('build'));
  116. const data = surveSwaggerSpecification();
  117. app.use('/api/v1/swagger', swaggerUi.serve, swaggerUi.setup(data));
  118. // handle every other route with index.html, which will contain
  119. // a script tag to your application's JavaScript file(s).
  120. app.get('*', (request, response) => {
  121. response.sendFile(path.join(__dirname, '../build/index.html'));
  122. });
  123. // ErrorInterceptor
  124. app.use(ErrorMiddleware);
  125. // start server
  126. server.listen(PORT, () => {
  127. console.log(chalk.green.bold(`Insight Server started on port ${PORT} :)`));
  128. });
  129. });