diff --git a/bin/server.js b/bin/server.js index b826e5b..4fa37fe 100644 --- a/bin/server.js +++ b/bin/server.js @@ -53,7 +53,8 @@ async function startServer () { app.use(passport.session()) // Attach REST API and JSON RPC controllers to the app. - const controllers = require('../src/controllers') + const Controllers = require('../src/controllers') + const controllers = new Controllers() controllers.attachControllers(app) // Enable CORS for testing diff --git a/src/controllers/index.js b/src/controllers/index.js index 1692740..4b33244 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -14,39 +14,81 @@ const JSONRPC = require('./json-rpc') // Load the Clean Architecture Use Case libraries. const UseCases = require('../use-cases') -const useCases = new UseCases({ adapters }) +// const useCases = new UseCases({ adapters }) // Load the REST API Controllers. const RESTControllers = require('./rest-api') +class Controllers { + constructor (localConfig = {}) { + this.adapters = adapters + this.useCases = new UseCases({ adapters }) + } + + async attachControllers (app) { + // Attach the REST controllers to the Koa app. + this.attachRESTControllers(app) + + // Start IPFS. + await this.adapters.ipfs.start() + + this.attachRPCControllers() + } + + // Top-level function for this library. + // Start the various Controllers and attach them to the app. + attachRESTControllers (app) { + const rESTControllers = new RESTControllers({ + adapters: this.adapters, + useCases: this.useCases + }) + + // Attach the REST API Controllers associated with the boilerplate code to the Koa 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 + }) + + // Attach the input of the JSON RPC router to the output of ipfs-coord. + this.adapters.ipfs.ipfsCoordAdapter.attachRPCRouter( + jsonRpcController.router + ) + } +} + // Top-level function for this library. // Start the various Controllers and attach them to the app. -async function attachControllers (app) { - // Attach the REST controllers to the Koa app. - attachRESTControllers(app) +// async function attachControllers (app) { +// // Attach the REST controllers to the Koa app. +// attachRESTControllers(app) +// +// // Start IPFS. +// await adapters.ipfs.start() +// +// attachRPCControllers() +// } +// +// function attachRESTControllers (app) { +// const rESTControllers = new RESTControllers({ +// adapters, +// useCases +// }) +// +// // Attach the REST API Controllers associated with the boilerplate code to the Koa app. +// rESTControllers.attachRESTControllers(app) +// } - // Start IPFS. - await adapters.ipfs.start() +// // Add the JSON RPC router to the ipfs-coord adapter. +// function attachRPCControllers () { +// const jsonRpcController = new JSONRPC({ adapters, useCases }) +// +// // Attach the input of the JSON RPC router to the output of ipfs-coord. +// adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(jsonRpcController.router) +// } - attachRPCControllers() -} - -function attachRESTControllers (app) { - const rESTControllers = new RESTControllers({ - adapters, - useCases - }) - - // Attach the REST API Controllers associated with the boilerplate code to the Koa app. - rESTControllers.attachRESTControllers(app) -} - -// Add the JSON RPC router to the ipfs-coord adapter. -function attachRPCControllers () { - const jsonRpcController = new JSONRPC({ adapters, useCases }) - - // Attach the input of the JSON RPC router to the output of ipfs-coord. - adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(jsonRpcController.router) -} - -module.exports = { attachControllers } +module.exports = Controllers diff --git a/test/unit/controllers/controllers.unit.js b/test/unit/controllers/controllers.unit.js index 7600abc..674e587 100644 --- a/test/unit/controllers/controllers.unit.js +++ b/test/unit/controllers/controllers.unit.js @@ -7,14 +7,17 @@ const sinon = require('sinon') const adapters = require('../../../src/adapters') -const { attachControllers } = require('../../../src/controllers') +// const { attachControllers } = require('../../../src/controllers') +const Controllers = require('../../../src/controllers') describe('#Controllers', () => { - // let uut + let uut let sandbox beforeEach(() => { sandbox = sinon.createSandbox() + + uut = new Controllers() }) afterEach(() => sandbox.restore()) @@ -31,7 +34,7 @@ describe('#Controllers', () => { use: () => {} } - await attachControllers(app) + await uut.attachControllers(app) }) }) })