main.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as helmet from 'helmet';
  2. import { NestFactory } from '@nestjs/core';
  3. import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
  4. import { AppModule } from './app.module';
  5. import { json } from 'body-parser';
  6. import * as hyperlinker from 'hyperlinker';
  7. /*
  8. Milvus insight API server bootstrap function
  9. */
  10. async function bootstrap() {
  11. // by default the server will be listening on port 3000
  12. const port = 3000;
  13. // create the nest application with Cross-origin resource sharing
  14. const app = await NestFactory.create(AppModule, { cors: true });
  15. // security patches
  16. app.use(
  17. helmet({
  18. // If true will cause blank page after client build.
  19. contentSecurityPolicy: false,
  20. }),
  21. );
  22. // set upload file size limit
  23. app.use(json({ limit: '150mb' }));
  24. // add an API prefix
  25. app.setGlobalPrefix('/api/v1');
  26. // prepare swagger config
  27. const config = new DocumentBuilder()
  28. .setTitle('Milvus insight')
  29. .setVersion('1.0')
  30. .build();
  31. // create swagger document
  32. const document = SwaggerModule.createDocument(app, config);
  33. // set up API
  34. SwaggerModule.setup('api', app, document);
  35. // start listening
  36. await app.listen(port);
  37. // output server info
  38. // eslint-disable-next-line @typescript-eslint/no-var-requires
  39. require('dns').lookup(require('os').hostname(), (err, add, fam) => {
  40. // get link
  41. // add = `127.0.0.1`;
  42. const link = `http://${add}:${port}/api`;
  43. const blue = `\x1b[34m%s\x1b[0m`;
  44. const light = '\x1b[1m%s\x1b[0m';
  45. console.log(blue, '\n Milvus insight server started.');
  46. console.log(
  47. light,
  48. ` View the API docs on ${hyperlinker(link, link)} \n`,
  49. );
  50. });
  51. }
  52. // Start the server
  53. bootstrap();