diff --git a/src/Configuration.ts b/src/Configuration.ts index e7902702..6bcead0c 100644 --- a/src/Configuration.ts +++ b/src/Configuration.ts @@ -44,6 +44,7 @@ export interface Configuration extends LoggingConfiguration, BitcoinRPCConfigura readonly bitcoinFeeEstimateMode: 'CONSERVATIVE' | 'ECONOMICAL' readonly bitcoinFeeRate: number + readonly ethereumRegistryWriterApiPort: number readonly ethereumRpcUrl: string readonly ethereumChainId: number readonly ethereumRegistryContractAddress: string @@ -137,6 +138,7 @@ export const DefaultConfiguration: Configuration = { forceBlockHeight: undefined, + ethereumRegistryWriterApiPort: 18081, ethereumRpcUrl: 'http://localhost:8545', ethereumChainId: 4, ethereumRegistryContractAddress: '', diff --git a/src/EthereumRegistryWriter/Business.ts b/src/EthereumRegistryWriter/Business.ts index ce52ba84..3173c45e 100644 --- a/src/EthereumRegistryWriter/Business.ts +++ b/src/EthereumRegistryWriter/Business.ts @@ -27,7 +27,12 @@ export interface Arguments { readonly configuration: Configuration } +export interface Health { + readonly healthy: boolean +} + export interface Business { + readonly getHealth: () => Promise readonly createDbIndices: () => Promise readonly insertClaimIdFilePair: (claimId: string, claimFile: string) => Promise readonly setBatchDirectory: (claimFiles: ReadonlyArray, ipfsDirectoryHash: string) => Promise @@ -83,6 +88,10 @@ export const Business = ({ const businessLogger: Pino.Logger = childWithFileName(logger, __filename) const claimAnchorReceiptsCollection: Collection = db.collection('claimAnchorReceipts') + const getHealth = async () => ({ + healthy: true, + }) + const createDbIndices = async () => { await claimAnchorReceiptsCollection.createIndex({ claimId: 1 }, { unique: true }) await claimAnchorReceiptsCollection.createIndex({ claimFile: 1 }, { unique: true }) @@ -351,6 +360,7 @@ export const Business = ({ } return { + getHealth, createDbIndices, insertClaimIdFilePair, setBatchDirectory, diff --git a/src/EthereumRegistryWriter/EthereumRegistryWriter.ts b/src/EthereumRegistryWriter/EthereumRegistryWriter.ts index 17fc23e9..03a4db26 100644 --- a/src/EthereumRegistryWriter/EthereumRegistryWriter.ts +++ b/src/EthereumRegistryWriter/EthereumRegistryWriter.ts @@ -14,6 +14,7 @@ import { Router } from './Router' import { Scheduler } from './Scheduler' export interface EthereumRegistryWriterConfiguration extends LoggingConfiguration { + readonly apiPort: number readonly mongodbUrl: string readonly rabbitmqUrl: string readonly exchanges: ExchangeConfiguration @@ -74,7 +75,10 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit business, ethereumRegistryContract, }, - exchange: configuration.exchanges, + configuration: { + apiPort: configuration.apiPort, + exchange: configuration.exchanges, + }, }) await router.start() @@ -95,6 +99,7 @@ export const EthereumRegistryWriter = async (configuration: EthereumRegistryWrit return async () => { logger.info('Stopping EthereumRegistryWriter...') await scheduler.stop() + await router.stop() logger.debug('Stopping EthereumRegistryWriter Messaging...') await messaging.stop() logger.info('EthereumRegistryWriter Messaging Stopped') diff --git a/src/EthereumRegistryWriter/Router.ts b/src/EthereumRegistryWriter/Router.ts index 5827e13d..62e79bcc 100644 --- a/src/EthereumRegistryWriter/Router.ts +++ b/src/EthereumRegistryWriter/Router.ts @@ -1,4 +1,8 @@ +import { Server } from 'http' +import Koa from 'koa' +import KoaRouter from 'koa-router' import Pino from 'pino' +import { promisify } from 'util' import { EthereumRegistryContract } from 'Helpers/EthereumRegistryContract' import { childWithFileName } from 'Helpers/Logging' @@ -9,6 +13,11 @@ import { Messaging } from 'Messaging/Messaging' import { Business } from './Business' import { ExchangeConfiguration } from './ExchangeConfiguration' +export interface Configuration { + readonly apiPort: number + readonly exchange: ExchangeConfiguration +} + export interface Dependencies { readonly logger: Pino.Logger readonly messaging: Messaging @@ -18,11 +27,12 @@ export interface Dependencies { export interface Arguments { readonly dependencies: Dependencies - readonly exchange: ExchangeConfiguration + readonly configuration: Configuration } export interface Router { readonly start: () => Promise + readonly stop: () => Promise } export const Router = ({ @@ -32,9 +42,15 @@ export const Router = ({ business, ethereumRegistryContract, }, - exchange, + configuration: { + apiPort, + exchange, + }, }: Arguments): Router => { const routerLogger = childWithFileName(logger, __filename) + const koa = new Koa() + const koaRouter = new KoaRouter() + let server: Server const start = async () => { await messaging.consume(exchange.claimIpfsHash, onClaimIPFSHash) @@ -61,6 +77,15 @@ export const Router = ({ .on('error', (error: any) => { logger.error({ error }, 'onCidAdded error') }) + + server = koa.listen(apiPort, '0.0.0.0') + } + + const stop = async () => { + routerLogger.debug('Stopping API Router...') + server.unref() + await promisify(server.close.bind(server)) + routerLogger.info('API Router stopped') } const onClaimIPFSHash = async (message: any) => { @@ -152,7 +177,16 @@ export const Router = ({ } } + const getHealth = async (context: KoaRouter.IRouterContext, next: () => Promise) => { + context.body = await business.getHealth() + } + + koaRouter.get('/health', getHealth) + koa.use(koaRouter.allowedMethods()) + koa.use(koaRouter.routes()) + return { start, + stop, } } diff --git a/src/EthereumRegistryWriter/index.ts b/src/EthereumRegistryWriter/index.ts index 75247a30..1d757b9a 100644 --- a/src/EthereumRegistryWriter/index.ts +++ b/src/EthereumRegistryWriter/index.ts @@ -19,6 +19,7 @@ const logger: Pino.Logger = Pino({ EthereumRegistryWriter({ loggingLevel: configuration.loggingLevel, loggingPretty: configuration.loggingPretty, + apiPort: configuration.ethereumRegistryWriterApiPort, mongodbUrl: configuration.mongodbUrl, rabbitmqUrl: configuration.rabbitmqUrl, ipfs: {