app.ts 3.9 KB

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