fix(controllers): Made Controllers library a class

This commit is contained in:
Chris Troutner
2021-07-25 16:49:01 -07:00
parent c46f7357c3
commit 44867f0173
3 changed files with 78 additions and 32 deletions
+2 -1
View File
@@ -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
+70 -28
View File
@@ -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
+6 -3
View File
@@ -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)
})
})
})