server.ts 3.4 KB

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