Skip to content

Commit 7fb7fc1

Browse files
feat(EthereumRegistryWriter): health endpoint (#956)
1 parent dc45939 commit 7fb7fc1

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

src/Configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface Configuration extends LoggingConfiguration, BitcoinRPCConfigura
4444
readonly bitcoinFeeEstimateMode: 'CONSERVATIVE' | 'ECONOMICAL'
4545
readonly bitcoinFeeRate: number
4646

47+
readonly ethereumRegistryWriterApiPort: number
4748
readonly ethereumRpcUrl: string
4849
readonly ethereumChainId: number
4950
readonly ethereumRegistryContractAddress: string
@@ -137,6 +138,7 @@ export const DefaultConfiguration: Configuration = {
137138

138139
forceBlockHeight: undefined,
139140

141+
ethereumRegistryWriterApiPort: 18081,
140142
ethereumRpcUrl: 'http://localhost:8545',
141143
ethereumChainId: 4,
142144
ethereumRegistryContractAddress: '',

src/EthereumRegistryWriter/Business.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export interface Arguments {
2727
readonly configuration: Configuration
2828
}
2929

30+
export interface Health {
31+
readonly healthy: boolean
32+
}
33+
3034
export interface Business {
35+
readonly getHealth: () => Promise<Health>
3136
readonly createDbIndices: () => Promise<void>
3237
readonly insertClaimIdFilePair: (claimId: string, claimFile: string) => Promise<void>
3338
readonly setBatchDirectory: (claimFiles: ReadonlyArray<string>, ipfsDirectoryHash: string) => Promise<void>
@@ -83,6 +88,10 @@ export const Business = ({
8388
const businessLogger: Pino.Logger = childWithFileName(logger, __filename)
8489
const claimAnchorReceiptsCollection: Collection<DbEntry> = db.collection('claimAnchorReceipts')
8590

91+
const getHealth = async () => ({
92+
healthy: true,
93+
})
94+
8695
const createDbIndices = async () => {
8796
await claimAnchorReceiptsCollection.createIndex({ claimId: 1 }, { unique: true })
8897
await claimAnchorReceiptsCollection.createIndex({ claimFile: 1 }, { unique: true })
@@ -351,6 +360,7 @@ export const Business = ({
351360
}
352361

353362
return {
363+
getHealth,
354364
createDbIndices,
355365
insertClaimIdFilePair,
356366
setBatchDirectory,

src/EthereumRegistryWriter/EthereumRegistryWriter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Router } from './Router'
1414
import { Scheduler } from './Scheduler'
1515

1616
export interface EthereumRegistryWriterConfiguration extends LoggingConfiguration {
17+
readonly apiPort: number
1718
readonly mongodbUrl: string
1819
readonly rabbitmqUrl: string
1920
readonly exchanges: ExchangeConfiguration
@@ -74,7 +75,10 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit
7475
business,
7576
ethereumRegistryContract,
7677
},
77-
exchange: configuration.exchanges,
78+
configuration: {
79+
apiPort: configuration.apiPort,
80+
exchange: configuration.exchanges,
81+
},
7882
})
7983
await router.start()
8084

@@ -95,6 +99,7 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit
9599
return async () => {
96100
logger.info('Stopping EthereumRegistryWriter...')
97101
await scheduler.stop()
102+
await router.stop()
98103
logger.debug('Stopping EthereumRegistryWriter Messaging...')
99104
await messaging.stop()
100105
logger.info('EthereumRegistryWriter Messaging Stopped')

src/EthereumRegistryWriter/Router.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import { Server } from 'http'
2+
import Koa from 'koa'
3+
import KoaRouter from 'koa-router'
14
import Pino from 'pino'
5+
import { promisify } from 'util'
26

37
import { EthereumRegistryContract } from 'Helpers/EthereumRegistryContract'
48
import { childWithFileName } from 'Helpers/Logging'
@@ -9,6 +13,11 @@ import { Messaging } from 'Messaging/Messaging'
913
import { Business } from './Business'
1014
import { ExchangeConfiguration } from './ExchangeConfiguration'
1115

16+
export interface Configuration {
17+
readonly apiPort: number
18+
readonly exchange: ExchangeConfiguration
19+
}
20+
1221
export interface Dependencies {
1322
readonly logger: Pino.Logger
1423
readonly messaging: Messaging
@@ -18,11 +27,12 @@ export interface Dependencies {
1827

1928
export interface Arguments {
2029
readonly dependencies: Dependencies
21-
readonly exchange: ExchangeConfiguration
30+
readonly configuration: Configuration
2231
}
2332

2433
export interface Router {
2534
readonly start: () => Promise<void>
35+
readonly stop: () => Promise<void>
2636
}
2737

2838
export const Router = ({
@@ -32,9 +42,15 @@ export const Router = ({
3242
business,
3343
ethereumRegistryContract,
3444
},
35-
exchange,
45+
configuration: {
46+
apiPort,
47+
exchange,
48+
},
3649
}: Arguments): Router => {
3750
const routerLogger = childWithFileName(logger, __filename)
51+
const koa = new Koa()
52+
const koaRouter = new KoaRouter()
53+
let server: Server
3854

3955
const start = async () => {
4056
await messaging.consume(exchange.claimIpfsHash, onClaimIPFSHash)
@@ -61,6 +77,15 @@ export const Router = ({
6177
.on('error', (error: any) => {
6278
logger.error({ error }, 'onCidAdded error')
6379
})
80+
81+
server = koa.listen(apiPort, '0.0.0.0')
82+
}
83+
84+
const stop = async () => {
85+
routerLogger.debug('Stopping API Router...')
86+
server.unref()
87+
await promisify(server.close.bind(server))
88+
routerLogger.info('API Router stopped')
6489
}
6590

6691
const onClaimIPFSHash = async (message: any) => {
@@ -152,7 +177,16 @@ export const Router = ({
152177
}
153178
}
154179

180+
const getHealth = async (context: KoaRouter.IRouterContext, next: () => Promise<any>) => {
181+
context.body = await business.getHealth()
182+
}
183+
184+
koaRouter.get('/health', getHealth)
185+
koa.use(koaRouter.allowedMethods())
186+
koa.use(koaRouter.routes())
187+
155188
return {
156189
start,
190+
stop,
157191
}
158192
}

src/EthereumRegistryWriter/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const logger: Pino.Logger = Pino({
1919
EthereumRegistryWriter({
2020
loggingLevel: configuration.loggingLevel,
2121
loggingPretty: configuration.loggingPretty,
22+
apiPort: configuration.ethereumRegistryWriterApiPort,
2223
mongodbUrl: configuration.mongodbUrl,
2324
rabbitmqUrl: configuration.rabbitmqUrl,
2425
ipfs: {

0 commit comments

Comments
 (0)