diff --git a/config/env/common.js b/config/env/common.js index 8471afc..9763763 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -13,6 +13,8 @@ const ipfsCoordName = process.env.COORD_NAME ? process.env.COORD_NAME : 'ipfs-bch-wallet-service' +console.log('GET_JWT_AT_STARTUP: ', process.env.GET_JWT_AT_STARTUP) + module.exports = { // Configure TCP port. port: process.env.PORT || 5001, diff --git a/src/adapters/index.js b/src/adapters/index.js index bdc8fe5..4494493 100644 --- a/src/adapters/index.js +++ b/src/adapters/index.js @@ -5,53 +5,53 @@ */ // Public NPM libraries -const BCHJS = require('@psf/bch-js') +const BCHJS = require("@psf/bch-js"); // Load individual adapter libraries. -const IPFSAdapter = require('./ipfs') -const LocalDB = require('./localdb') -const LogsAPI = require('./logapi') -const Passport = require('./passport') -const Nodemailer = require('./nodemailer') +const IPFSAdapter = require("./ipfs"); +const LocalDB = require("./localdb"); +const LogsAPI = require("./logapi"); +const Passport = require("./passport"); +const Nodemailer = require("./nodemailer"); // const { wlogger } = require('./wlogger') -const JSONFiles = require('./json-files') -const FullStackJWT = require('./fullstack-jwt') +const JSONFiles = require("./json-files"); +const FullStackJWT = require("./fullstack-jwt"); -const config = require('../../config') +const config = require("../../config"); class Adapters { - constructor (localConfig = {}) { + constructor(localConfig = {}) { // Encapsulate dependencies - this.ipfs = new IPFSAdapter() - this.localdb = new LocalDB() - this.logapi = new LogsAPI() - this.passport = new Passport() - this.nodemailer = new Nodemailer() - this.jsonFiles = new JSONFiles() - this.bchjs = new BCHJS() - this.config = config + this.ipfs = new IPFSAdapter(); + this.localdb = new LocalDB(); + this.logapi = new LogsAPI(); + this.passport = new Passport(); + this.nodemailer = new Nodemailer(); + this.jsonFiles = new JSONFiles(); + this.bchjs = new BCHJS(); + this.config = config; // Get a valid JWT API key and instance bch-js. - this.fullStackJwt = new FullStackJWT(config) + this.fullStackJwt = new FullStackJWT(config); } - async start () { + async start() { try { if (this.config.getJwtAtStartup) { // Get a JWT token and instantiate bch-js with it. Then pass that instance // to all the rest of the apps controllers and adapters. - await this.fullStackJwt.getJWT() + await this.fullStackJwt.getJWT(); // Instantiate bch-js with the JWT token, and overwrite the placeholder for bch-js. - this.bchjs = await this.fullStackJwt.instanceBchjs() + this.bchjs = await this.fullStackJwt.instanceBchjs(); } // Start the IPFS node. - await this.ipfs.start({ bchjs: this.bchjs }) + await this.ipfs.start({ bchjs: this.bchjs }); } catch (err) { - console.error('Error in adapters/index.js/start()') - throw err + console.error("Error in adapters/index.js/start()"); + throw err; } } } -module.exports = Adapters +module.exports = Adapters; diff --git a/src/adapters/ipfs/index.js b/src/adapters/ipfs/index.js index d124657..db650fe 100644 --- a/src/adapters/ipfs/index.js +++ b/src/adapters/ipfs/index.js @@ -2,60 +2,66 @@ top-level IPFS library that combines the individual IPFS-based libraries. */ -const IpfsAdapter = require('./ipfs') -const IpfsCoordAdapter = require('./ipfs-coord') +const IpfsAdapter = require("./ipfs"); +const IpfsCoordAdapter = require("./ipfs-coord"); class IPFS { - constructor (localConfig = {}) { + constructor(localConfig = {}) { // Encapsulate dependencies - this.ipfsAdapter = new IpfsAdapter() - this.IpfsCoordAdapter = IpfsCoordAdapter + this.ipfsAdapter = new IpfsAdapter(); + this.IpfsCoordAdapter = IpfsCoordAdapter; - this.ipfsCoordAdapter = {} // placeholder + this.ipfsCoordAdapter = {}; // placeholder // Properties of this class instance. - this.isReady = false + this.isReady = false; } // Provides a global start() function that triggers the start() function in // the underlying libraries. - async start (localConfig = {}) { + async start(localConfig = {}) { try { - const bchjs = localConfig.bchjs + const bchjs = localConfig.bchjs; if (!bchjs) { throw new Error( - 'Instance of bch-js must be passed when instantiating IPFS adapter.' - ) + "Instance of bch-js must be passed when instantiating IPFS adapter." + ); } // Start IPFS - await this.ipfsAdapter.start() - console.log('IPFS is ready.') + await this.ipfsAdapter.start(); + console.log("IPFS is ready."); // this.ipfs is a Promise that will resolve into an instance of an IPFS node. - this.ipfs = this.ipfsAdapter.ipfs + this.ipfs = this.ipfsAdapter.ipfs; // Start ipfs-coord this.ipfsCoordAdapter = new this.IpfsCoordAdapter({ ipfs: this.ipfs, - bchjs - }) - await this.ipfsCoordAdapter.start() - console.log('ipfs-coord is ready.') + bchjs, + }); + await this.ipfsCoordAdapter.start(); + console.log("ipfs-coord is ready."); - return true + return true; } catch (err) { - console.error('Error in adapters/ipfs/index.js/start()') + console.error("Error in adapters/ipfs/index.js/start()"); - // If error is due to a lock file issue. Kill the process, so that - // Docker or pm2 has a chance to restart the service. - if (err.message.includes('Lock already being held')) { - process.exit(1) + // If we are not in a test environment. + if (process.env.SVC_ENV !== "test") { + // If error is due to a lock file issue. Kill the process, so that + // Docker or pm2 has a chance to restart the service. + if (err.message.includes("Lock already being held")) { + console.log( + "Lock file issue with IPFS. Shutting down so that process manager can restart app." + ); + process.exit(1); + } } - throw err + throw err; } } } -module.exports = IPFS +module.exports = IPFS; diff --git a/src/controllers/index.js b/src/controllers/index.js index 6167168..0025fa8 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -7,64 +7,55 @@ // Public npm libraries. // Load the Clean Architecture Adapters library -const Adapters = require('../adapters') +const Adapters = require("../adapters"); // Load the JSON RPC Controller. -const JSONRPC = require('./json-rpc') +const JSONRPC = require("./json-rpc"); // Load the Clean Architecture Use Case libraries. -const UseCases = require('../use-cases') +const UseCases = require("../use-cases"); // const useCases = new UseCases({ adapters }) // Load the REST API Controllers. -const RESTControllers = require('./rest-api') +const RESTControllers = require("./rest-api"); class Controllers { - constructor (localConfig = {}) { - this.adapters = new Adapters() - this.useCases = new UseCases({ adapters: this.adapters }) + constructor(localConfig = {}) { + this.adapters = new Adapters(); + this.useCases = new UseCases({ adapters: this.adapters }); } - async attachControllers (app) { - // Get a JWT token and instantiate bch-js with it. Then pass that instance - // to all the rest of the apps controllers and adapters. - await this.adapters.fullStackJwt.getJWT() - // Instantiate bch-js with the JWT token, and overwrite the placeholder for bch-js. - this.adapters.bchjs = await this.adapters.fullStackJwt.instanceBchjs() - + async attachControllers(app) { // Wait for any startup processes to complete for the Adapters libraries. - await this.adapters.start() + await this.adapters.start(); - // Attach the REST controllers to the Koa app. - // this.attachRESTControllers(app) - - this.attachRPCControllers() + this.attachRPCControllers(); } // Top-level function for this library. // Start the various Controllers and attach them to the app. - attachRESTControllers (app) { + attachRESTControllers(app) { const restControllers = new RESTControllers({ adapters: this.adapters, - useCases: this.useCases - }) + useCases: this.useCases, + }); // Attach the REST API Controllers associated with the boilerplate code to the Koa app. - restControllers.attachRESTControllers(app) + restControllers.attachRESTControllers(app); } // Add the JSON RPC router to the ipfs-coord adapter. - attachRPCControllers () { + attachRPCControllers() { const jsonRpcController = new JSONRPC({ adapters: this.adapters, - useCases: this.useCases - }) + useCases: this.useCases, + }); // Attach the input of the JSON RPC router to the output of ipfs-coord. this.adapters.ipfs.ipfsCoordAdapter.attachRPCRouter( jsonRpcController.router - ) + ); } } -module.exports = Controllers +module.exports = Controllers; diff --git a/test/unit/controllers/controllers.unit.js b/test/unit/controllers/controllers.unit.js index d85aa50..dfd4642 100644 --- a/test/unit/controllers/controllers.unit.js +++ b/test/unit/controllers/controllers.unit.js @@ -3,63 +3,61 @@ */ // Public npm libraries -const assert = require('chai').assert -const sinon = require('sinon') +const assert = require("chai").assert; +const sinon = require("sinon"); -const Controllers = require('../../../src/controllers') +const Controllers = require("../../../src/controllers"); -describe('#Controllers', () => { - let uut - let sandbox +describe("#Controllers", () => { + let uut; + let sandbox; beforeEach(() => { - sandbox = sinon.createSandbox() + sandbox = sinon.createSandbox(); - uut = new Controllers() - }) + uut = new Controllers(); + }); - afterEach(() => sandbox.restore()) + afterEach(() => sandbox.restore()); - describe('#attachControllers', () => { - it('should attach the controllers', async () => { + describe("#attachControllers", () => { + it("should attach the controllers", async () => { // mock dependencies // sandbox.stub(adapters.fullStackJwt, 'getJWT').resolves({}) // sandbox.stub(adapters.fullStackJwt, 'instanceBchjs').resolves({}) // sandbox.stub(adapters.ipfs, 'start').resolves({}) // adapters.ipfs.ipfsCoordAdapter = { - sandbox.stub(uut.adapters, 'start').resolves({}) + sandbox.stub(uut.adapters, "start").resolves({}); uut.adapters.ipfs.ipfsCoordAdapter = { - attachRPCRouter: () => {} - } + attachRPCRouter: () => {}, + }; const app = { - use: () => {} - } + use: () => {}, + }; - await uut.attachControllers(app) + await uut.attachControllers(app); - assert.isOk(true, 'Not throwing an error is a success') - }) + assert.isOk(true, "Not throwing an error is a success"); + }); - it('should catch and throw errors', async () => { + it("should catch and throw errors", async () => { try { // Force an error - sandbox - .stub(uut.adapters.fullStackJwt, 'getJWT') - .rejects(new Error('test error')) + sandbox.stub(uut.adapters, "start").rejects(new Error("test error")); const app = { - use: () => {} - } + use: () => {}, + }; - await uut.attachControllers(app) + await uut.attachControllers(app); - assert.fail('Unexpected code path') + assert.fail("Unexpected code path"); } catch (err) { // console.log('err.message: ', err.message) - assert.include(err.message, 'test error') + assert.include(err.message, "test error"); } - }) - }) -}) + }); + }); +});