server.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import 'reflect-metadata'
  2. import '../src/Infra/InversifyExpressUtils/AnnotatedHealthCheckController'
  3. import '../src/Infra/InversifyExpressUtils/AnnotatedItemsController'
  4. import '../src/Infra/InversifyExpressUtils/AnnotatedMessagesController'
  5. import '../src/Infra/InversifyExpressUtils/AnnotatedSharedVaultInvitesController'
  6. import '../src/Infra/InversifyExpressUtils/AnnotatedSharedVaultUsersController'
  7. import '../src/Infra/InversifyExpressUtils/AnnotatedSharedVaultsController'
  8. import helmet from 'helmet'
  9. import * as cors from 'cors'
  10. import { urlencoded, json, Request, Response, NextFunction } from 'express'
  11. import * as winston from 'winston'
  12. import * as AWSXRay from 'aws-xray-sdk'
  13. import { InversifyExpressServer } from 'inversify-express-utils'
  14. import TYPES from '../src/Bootstrap/Types'
  15. import { Env } from '../src/Bootstrap/Env'
  16. import { ContainerConfigLoader } from '../src/Bootstrap/Container'
  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.SyncingServer))
  31. }
  32. app.use((_request: Request, response: Response, next: NextFunction) => {
  33. response.setHeader('X-SSJS-Version', container.get(TYPES.Sync_VERSION))
  34. next()
  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"],
  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(urlencoded({ extended: true, limit: '50mb', parameterLimit: 5000 }))
  60. app.use(cors())
  61. })
  62. const logger: winston.Logger = container.get(TYPES.Sync_Logger)
  63. server.setErrorConfig((app) => {
  64. app.use((error: Record<string, unknown>, _request: Request, response: Response, _next: NextFunction) => {
  65. logger.error(error.stack)
  66. response.status(500).send({
  67. error: {
  68. message:
  69. "Unfortunately, we couldn't handle your request. Please try again or contact our support if the error persists.",
  70. },
  71. })
  72. })
  73. })
  74. const serverInstance = server.build()
  75. if (isConfiguredForAWSProduction) {
  76. serverInstance.use(AWSXRay.express.closeSegment())
  77. }
  78. serverInstance.listen(env.get('PORT'))
  79. logger.info(`Server started on port ${process.env.PORT}`)
  80. })