-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.ts
127 lines (123 loc) · 3.57 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { createLogger } from 'bunyan'
import { Point } from '@cardano-ogmios/schema'
import { getConfig } from './config'
import {
AlonzoGenesis,
buildSchema as buildCardanoDbHasuraSchema,
ByronGenesis,
CardanoNodeClient,
ChainFollower,
Db,
Genesis,
HasuraClient,
MetadataClient, ShelleyGenesis,
Worker
} from '@cardano-graphql/api-cardano-db-hasura'
import { errors } from '@cardano-graphql/util'
import onDeath from 'death'
import { GraphQLSchema } from 'graphql'
import path from 'path'
import { Logger } from 'ts-log'
import { Server } from './Server'
export * from './config'
(async function () {
const config = await getConfig()
const logger: Logger = createLogger({
name: 'cardano-graphql',
level: config.loggerMinSeverity
})
try {
const loadGenesis = (eraName: string) =>
require(
path.resolve(
path.dirname(config.cardanoNodeConfigPath),
cardanoNodeConfig[`${eraName}GenesisFile`]
)
)
const schemas: GraphQLSchema[] = []
const cardanoNodeConfig = require(config.cardanoNodeConfigPath)
const genesis: Genesis = {
alonzo: loadGenesis('Alonzo') as AlonzoGenesis,
byron: loadGenesis('Byron') as ByronGenesis,
shelley: loadGenesis('Shelley') as ShelleyGenesis
}
const lastConfiguredMajorVersion = require(config.cardanoNodeConfigPath)['LastKnownBlockVersion-Major']
const cardanoNodeClient = new CardanoNodeClient(
lastConfiguredMajorVersion,
logger
)
const hasuraClient = new HasuraClient(
config.hasuraCliPath,
config.hasuraUri,
config.pollingInterval.adaSupply,
lastConfiguredMajorVersion,
logger
)
const db = new Db(config.db, logger)
const chainFollower = new ChainFollower(
hasuraClient,
logger,
config.db
)
const metadataClient = new MetadataClient(
config.metadataServerUri
)
const worker = new Worker(
hasuraClient,
logger,
metadataClient,
config.db,
{
metadataUpdateInterval: {
assets: config.metadataUpdateInterval?.assets
}
}
)
const server = new Server(schemas, config, logger)
schemas.push(await buildCardanoDbHasuraSchema(hasuraClient, genesis, cardanoNodeClient))
await db.init({
onDbInit: async () => {
await Promise.all([
hasuraClient.shutdown,
cardanoNodeClient.shutdown,
worker.shutdown,
chainFollower.shutdown
])
await server.shutdown()
},
onDbSetup: async () => {
try {
await server.init()
await hasuraClient.initialize()
await cardanoNodeClient.initialize(config.ogmios)
await metadataClient.initialize()
await chainFollower.initialize(config.ogmios)
const mostRecentPoint = await hasuraClient.getMostRecentPointWithNewAsset()
const points: Point[] = mostRecentPoint !== null ? [mostRecentPoint, 'origin'] : ['origin']
await worker.start()
await chainFollower.start(points)
await server.start()
} catch (error) {
logger.error(error.message)
if (error instanceof errors.HostDoesNotExist) {
process.exit(1)
}
}
}
})
onDeath(async () => {
await Promise.all([
hasuraClient.shutdown,
cardanoNodeClient.shutdown,
worker.shutdown,
chainFollower.shutdown
])
await db.shutdown()
await server.shutdown()
process.exit(1)
})
} catch (error) {
logger.error(error)
process.exit(1)
}
})()