From b2e455ffccafb506f6fa9b40e002037d9d287df1 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 7 Jul 2021 07:58:33 -0700 Subject: [PATCH] Moved logapi lib to adapter dir --- src/adapters/index.js | 4 +- src/{lib => adapters}/logapi.js | 0 src/controllers/rest-api/logs/controller.js | 2 +- src/lib/ipfs.js | 119 -------------------- test/unit/biz-logic/a06-logapi.lib-unit.js | 2 +- 5 files changed, 5 insertions(+), 122 deletions(-) rename src/{lib => adapters}/logapi.js (100%) delete mode 100644 src/lib/ipfs.js diff --git a/src/adapters/index.js b/src/adapters/index.js index 8a96f89..106a1cd 100644 --- a/src/adapters/index.js +++ b/src/adapters/index.js @@ -7,9 +7,11 @@ // Load individual adapter libraries. const IPFSAdapter = require('./ipfs') const LocalDB = require('./localdb') +const LogsAPI = require('./logapi') // Instantiate adapter libraries. const ipfs = new IPFSAdapter() const localdb = new LocalDB() +const logapi = new LogsAPI() -module.exports = { ipfs, localdb } +module.exports = { ipfs, localdb, logapi } diff --git a/src/lib/logapi.js b/src/adapters/logapi.js similarity index 100% rename from src/lib/logapi.js rename to src/adapters/logapi.js diff --git a/src/controllers/rest-api/logs/controller.js b/src/controllers/rest-api/logs/controller.js index 2e5d710..c8a2398 100644 --- a/src/controllers/rest-api/logs/controller.js +++ b/src/controllers/rest-api/logs/controller.js @@ -1,4 +1,4 @@ -const LogsApiLib = require('../../../lib/logapi') +const LogsApiLib = require('../../../adapters/logapi') const logsApiLib = new LogsApiLib() let _this diff --git a/src/lib/ipfs.js b/src/lib/ipfs.js deleted file mode 100644 index 22c1df8..0000000 --- a/src/lib/ipfs.js +++ /dev/null @@ -1,119 +0,0 @@ -/* - This support library handles the connection to the IPFS network. It instantiates - the IPFS node and starts the ipfs-coord library. -*/ - -// Global npm libraries -const IPFS = require('ipfs') -const IpfsCoord = require('ipfs-coord') -// const IpfsCoord = require('../../../ipfs-coord') -const BCHJS = require('@psf/bch-js') - -// Local libraries -const config = require('../../config') -const JSONRPC = require('../controllers/json-rpc') - -class IPFSLib { - constructor (localConfig) { - // Encapsulate dependencies - this.IPFS = IPFS - this.IpfsCoord = IpfsCoord - this.bchjs = new BCHJS() - this.rpc = new JSONRPC() - this.config = config - - // this.rpc = {} - // if (localConfig.rpc) { - // this.rpc = localConfig.rpc - // } - } - - // This is a 'macro' start method. It kicks off several smaller methods that - // start the various subcomponents of this IPFS library. - async start () { - try { - await this.startIpfs() - await this.startIpfsCoord() - - // Update the RPC instance with the instance of ipfs-coord. - this.rpc.ipfsCoord = this.ipfsCoord - - console.log('IPFS is ready.') - } catch (err) { - console.error('Error trying to start IPFS: ', err) - - // Added the exit() call because this app has been observed crashing due - // to out-of-memory errors. IPFS is a memory hog. It then can't automatically - // restart due to an IPFS lock-file error. Exiting the app will give a - // process management like pm2 or systemd to successfully restart the app. - console.log('Shutting down app. Hopefully pm2 can restart it!') - process.exit(1) - } - } - - async startIpfs () { - try { - // Ipfs Options - const ipfsOptions = { - repo: './ipfsdata', - start: true, - config: { - relay: { - enabled: true, // enable circuit relay dialer and listener - hop: { - enabled: true // enable circuit relay HOP (make this node a relay) - } - }, - pubsub: true, // enable pubsub - Swarm: { - ConnMgr: { - HighWater: 30, - LowWater: 10 - } - } - } - } - - // Create a new IPFS node. - this.ipfs = await this.IPFS.create(ipfsOptions) - - // Set the 'server' profile so the node does not scan private networks. - await this.ipfs.config.profiles.apply('server') - - // const nodeConfig = await this.ipfs.config.getAll() - // console.log( - // `IPFS node configuration: ${JSON.stringify(nodeConfig, null, 2)}` - // ) - - // Stop the IPFS node if we're running tests. - if (this.config.env === 'test') { - await this.ipfs.stop() - } - } catch (err) { - console.error('Error in startIpfs()') - throw err - } - } - - async startIpfsCoord () { - try { - this.ipfsCoord = new this.IpfsCoord({ - ipfs: this.ipfs, - type: 'node.js', - // type: 'browser', - bchjs: this.bchjs, - privateLog: this.rpc.router, - isCircuitRelay: this.config.isCircuitRelay, - apiInfo: this.config.apiInfo, - announceJsonLd: this.config.announceJsonLd - }) - - await this.ipfsCoord.isReady() - } catch (err) { - console.error('Error in startIpfsCoord()') - throw err - } - } -} - -module.exports = IPFSLib diff --git a/test/unit/biz-logic/a06-logapi.lib-unit.js b/test/unit/biz-logic/a06-logapi.lib-unit.js index 19b3390..4563a69 100644 --- a/test/unit/biz-logic/a06-logapi.lib-unit.js +++ b/test/unit/biz-logic/a06-logapi.lib-unit.js @@ -5,7 +5,7 @@ const sinon = require('sinon') const util = require('util') util.inspect.defaultOptions = { depth: 1 } -const LogsApiLib = require('../../../src/lib/logapi') +const LogsApiLib = require('../../../src/adapters/logapi') const mockData = require('../mocks/log-api-mock') const context = {}