server.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'reflect-metadata'
  2. import { OpenTelemetrySDK } from '@standardnotes/domain-events-infra'
  3. import { ServiceIdentifier } from '@standardnotes/domain-core'
  4. const sdk = new OpenTelemetrySDK({ serviceName: ServiceIdentifier.NAMES.Files })
  5. sdk.start()
  6. import * as busboy from 'connect-busboy'
  7. import '../src/Infra/InversifyExpress/AnnotatedFallbackController'
  8. import '../src/Infra/InversifyExpress/AnnotatedHealthCheckController'
  9. import '../src/Infra/InversifyExpress/AnnotatedFilesController'
  10. import '../src/Infra/InversifyExpress/AnnotatedSharedVaultFilesController'
  11. import helmet from 'helmet'
  12. import * as cors from 'cors'
  13. import { urlencoded, json, raw, Request, Response, NextFunction } from 'express'
  14. import * as winston from 'winston'
  15. // eslint-disable-next-line @typescript-eslint/no-var-requires
  16. const robots = require('express-robots-txt')
  17. import { InversifyExpressServer } from 'inversify-express-utils'
  18. import { ContainerConfigLoader } from '../src/Bootstrap/Container'
  19. import TYPES from '../src/Bootstrap/Types'
  20. import { Env } from '../src/Bootstrap/Env'
  21. const container = new ContainerConfigLoader()
  22. void container.load().then((container) => {
  23. const env: Env = new Env()
  24. env.load()
  25. const server = new InversifyExpressServer(container)
  26. server.setConfig((app) => {
  27. app.use((_request: Request, response: Response, next: NextFunction) => {
  28. response.setHeader('X-Files-Version', container.get(TYPES.Files_VERSION))
  29. next()
  30. })
  31. app.use(
  32. busboy({
  33. highWaterMark: 2 * 1024 * 1024,
  34. }),
  35. )
  36. /* eslint-disable */
  37. app.use(helmet({
  38. contentSecurityPolicy: {
  39. directives: {
  40. defaultSrc: ["https: 'self'"],
  41. baseUri: ["'self'"],
  42. childSrc: ["*", "blob:"],
  43. connectSrc: ["*"],
  44. fontSrc: ["*", "'self'"],
  45. formAction: ["'self'"],
  46. frameAncestors: ["*", "*.standardnotes.org", "*.standardnotes.com"],
  47. frameSrc: ["*", "blob:"],
  48. imgSrc: ["'self'", "*", "data:"],
  49. manifestSrc: ["'self'"],
  50. mediaSrc: ["'self'"],
  51. objectSrc: ["'self'"],
  52. scriptSrc: ["'self'"],
  53. styleSrc: ["'self'"]
  54. }
  55. }
  56. }))
  57. /* eslint-enable */
  58. app.use(json({ limit: '50mb' }))
  59. app.use(raw({ limit: '50mb', type: 'application/octet-stream' }))
  60. app.use(urlencoded({ extended: true, limit: '50mb' }))
  61. app.use(
  62. cors({
  63. exposedHeaders: ['Content-Range', 'Accept-Ranges'],
  64. }),
  65. )
  66. app.use(
  67. robots({
  68. UserAgent: '*',
  69. Disallow: '/',
  70. }),
  71. )
  72. })
  73. const logger: winston.Logger = container.get(TYPES.Files_Logger)
  74. server.setErrorConfig((app) => {
  75. app.use((error: Record<string, unknown>, _request: Request, response: Response, _next: NextFunction) => {
  76. logger.error(error.stack)
  77. response.status(500).send({
  78. error: {
  79. message:
  80. "Unfortunately, we couldn't handle your request. Please try again or contact our support if the error persists.",
  81. },
  82. })
  83. })
  84. })
  85. const serverInstance = server.build()
  86. serverInstance.listen(env.get('PORT'))
  87. logger.info(`Server started on port ${process.env.PORT}`)
  88. })