Container.ts 7.0 KB

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