injecting dependencies to controllers

This commit is contained in:
Chris Troutner
2021-07-06 19:29:49 -07:00
parent 20cd77efa5
commit b36cf15b36
2 changed files with 46 additions and 18 deletions
+9 -4
View File
@@ -6,9 +6,6 @@
// Public npm libraries.
// Load the REST API Controllers.
const boilerplateRESTControllers = require('./rest-api')
// Load the Clean Architecture Adapters library
const adapters = require('../adapters')
@@ -19,6 +16,9 @@ const JSONRPC = require('./json-rpc')
const UseCases = require('../use-cases')
const useCases = new UseCases({ adapters })
// Load the REST API Controllers.
const RESTControllers = require('./rest-api')
// Top-level function for this library.
// Start the various Controllers and attach them to the app.
async function attachControllers (app) {
@@ -32,8 +32,13 @@ async function attachControllers (app) {
}
function attachRESTControllers (app) {
const rESTControllers = new RESTControllers({
adapters,
useCases
})
// Attach the REST API Controllers associated with the boilerplate code to the Koa app.
boilerplateRESTControllers.attachRESTControllers(app)
rESTControllers.attachRESTControllers(app)
}
// Add the JSON RPC router to the ipfs-coord adapter.
+37 -14
View File
@@ -12,22 +12,45 @@ const UserRESTController = require('./users')
const ContactRESTController = require('./contact')
const LogsRESTController = require('./logs')
function attachRESTControllers (app) {
// Attach the REST API Controllers associated with the /auth route
const authRESTController = new AuthRESTController()
authRESTController.attach(app)
class RESTControllers {
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.'
)
}
}
// Attach the REST API Controllers associated with the /user route
const userRESTController = new UserRESTController()
userRESTController.attach(app)
attachRESTControllers (app) {
const dependencies = {
adapters: this.adapters,
useCases: this.useCases
}
// Attach the REST API Controllers associated with the /contact route
const contactRESTController = new ContactRESTController()
contactRESTController.attach(app)
// Attach the REST API Controllers associated with the /auth route
const authRESTController = new AuthRESTController(dependencies)
authRESTController.attach(app)
// Attach the REST API Controllers associated with the /logs route
const logsRESTController = new LogsRESTController()
logsRESTController.attach(app)
// Attach the REST API Controllers associated with the /user route
const userRESTController = new UserRESTController(dependencies)
userRESTController.attach(app)
// Attach the REST API Controllers associated with the /contact route
const contactRESTController = new ContactRESTController(dependencies)
contactRESTController.attach(app)
// Attach the REST API Controllers associated with the /logs route
const logsRESTController = new LogsRESTController(dependencies)
logsRESTController.attach(app)
}
}
module.exports = { attachRESTControllers }
module.exports = RESTControllers