-
Notifications
You must be signed in to change notification settings - Fork 2
/
injection-root.ts
61 lines (58 loc) · 2 KB
/
injection-root.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { EnvSchema, getServerConfig, ServerConfig } from 'lib/server/config'
import * as Sentry from '@sentry/nextjs'
import { connectionFactory } from 'lib/server/db-connection'
import { RadioRepository } from 'lib/server/radio-repository'
import { schemas } from 'lib/server/schemas'
import { countryDataByKey, fetchIpInfo } from 'lib/server/utils'
import { SharedConfig, SHARED_CONFIG } from 'lib/shared/config'
import { getSession } from 'next-auth/react'
import pino from 'pino'
import { PumpIt, SCOPE, transform } from 'pumpit'
import { RadioBrowserApi } from 'radio-browser-api'
import requestIp from 'request-ip'
import { getSongInfo, logServerError } from './utils'
let container: PumpIt
export function getServerContainer() {
if (!container) {
container = new PumpIt()
.bindValue(getSession, getSession)
.bindValue(Sentry, Sentry)
.bindValue('config', getServerConfig(process.env as unknown as EnvSchema))
.bindValue('sharedConfig', SHARED_CONFIG)
.bindFactory(connectionFactory, connectionFactory, {
scope: SCOPE.SINGLETON
})
.bindClass(
RadioBrowserApi,
{
value: RadioBrowserApi,
inject: transform(['sharedConfig'], (_, config: SharedConfig) => {
return [config.radioAPIUserAgent]
})
},
{ scope: SCOPE.CONTAINER_SINGLETON }
)
.bindClass(RadioRepository, RadioRepository)
.bindFactory(logServerError, logServerError, {
scope: SCOPE.CONTAINER_SINGLETON
})
.bindFactory(
'logger',
{
value: (config: ServerConfig) => {
return pino({
level: config.logLevel
})
},
inject: ['config']
},
{ scope: SCOPE.CONTAINER_SINGLETON }
)
.bindValue(schemas, schemas)
.bindValue(getSongInfo, getSongInfo)
.bindValue(requestIp, requestIp)
.bindValue(countryDataByKey, countryDataByKey)
.bindValue(fetchIpInfo, fetchIpInfo)
}
return container
}