Created and connected index.js files for use-cases, adapters, and controllers

This commit is contained in:
Chris Troutner
2021-07-06 17:17:26 -07:00
parent 574e4a3d90
commit fd2cfe0b2f
15 changed files with 3635 additions and 311 deletions
+47
View File
@@ -0,0 +1,47 @@
/*
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 REST API Controllers.
const boilerplateRESTControllers = require('./rest-api')
// 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 })
// 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)
// Start IPFS.
await adapters.ipfs.start()
attachRPCControllers()
}
function attachRESTControllers (app) {
// Attach the REST API Controllers associated with the boilerplate code to the Koa app.
boilerplateRESTControllers.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 }
+15 -3
View File
@@ -15,15 +15,27 @@ let _this
class JSONRPC {
constructor (localConfig) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating PostEntry REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating PostEntry REST Controller.'
)
}
// Encapsulate dependencies
this.ipfsCoord = this.adapters.ipfs.ipfsCoordAdapter.ipfsCoord
this.jsonrpc = jsonrpc
this.userController = new UserController()
this.authController = new AuthController()
this.aboutController = new AboutController()
// This will be replaced once the ipfs-coord lib finishes initializing.
this.ipfsCoord = {}
_this = this
}
+1 -71
View File
@@ -7,40 +7,11 @@
// Public npm libraries.
// Load the REST API Controllers.
// const EntryRESTController = require('./rest/entry')
// const WebhookRESTController = require('./rest/webhook')
// const PostWebhook = require('./rest/post-webhook')
const AuthRESTController = require('./auth')
const UserRESTController = require('./users')
const ContactRESTController = require('./contact')
const LogsRESTController = require('./logs')
// 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 })
// 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)
// Start the P2WDB.
// await adapters.p2wdb.start()
// Start the P2WDB and attach the validation event handler/controller to
// the add-entry Use Case.
// await attachValidationController()
// attachRPCControllers()
}
function attachRESTControllers (app) {
// Attach the REST API Controllers associated with the /auth route
const authRESTController = new AuthRESTController()
@@ -59,45 +30,4 @@ function attachRESTControllers (app) {
logsRESTController.attach(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.p2wdb.ipfsAdapters.ipfsCoordAdapter.attachRPCRouter(
// jsonRpcController.router
// )
// }
// Start the P2WDB and its downstream depenencies (IPFS, ipfs-coord, OrbitDB).
// Also attach the post-validation, peer-replication event handler (controller)
// to the Add-Entry Use Case.
// async function attachValidationController () {
// try {
// // Trigger the addPeerEntry() use-case after a replication-validation event.
// adapters.p2wdb.orbit.validationEvent.on(
// 'ValidationSucceeded',
// async function (data) {
// try {
// // console.log(
// // 'ValidationSucceeded event triggering addPeerEntry() with this data: ',
// // data
// // )
//
// await useCases.entry.addEntry.addPeerEntry(data)
// } catch (err) {
// console.error(
// 'Error trying to process peer data with addPeerEntry(): ',
// err
// )
// // Do not throw an error. This is a top-level function.
// }
// }
// )
// } catch (err) {
// console.error('Error in controllers/index.js/startP2wdb()')
// throw err
// }
// }
module.exports = { attachControllers }
module.exports = { attachRESTControllers }