Container.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as winston from 'winston'
  2. // eslint-disable-next-line @typescript-eslint/no-var-requires
  3. const axios = require('axios')
  4. import { AxiosInstance } from 'axios'
  5. import Redis from 'ioredis'
  6. import { Container } from 'inversify'
  7. import { Timer, TimerInterface } from '@standardnotes/time'
  8. import { Env } from './Env'
  9. import { TYPES } from './Types'
  10. import { ServiceProxyInterface } from '../Service/Http/ServiceProxyInterface'
  11. import { HttpServiceProxy } from '../Service/Http/HttpServiceProxy'
  12. import { SubscriptionTokenAuthMiddleware } from '../Controller/SubscriptionTokenAuthMiddleware'
  13. import { CrossServiceTokenCacheInterface } from '../Service/Cache/CrossServiceTokenCacheInterface'
  14. import { RedisCrossServiceTokenCache } from '../Infra/Redis/RedisCrossServiceTokenCache'
  15. import { WebSocketAuthMiddleware } from '../Controller/WebSocketAuthMiddleware'
  16. import { InMemoryCrossServiceTokenCache } from '../Infra/InMemory/InMemoryCrossServiceTokenCache'
  17. import { DirectCallServiceProxy } from '../Service/Proxy/DirectCallServiceProxy'
  18. import { ServiceContainerInterface } from '@standardnotes/domain-core'
  19. import { EndpointResolverInterface } from '../Service/Resolver/EndpointResolverInterface'
  20. import { EndpointResolver } from '../Service/Resolver/EndpointResolver'
  21. import { RequiredCrossServiceTokenMiddleware } from '../Controller/RequiredCrossServiceTokenMiddleware'
  22. import { OptionalCrossServiceTokenMiddleware } from '../Controller/OptionalCrossServiceTokenMiddleware'
  23. import { Transform } from 'stream'
  24. export class ContainerConfigLoader {
  25. async load(configuration?: {
  26. serviceContainer?: ServiceContainerInterface
  27. logger?: Transform
  28. environmentOverrides?: { [name: string]: string }
  29. }): Promise<Container> {
  30. const env: Env = new Env(configuration?.environmentOverrides)
  31. env.load()
  32. const container = new Container()
  33. const isConfiguredForHomeServer = env.get('MODE', true) === 'home-server'
  34. const isConfiguredForInMemoryCache = env.get('CACHE_TYPE', true) === 'memory'
  35. const winstonFormatters = [winston.format.splat(), winston.format.json()]
  36. if (env.get('NEW_RELIC_ENABLED', true) === 'true') {
  37. await import('newrelic')
  38. // eslint-disable-next-line @typescript-eslint/no-var-requires
  39. const newrelicFormatter = require('@newrelic/winston-enricher')
  40. const newrelicWinstonFormatter = newrelicFormatter(winston)
  41. winstonFormatters.push(newrelicWinstonFormatter())
  42. }
  43. let logger: winston.Logger
  44. if (configuration?.logger) {
  45. logger = configuration.logger as winston.Logger
  46. } else {
  47. logger = winston.createLogger({
  48. level: env.get('LOG_LEVEL', true) || 'info',
  49. format: winston.format.combine(...winstonFormatters),
  50. transports: [new winston.transports.Console({ level: env.get('LOG_LEVEL', true) || 'info' })],
  51. defaultMeta: { service: 'api-gateway' },
  52. })
  53. }
  54. container.bind<winston.Logger>(TYPES.ApiGateway_Logger).toConstantValue(logger)
  55. if (!isConfiguredForInMemoryCache) {
  56. const redisUrl = env.get('REDIS_URL')
  57. const isRedisInClusterMode = redisUrl.indexOf(',') > 0
  58. let redis
  59. if (isRedisInClusterMode) {
  60. redis = new Redis.Cluster(redisUrl.split(','))
  61. } else {
  62. redis = new Redis(redisUrl)
  63. }
  64. container.bind(TYPES.ApiGateway_Redis).toConstantValue(redis)
  65. }
  66. container.bind<AxiosInstance>(TYPES.ApiGateway_HTTPClient).toConstantValue(axios.create())
  67. // env vars
  68. container.bind(TYPES.ApiGateway_SYNCING_SERVER_JS_URL).toConstantValue(env.get('SYNCING_SERVER_JS_URL', true))
  69. container.bind(TYPES.ApiGateway_AUTH_SERVER_URL).toConstantValue(env.get('AUTH_SERVER_URL', true))
  70. container.bind(TYPES.ApiGateway_REVISIONS_SERVER_URL).toConstantValue(env.get('REVISIONS_SERVER_URL', true))
  71. container.bind(TYPES.ApiGateway_EMAIL_SERVER_URL).toConstantValue(env.get('EMAIL_SERVER_URL', true))
  72. container.bind(TYPES.ApiGateway_PAYMENTS_SERVER_URL).toConstantValue(env.get('PAYMENTS_SERVER_URL', true))
  73. container.bind(TYPES.ApiGateway_FILES_SERVER_URL).toConstantValue(env.get('FILES_SERVER_URL', true))
  74. container.bind(TYPES.ApiGateway_WEB_SOCKET_SERVER_URL).toConstantValue(env.get('WEB_SOCKET_SERVER_URL', true))
  75. container.bind(TYPES.ApiGateway_AUTH_JWT_SECRET).toConstantValue(env.get('AUTH_JWT_SECRET'))
  76. container
  77. .bind(TYPES.ApiGateway_HTTP_CALL_TIMEOUT)
  78. .toConstantValue(env.get('HTTP_CALL_TIMEOUT', true) ? +env.get('HTTP_CALL_TIMEOUT', true) : 60_000)
  79. container.bind(TYPES.ApiGateway_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
  80. container
  81. .bind(TYPES.ApiGateway_CROSS_SERVICE_TOKEN_CACHE_TTL)
  82. .toConstantValue(+env.get('CROSS_SERVICE_TOKEN_CACHE_TTL', true))
  83. container.bind(TYPES.ApiGateway_IS_CONFIGURED_FOR_HOME_SERVER).toConstantValue(isConfiguredForHomeServer)
  84. // Middleware
  85. container
  86. .bind<RequiredCrossServiceTokenMiddleware>(TYPES.ApiGateway_RequiredCrossServiceTokenMiddleware)
  87. .to(RequiredCrossServiceTokenMiddleware)
  88. container
  89. .bind<OptionalCrossServiceTokenMiddleware>(TYPES.ApiGateway_OptionalCrossServiceTokenMiddleware)
  90. .to(OptionalCrossServiceTokenMiddleware)
  91. container.bind<WebSocketAuthMiddleware>(TYPES.ApiGateway_WebSocketAuthMiddleware).to(WebSocketAuthMiddleware)
  92. container
  93. .bind<SubscriptionTokenAuthMiddleware>(TYPES.ApiGateway_SubscriptionTokenAuthMiddleware)
  94. .to(SubscriptionTokenAuthMiddleware)
  95. // Services
  96. if (isConfiguredForHomeServer) {
  97. if (!configuration?.serviceContainer) {
  98. throw new Error('Service container is required when configured for home server')
  99. }
  100. container
  101. .bind<ServiceProxyInterface>(TYPES.ApiGateway_ServiceProxy)
  102. .toConstantValue(
  103. new DirectCallServiceProxy(configuration.serviceContainer, container.get(TYPES.ApiGateway_FILES_SERVER_URL)),
  104. )
  105. } else {
  106. container.bind<ServiceProxyInterface>(TYPES.ApiGateway_ServiceProxy).to(HttpServiceProxy)
  107. }
  108. container.bind<TimerInterface>(TYPES.ApiGateway_Timer).toConstantValue(new Timer())
  109. if (isConfiguredForHomeServer) {
  110. container
  111. .bind<CrossServiceTokenCacheInterface>(TYPES.ApiGateway_CrossServiceTokenCache)
  112. .toConstantValue(new InMemoryCrossServiceTokenCache(container.get(TYPES.ApiGateway_Timer)))
  113. } else {
  114. container
  115. .bind<CrossServiceTokenCacheInterface>(TYPES.ApiGateway_CrossServiceTokenCache)
  116. .to(RedisCrossServiceTokenCache)
  117. }
  118. container
  119. .bind<EndpointResolverInterface>(TYPES.ApiGateway_EndpointResolver)
  120. .toConstantValue(new EndpointResolver(isConfiguredForHomeServer))
  121. logger.debug('Configuration complete')
  122. return container
  123. }
  124. }