fix(startup): Fixing startup issues caused by code mixup with upstream

This commit is contained in:
Chris Troutner
2021-09-08 12:35:00 -07:00
parent 27f6f62f8c
commit 22ab7260f4
5 changed files with 109 additions and 112 deletions
+2
View File
@@ -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,
+24 -24
View File
@@ -5,34 +5,34 @@
*/
// 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 = {}) {
// 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() {
@@ -40,18 +40,18 @@ class Adapters {
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;
+28 -22
View File
@@ -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 = {}) {
// 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 = {}) {
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 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')) {
process.exit(1)
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;
+15 -24
View File
@@ -7,38 +7,29 @@
// 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 })
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()
// 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.
@@ -46,25 +37,25 @@ class Controllers {
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() {
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;
+30 -32
View File
@@ -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");
}
})
})
})
});
});
});