From 5db4251f68b25f4098ecbb91c302725661a73f6c Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 5 Jul 2021 19:36:56 -0700 Subject: [PATCH] Ported /auth route to src/controllers/ --- bin/server.js | 4 + .../rest-api}/auth/controller.js | 6 +- src/controllers/rest-api/auth/index.js | 17 ++++ src/controllers/rest-api/auth/router.js | 37 ++++++++ src/controllers/rest-api/index.js | 88 +++++++++++++++++++ src/lib/ipfs.js | 13 ++- src/modules/auth/router.js | 14 --- .../{ => temp}/a02-users.rest-e2e.js | 0 .../{ => temp}/a04-contact.rest-e2e.js | 0 .../{ => temp}/a06-logapi.rest-e2e.js | 0 .../{ => temp}/a08-validators.rest-e2e.js | 0 .../{ => temp}/a09-admin.rest-e2e.js | 0 12 files changed, 158 insertions(+), 21 deletions(-) rename src/{modules => controllers/rest-api}/auth/controller.js (94%) create mode 100644 src/controllers/rest-api/auth/index.js create mode 100644 src/controllers/rest-api/auth/router.js create mode 100644 src/controllers/rest-api/index.js delete mode 100644 src/modules/auth/router.js rename test/e2e/automated/{ => temp}/a02-users.rest-e2e.js (100%) rename test/e2e/automated/{ => temp}/a04-contact.rest-e2e.js (100%) rename test/e2e/automated/{ => temp}/a06-logapi.rest-e2e.js (100%) rename test/e2e/automated/{ => temp}/a08-validators.rest-e2e.js (100%) rename test/e2e/automated/{ => temp}/a09-admin.rest-e2e.js (100%) diff --git a/bin/server.js b/bin/server.js index 01facf3..a89d1b7 100644 --- a/bin/server.js +++ b/bin/server.js @@ -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) diff --git a/src/modules/auth/controller.js b/src/controllers/rest-api/auth/controller.js similarity index 94% rename from src/modules/auth/controller.js rename to src/controllers/rest-api/auth/controller.js index b888c34..52cdeeb 100644 --- a/src/modules/auth/controller.js +++ b/src/controllers/rest-api/auth/controller.js @@ -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 diff --git a/src/controllers/rest-api/auth/index.js b/src/controllers/rest-api/auth/index.js new file mode 100644 index 0000000..20c769c --- /dev/null +++ b/src/controllers/rest-api/auth/index.js @@ -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 diff --git a/src/controllers/rest-api/auth/router.js b/src/controllers/rest-api/auth/router.js new file mode 100644 index 0000000..c826321 --- /dev/null +++ b/src/controllers/rest-api/auth/router.js @@ -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 diff --git a/src/controllers/rest-api/index.js b/src/controllers/rest-api/index.js new file mode 100644 index 0000000..11f32d7 --- /dev/null +++ b/src/controllers/rest-api/index.js @@ -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 } diff --git a/src/lib/ipfs.js b/src/lib/ipfs.js index 3b87836..897504f 100644 --- a/src/lib/ipfs.js +++ b/src/lib/ipfs.js @@ -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 diff --git a/src/modules/auth/router.js b/src/modules/auth/router.js deleted file mode 100644 index 56cf0e0..0000000 --- a/src/modules/auth/router.js +++ /dev/null @@ -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] - } -] diff --git a/test/e2e/automated/a02-users.rest-e2e.js b/test/e2e/automated/temp/a02-users.rest-e2e.js similarity index 100% rename from test/e2e/automated/a02-users.rest-e2e.js rename to test/e2e/automated/temp/a02-users.rest-e2e.js diff --git a/test/e2e/automated/a04-contact.rest-e2e.js b/test/e2e/automated/temp/a04-contact.rest-e2e.js similarity index 100% rename from test/e2e/automated/a04-contact.rest-e2e.js rename to test/e2e/automated/temp/a04-contact.rest-e2e.js diff --git a/test/e2e/automated/a06-logapi.rest-e2e.js b/test/e2e/automated/temp/a06-logapi.rest-e2e.js similarity index 100% rename from test/e2e/automated/a06-logapi.rest-e2e.js rename to test/e2e/automated/temp/a06-logapi.rest-e2e.js diff --git a/test/e2e/automated/a08-validators.rest-e2e.js b/test/e2e/automated/temp/a08-validators.rest-e2e.js similarity index 100% rename from test/e2e/automated/a08-validators.rest-e2e.js rename to test/e2e/automated/temp/a08-validators.rest-e2e.js diff --git a/test/e2e/automated/a09-admin.rest-e2e.js b/test/e2e/automated/temp/a09-admin.rest-e2e.js similarity index 100% rename from test/e2e/automated/a09-admin.rest-e2e.js rename to test/e2e/automated/temp/a09-admin.rest-e2e.js