import fs from 'fs' ; import path from 'path' ; import { Pool } from 'pg' ; import createPool from './db/connection' ; import * as Repostories from './db/repositories' ; import buildServer from './server' ; import * as Services from './services/services' ; import * as CardanoCli from './utils/cardano/cli/cardanonode-cli' ; import * as CardanoNode from './utils/cardano/cli/cardano-node' ; import { Environment , parseEnvironment } from './utils/environment-parser' ; import { DepositParameters } from './services/cardano-services' ; const genesis = JSON . parse ( fs . readFileSync ( path . resolve ( process . env . GENESIS_SHELLEY_PATH ) ) . toString ( ) ) ; const networkMagic = genesis . networkMagic ; const networkId = genesis . networkId . toLowerCase ( ) ; const depositParameters = { keyDeposit : genesis . protocolParams . keyDeposit , poolDeposit : genesis . protocolParams . poolDeposit } ; const start = async ( databaseInstance ) => { let server ; try { const environment = parseEnvironment ( ) ; const repository = Repostories . configure ( databaseInstance ) ; const cardanoNode = CardanoNode . configure ( environment . CARDANO_NODE_PATH ) ; const cardanoCli = CardanoCli . configure ( environment . CARDANO_CLI_PATH , networkMagic ) ; const services = Services . configure ( repository , networkId , networkMagic , environment . TOPOLOGY_FILE , environment . DEFAULT_RELATIVE_TTL , depositParameters ) ; server = buildServer ( services , cardanoCli , cardanoNode , environment . LOGGER_LEVEL , { networkId , pageSize : environment . PAGE_SIZE , disableSearchApi : environment . DISABLE_SEARCH_API } ) ; server . addHook ( 'onClose' , ( _ , done ) => databaseInstance . end ( done ) ) ; await server . listen ( environment . PORT , environment . BIND_ADDRESS ) ; server . blipp ( ) ; } catch ( error ) { server ?. log . error ( error ) ; await databaseInstance ?. end ( ) ; process . exit ( 1 ) ; } } ; process . on ( 'uncaughtException' , error => { console . error ( error ) ; } ) ; process . on ( 'unhandledRejection' , error => { console . error ( error ) ; } ) ; const connectDB = async ( ) => await createPool ( process . env . DB_CONNECTION_STRING ) ; connectDB ( ) . then ( databaseInstance => start ( databaseInstance ) ) . catch ( console . error ) ;