Ported /auth route to src/controllers/

This commit is contained in:
Chris Troutner
2021-07-05 19:36:56 -07:00
parent 7e3b74711f
commit 5db4251f68
12 changed files with 158 additions and 21 deletions
+4
View File
@@ -52,6 +52,10 @@ async function startServer () {
app.use(passport.initialize())
app.use(passport.session())
// Attach boilerplate REST API endpoints.
const restApi = require('../src/controllers/rest-api')
restApi.attachControllers(app)
// Custom Middleware Modules
const modules = require('../src/modules')
modules(app)
@@ -1,9 +1,9 @@
const Passport = require('../../lib/passport')
const Passport = require('../../../lib/passport')
const passport = new Passport()
let _this
class Auth {
class AuthRESTController {
constructor () {
_this = this
this.passport = passport
@@ -82,4 +82,4 @@ class Auth {
}
}
module.exports = Auth
module.exports = AuthRESTController
+17
View File
@@ -0,0 +1,17 @@
/*
REST API library for auth route.
*/
const AuthRESTRouter = require('./router')
class AuthRESTController {
constructor (localConfig = {}) {
this.authRESTRouter = new AuthRESTRouter()
}
attach (app) {
this.authRESTRouter.attachControllers(app)
}
}
module.exports = AuthRESTController
+37
View File
@@ -0,0 +1,37 @@
/*
REST Router for the /auth route.
*/
// Public npm libraries.
const Router = require('koa-router')
// Local libraries.
const AuthRESTController = require('./controller')
class AuthRESTRouter {
constructor (localConfig = {}) {
// Encapsulate dependencies.
this.authRESTController = new AuthRESTController()
// Instantiate the router and set the base route.
const baseUrl = '/auth'
this.router = new Router({ prefix: baseUrl })
}
attachControllers (app) {
if (!app) {
throw new Error(
'Must pass app object when attached REST API controllers.'
)
}
// Define the routes and attach the controller.
this.router.post('/', this.authRESTController.authUser)
// Attach the Controller routes to the Koa app.
app.use(this.router.routes())
app.use(this.router.allowedMethods())
}
}
module.exports = AuthRESTRouter
+88
View File
@@ -0,0 +1,88 @@
/*
This index file for the Clean Architecture Controllers loads dependencies,
creates instances, and attaches the controller to REST API endpoints for
Koa.
*/
// 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')
// 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()
authRESTController.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 }
+9 -4
View File
@@ -80,10 +80,15 @@ class IPFSLib {
// Set the 'server' profile so the node does not scan private networks.
await this.ipfs.config.profiles.apply('server')
const nodeConfig = await this.ipfs.config.getAll()
console.log(
`IPFS node configuration: ${JSON.stringify(nodeConfig, null, 2)}`
)
// const nodeConfig = await this.ipfs.config.getAll()
// console.log(
// `IPFS node configuration: ${JSON.stringify(nodeConfig, null, 2)}`
// )
// Stop the IPFS node if we're running tests.
if (this.config.env === 'test') {
await this.ipfs.stop()
}
} catch (err) {
console.error('Error in startIpfs()')
throw err
-14
View File
@@ -1,14 +0,0 @@
// import * as auth from './controller'
const CONTROLLER = require('./controller')
const controller = new CONTROLLER()
// export const baseUrl = '/auth'
module.exports.baseUrl = '/auth'
// export default [
module.exports.routes = [
{
method: 'POST',
route: '/',
handlers: [controller.authUser]
}
]