2021-07-06 17:17:26 -07:00
|
|
|
/*
|
|
|
|
|
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
|
2021-08-08 07:38:47 -07:00
|
|
|
const Adapters = require('../adapters')
|
2021-07-06 17:17:26 -07:00
|
|
|
|
|
|
|
|
// Load the JSON RPC Controller.
|
|
|
|
|
const JSONRPC = require('./json-rpc')
|
|
|
|
|
|
|
|
|
|
// Load the Clean Architecture Use Case libraries.
|
|
|
|
|
const UseCases = require('../use-cases')
|
2021-07-25 16:49:01 -07:00
|
|
|
// const useCases = new UseCases({ adapters })
|
2021-07-06 17:17:26 -07:00
|
|
|
|
2021-07-06 19:29:49 -07:00
|
|
|
// Load the REST API Controllers.
|
|
|
|
|
const RESTControllers = require('./rest-api')
|
|
|
|
|
|
2021-07-25 16:49:01 -07:00
|
|
|
class Controllers {
|
|
|
|
|
constructor (localConfig = {}) {
|
2021-08-08 07:38:47 -07:00
|
|
|
this.adapters = new Adapters()
|
|
|
|
|
this.useCases = new UseCases({ adapters: this.adapters })
|
2021-07-25 16:49:01 -07:00
|
|
|
}
|
2021-07-06 17:17:26 -07:00
|
|
|
|
2022-06-27 10:09:08 -07:00
|
|
|
// Spin up any adapter libraries that have async startup needs.
|
|
|
|
|
async initAdapters () {
|
2021-08-08 07:38:47 -07:00
|
|
|
await this.adapters.start()
|
2022-06-27 10:09:08 -07:00
|
|
|
}
|
2021-08-08 07:38:47 -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-25 16:49:01 -07:00
|
|
|
}
|
2021-07-06 19:29:49 -07:00
|
|
|
|
2021-07-25 16:49:01 -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({
|
2021-07-25 16:49:01 -07:00
|
|
|
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)
|
2021-07-25 16:49:01 -07:00
|
|
|
}
|
2021-07-06 17:17:26 -07:00
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 16:49:01 -07:00
|
|
|
// Add the JSON RPC router to the ipfs-coord adapter.
|
|
|
|
|
attachRPCControllers () {
|
|
|
|
|
const jsonRpcController = new JSONRPC({
|
|
|
|
|
adapters: this.adapters,
|
|
|
|
|
useCases: this.useCases
|
|
|
|
|
})
|
2021-07-06 17:17:26 -07:00
|
|
|
|
2021-07-25 16:49:01 -07:00
|
|
|
// Attach the input of the JSON RPC router to the output of ipfs-coord.
|
|
|
|
|
this.adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(
|
|
|
|
|
jsonRpcController.router
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-07-06 17:17:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-25 16:49:01 -07:00
|
|
|
module.exports = Controllers
|