/ pushtx / index.js
index.js
 1  /*!
 2   * pushtx/index.js
 3   * Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved.
 4   */
 5  
 6  import Logger from '../lib/logger.js'
 7  import db from '../lib/db/mysql-db-wrapper.js'
 8  import { waitForBitcoindRpcApi } from '../lib/bitcoind-rpc/rpc-client.js'
 9  import network from '../lib/bitcoin/network.js'
10  import keysFile from '../keys/index.js'
11  import HttpServer from '../lib/http-server/http-server.js'
12  import PushTxRestApi from './pushtx-rest-api.js'
13  import pushTxProcessor from './pushtx-processor.js'
14  
15  const keys = keysFile[network.key]
16  
17  try {
18      /**
19       * PushTx API
20       */
21      Logger.info(`PushTx : Process ID: ${process.pid}`)
22      Logger.info('PushTx : Preparing the pushTx API')
23  
24      // Wait for Bitcoind RPC API
25      // being ready to process requests
26      await waitForBitcoindRpcApi()
27  
28      // Initialize the db wrapper
29      const dbConfig = {
30          connectionLimit: keys.db.connectionLimitPushTxApi,
31          acquireTimeout: keys.db.acquireTimeout,
32          host: keys.db.host,
33          user: keys.db.user,
34          password: keys.db.pass,
35          database: keys.db.database
36      }
37  
38      db.connect(dbConfig)
39  
40      // Initialize notification sockets of singleton pushTxProcessor
41      pushTxProcessor.initNotifications({
42          uriSocket: `tcp://127.0.0.1:${keys.ports.notifpushtx}`
43      })
44  
45      // Initialize the http server
46      const host = keys.apiBind
47      const port = keys.ports.pushtx
48      const httpServer = new HttpServer(port, host)
49  
50      // Initialize the PushTx rest api
51      new PushTxRestApi(httpServer)
52  
53      // Start the http server
54      httpServer.start()
55  
56      // Signal that the process is ready
57      process.send('ready')
58  
59      const exit = async () => {
60          httpServer.stop()
61          await db.disconnect()
62          process.exit(0)
63      }
64  
65      process.on('SIGTERM', async () => {
66          await exit()
67      })
68  
69      process.on('SIGINT', async () => {
70          await exit()
71      })
72  
73  } catch (error) {
74      Logger.error(error, 'PushTx : Unhandled error, exiting...')
75      process.exit(1)
76  }