Files
bch-dex/src/controllers/index.js
T

76 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
This is a top-level library that encapsulates all the additional Controllers.
The concept of Controllers comes from Clean Architecture:
https://troutsblog.com/blog/clean-architecture
*/
// Public npm libraries.
// Load the Clean Architecture Adapters library
const Adapters = require('../adapters')
// Load the JSON RPC Controller.
const JSONRPC = require('./json-rpc')
// Load the Clean Architecture Use Case libraries.
const UseCases = require('../use-cases')
// const useCases = new UseCases({ adapters })
2021-07-06 19:29:49 -07:00
// Load the REST API Controllers.
const RESTControllers = require('./rest-api')
class Controllers {
constructor (localConfig = {}) {
this.adapters = new Adapters()
this.useCases = new UseCases({ adapters: this.adapters })
}
2022-06-27 10:09:08 -07:00
// Spin up any adapter libraries that have async startup needs.
async initAdapters () {
await this.adapters.start()
2022-06-27 10:09:08 -07:00
}
2022-06-27 10:09:08 -07:00
// Run any Use Cases to startup the app.
async initUseCases () {
await this.useCases.start()
}
2021-07-06 19:29:49 -07:00
// Top-level function for this library.
// Start the various Controllers and attach them to the app.
attachRESTControllers (app) {
2021-08-08 08:09:48 -07:00
const restControllers = new RESTControllers({
adapters: this.adapters,
useCases: this.useCases
})
// Attach the REST API Controllers associated with the boilerplate code to the Koa app.
2021-08-08 08:09:48 -07:00
restControllers.attachRESTControllers(app)
}
2022-06-27 10:09:08 -07:00
// Attach any other controllers other than REST API controllers.
async attachControllers (app) {
// Wait for any startup processes to complete for the Adapters libraries.
// await this.adapters.start()
// Attach the REST controllers to the Koa app.
// this.attachRESTControllers(app)
this.attachRPCControllers()
}
// 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
)
}
}
module.exports = Controllers