Container.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 isConfiguredForSelfHosting = env.get('MODE', true) === 'self-hosted'
  35. const isConfiguredForHomeServerOrSelfHosting = isConfiguredForHomeServer || isConfiguredForSelfHosting
  36. const isConfiguredForInMemoryCache = env.get('CACHE_TYPE', true) === 'memory'
  37. container
  38. .bind<boolean>(TYPES.ApiGateway_IS_CONFIGURED_FOR_HOME_SERVER_OR_SELF_HOSTING)
  39. .toConstantValue(isConfiguredForHomeServerOrSelfHosting)
  40. const winstonFormatters = [winston.format.splat(), winston.format.json()]
  41. let logger: winston.Logger
  42. if (configuration?.logger) {
  43. logger = configuration.logger as winston.Logger
  44. } else {
  45. logger = winston.createLogger({
  46. level: env.get('LOG_LEVEL', true) || 'info',
  47. format: winston.format.combine(...winstonFormatters),
  48. transports: [new winston.transports.Console({ level: env.get('LOG_LEVEL', true) || 'info' })],
  49. defaultMeta: { service: 'api-gateway' },
  50. })
  51. }
  52. container.bind<winston.Logger>(TYPES.ApiGateway_Logger).toConstantValue(logger)
  53. if (!isConfiguredForInMemoryCache) {
  54. const redisUrl = env.get('REDIS_URL')
  55. const isRedisInClusterMode = redisUrl.indexOf(',') > 0
  56. let redis
  57. if (isRedisInClusterMode) {
  58. redis = new Redis.Cluster(redisUrl.split(','))
  59. } else {
  60. redis = new Redis(redisUrl)
  61. }
  62. container.bind(TYPES.ApiGateway_Redis).toConstantValue(redis)
  63. }
  64. container.bind<AxiosInstance>(TYPES.ApiGateway_HTTPClient).toConstantValue(axios.create())
  65. // env vars
  66. container.bind(TYPES.ApiGateway_SYNCING_SERVER_JS_URL).toConstantValue(env.get('SYNCING_SERVER_JS_URL', true))
  67. container.bind(TYPES.ApiGateway_AUTH_SERVER_URL).toConstantValue(env.get('AUTH_SERVER_URL', true))
  68. container.bind(TYPES.ApiGateway_REVISIONS_SERVER_URL).toConstantValue(env.get('REVISIONS_SERVER_URL', true))
  69. container.bind(TYPES.ApiGateway_EMAIL_SERVER_URL).toConstantValue(env.get('EMAIL_SERVER_URL', true))
  70. container.bind(TYPES.ApiGateway_PAYMENTS_SERVER_URL).toConstantValue(env.get('PAYMENTS_SERVER_URL', true))
  71. container.bind(TYPES.ApiGateway_FILES_SERVER_URL).toConstantValue(env.get('FILES_SERVER_URL', true))
  72. container.bind(TYPES.ApiGateway_WEB_SOCKET_SERVER_URL).toConstantValue(env.get('WEB_SOCKET_SERVER_URL', true))
  73. container.bind(TYPES.ApiGateway_AUTH_JWT_SECRET).toConstantValue(env.get('AUTH_JWT_SECRET'))
  74. container
  75. .bind(TYPES.ApiGateway_HTTP_CALL_TIMEOUT)
  76. .toConstantValue(env.get('HTTP_CALL_TIMEOUT', true) ? +env.get('HTTP_CALL_TIMEOUT', true) : 60_000)
  77. container.bind(TYPES.ApiGateway_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
  78. container
  79. .bind(TYPES.ApiGateway_CROSS_SERVICE_TOKEN_CACHE_TTL)
  80. .toConstantValue(+env.get('CROSS_SERVICE_TOKEN_CACHE_TTL', true))
  81. container.bind(TYPES.ApiGateway_IS_CONFIGURED_FOR_HOME_SERVER).toConstantValue(isConfiguredForHomeServer)
  82. // Middleware
  83. container
  84. .bind<RequiredCrossServiceTokenMiddleware>(TYPES.ApiGateway_RequiredCrossServiceTokenMiddleware)
  85. .to(RequiredCrossServiceTokenMiddleware)
  86. container
  87. .bind<OptionalCrossServiceTokenMiddleware>(TYPES.ApiGateway_OptionalCrossServiceTokenMiddleware)
  88. .to(OptionalCrossServiceTokenMiddleware)
  89. container.bind<WebSocketAuthMiddleware>(TYPES.ApiGateway_WebSocketAuthMiddleware).to(WebSocketAuthMiddleware)
  90. container
  91. .bind<SubscriptionTokenAuthMiddleware>(TYPES.ApiGateway_SubscriptionTokenAuthMiddleware)
  92. .to(SubscriptionTokenAuthMiddleware)
  93. // Services
  94. if (isConfiguredForHomeServer) {
  95. if (!configuration?.serviceContainer) {
  96. throw new Error('Service container is required when configured for home server')
  97. }
  98. container
  99. .bind<ServiceProxyInterface>(TYPES.ApiGateway_ServiceProxy)
  100. .toConstantValue(
  101. new DirectCallServiceProxy(configuration.serviceContainer, container.get(TYPES.ApiGateway_FILES_SERVER_URL)),
  102. )
  103. } else {
  104. container.bind<ServiceProxyInterface>(TYPES.ApiGateway_ServiceProxy).to(HttpServiceProxy)
  105. }
  106. container.bind<TimerInterface>(TYPES.ApiGateway_Timer).toConstantValue(new Timer())
  107. if (isConfiguredForHomeServer) {
  108. container
  109. .bind<CrossServiceTokenCacheInterface>(TYPES.ApiGateway_CrossServiceTokenCache)
  110. .toConstantValue(new InMemoryCrossServiceTokenCache(container.get(TYPES.ApiGateway_Timer)))
  111. } else {
  112. container
  113. .bind<CrossServiceTokenCacheInterface>(TYPES.ApiGateway_CrossServiceTokenCache)
  114. .to(RedisCrossServiceTokenCache)
  115. }
  116. container
  117. .bind<EndpointResolverInterface>(TYPES.ApiGateway_EndpointResolver)
  118. .toConstantValue(new EndpointResolver(isConfiguredForHomeServer))
  119. logger.debug('Configuration complete')
  120. return container
  121. }
  122. }