mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
feat(modules): Removed src/modules folder. Ported all REST API to /src/controllers/ folder
This commit is contained in:
+2
-2
@@ -57,8 +57,8 @@ async function startServer () {
|
||||
restApi.attachControllers(app)
|
||||
|
||||
// Custom Middleware Modules
|
||||
const modules = require('../src/modules')
|
||||
modules(app)
|
||||
// const modules = require('../src/modules')
|
||||
// modules(app)
|
||||
|
||||
// Enable CORS for testing
|
||||
// THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
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')
|
||||
@@ -52,6 +53,10 @@ function attachRESTControllers (app) {
|
||||
// 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 /logs route
|
||||
const logsRESTController = new LogsRESTController()
|
||||
logsRESTController.attach(app)
|
||||
}
|
||||
|
||||
// Add the JSON RPC router to the ipfs-coord adapter.
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
const LogsApiLib = require('../../lib/logapi')
|
||||
const LogsApiLib = require('../../../lib/logapi')
|
||||
const logsApiLib = new LogsApiLib()
|
||||
let _this
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
REST API library for /logs route.
|
||||
*/
|
||||
|
||||
const LogsRESTRouter = require('./router')
|
||||
|
||||
class LogsRESTController {
|
||||
constructor (localConfig = {}) {
|
||||
this.logsRESTRouter = new LogsRESTRouter()
|
||||
}
|
||||
|
||||
attach (app) {
|
||||
this.logsRESTRouter.attachControllers(app)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LogsRESTController
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
REST Router for the /logs route.
|
||||
*/
|
||||
|
||||
// Public npm libraries.
|
||||
const Router = require('koa-router')
|
||||
|
||||
// Local libraries.
|
||||
const LogsRESTControllerLib = require('./controller')
|
||||
const Validators = require('../middleware/validators')
|
||||
|
||||
// let _this
|
||||
|
||||
class LogsRESTRouter {
|
||||
constructor (localConfig = {}) {
|
||||
// Encapsulate dependencies.
|
||||
this.logsRESTController = new LogsRESTControllerLib()
|
||||
this.validators = new Validators()
|
||||
|
||||
// Instantiate the router and set the base route.
|
||||
const baseUrl = '/logs'
|
||||
this.router = new Router({ prefix: baseUrl })
|
||||
|
||||
// _this = this
|
||||
}
|
||||
|
||||
attachControllers (app) {
|
||||
if (!app) {
|
||||
throw new Error(
|
||||
'Must pass app object when attaching REST API controllers.'
|
||||
)
|
||||
}
|
||||
|
||||
// Define the routes and attach the controller.
|
||||
this.router.post('/', this.logsRESTController.getLogs)
|
||||
|
||||
// Attach the Controller routes to the Koa app.
|
||||
app.use(this.router.routes())
|
||||
app.use(this.router.allowedMethods())
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LogsRESTRouter
|
||||
@@ -1,55 +0,0 @@
|
||||
const glob = require('glob')
|
||||
const Router = require('koa-router')
|
||||
|
||||
module.exports = function initModules (app) {
|
||||
glob(
|
||||
`${__dirname.toString()}/*`,
|
||||
{ ignore: '**/index.js' },
|
||||
(err, matches) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
// Loop through each sub-directory in the modules directory.
|
||||
matches.forEach((mod) => {
|
||||
// console.log(`router = ${mod}/router`)
|
||||
const router = require(`${mod}/router`)
|
||||
|
||||
const routes = router.routes
|
||||
const baseUrl = router.baseUrl
|
||||
const instance = new Router({ prefix: baseUrl })
|
||||
|
||||
// console.log(`routes: ${JSON.stringify(routes, null, 2)}`)
|
||||
|
||||
// Loop through each route defined in the router.js file.
|
||||
routes.forEach((config) => {
|
||||
// console.log(`modules/index.js config: ${JSON.stringify(config, null, 2)}`)
|
||||
// const {
|
||||
// method = '',
|
||||
// route = '',
|
||||
// handlers = []
|
||||
// } = config
|
||||
const method = config.method || ''
|
||||
const route = config.route || ''
|
||||
const handlers = config.handlers || []
|
||||
|
||||
const lastHandler = handlers.pop()
|
||||
|
||||
instance[method.toLowerCase()](
|
||||
route,
|
||||
...handlers,
|
||||
async function (ctx) {
|
||||
// console.log(`typeof lastHandler: ${typeof (lastHandler)}`)
|
||||
// return await lastHandler(ctx)
|
||||
return lastHandler(ctx)
|
||||
}
|
||||
)
|
||||
|
||||
// console.log(`instance: ${JSON.stringify(instance, null, 2)}`)
|
||||
|
||||
app.use(instance.routes()).use(instance.allowedMethods())
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
// const validator = require('../../middleware/validators')
|
||||
const LogApi = require('./controller')
|
||||
const logApi = new LogApi()
|
||||
|
||||
module.exports.baseUrl = '/logapi'
|
||||
|
||||
module.exports.routes = [
|
||||
{
|
||||
method: 'POST',
|
||||
route: '/',
|
||||
handlers: [logApi.getLogs]
|
||||
}
|
||||
/*
|
||||
{
|
||||
method: 'GET',
|
||||
route: '/',
|
||||
handlers: [validator.ensureUser, user.getUsers]
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
route: '/:id',
|
||||
handlers: [validator.ensureUser, user.getUser]
|
||||
},
|
||||
{
|
||||
method: 'PUT',
|
||||
route: '/:id',
|
||||
handlers: [validator.ensureTargetUserOrAdmin, user.getUser, user.updateUser]
|
||||
},
|
||||
{
|
||||
method: 'DELETE',
|
||||
route: '/:id',
|
||||
handlers: [validator.ensureTargetUserOrAdmin, user.getUser, user.deleteUser]
|
||||
}
|
||||
*/
|
||||
]
|
||||
+11
-5
@@ -9,7 +9,7 @@ util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const LOCALHOST = `http://localhost:${config.port}`
|
||||
|
||||
const LogsController = require('../../../src/modules/logapi/controller')
|
||||
const LogsController = require('../../../src/controllers/rest-api/logs/controller')
|
||||
const mockContext = require('../../unit/mocks/ctx-mock').context
|
||||
|
||||
let sandbox
|
||||
@@ -23,12 +23,12 @@ describe('LogsApi', () => {
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('POST /logapi', () => {
|
||||
describe('POST /logs', () => {
|
||||
it('should return false if password is not provided', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'post',
|
||||
url: `${LOCALHOST}/logapi`,
|
||||
url: `${LOCALHOST}/logs`,
|
||||
data: {}
|
||||
}
|
||||
|
||||
@@ -38,11 +38,12 @@ describe('LogsApi', () => {
|
||||
assert(false, 'Unexpected result')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return log', async () => {
|
||||
try {
|
||||
const options = {
|
||||
method: 'post',
|
||||
url: `${LOCALHOST}/logapi`,
|
||||
url: `${LOCALHOST}/logs`,
|
||||
data: {
|
||||
password: 'test'
|
||||
}
|
||||
@@ -59,6 +60,7 @@ describe('LogsApi', () => {
|
||||
assert(false, 'Unexpected result')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return false if files are not found!', async () => {
|
||||
try {
|
||||
sandbox.stub(uut.logsApiLib, 'getLogs').resolves({
|
||||
@@ -80,10 +82,13 @@ describe('LogsApi', () => {
|
||||
assert.fail('Unexpected result')
|
||||
}
|
||||
})
|
||||
|
||||
it('should catch and handle errors', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.logsApiLib.fs, 'existsSync').throws(new Error('test error'))
|
||||
sandbox
|
||||
.stub(uut.logsApiLib.fs, 'existsSync')
|
||||
.throws(new Error('test error'))
|
||||
|
||||
// Mock the context object.
|
||||
const ctx = mockContext()
|
||||
@@ -101,6 +106,7 @@ describe('LogsApi', () => {
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw unhandled error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
@@ -6,7 +6,7 @@
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
|
||||
const LogsApiController = require('../../../src/modules/logapi/controller')
|
||||
const LogsApiController = require('../../../src/controllers/rest-api/logs/controller')
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
@@ -14,8 +14,7 @@ let ctx
|
||||
const mockContext = require('../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('Logapi', () => {
|
||||
before(async () => {
|
||||
})
|
||||
before(async () => {})
|
||||
|
||||
beforeEach(() => {
|
||||
uut = new LogsApiController()
|
||||
@@ -42,7 +41,9 @@ describe('Logapi', () => {
|
||||
it('should return 500 status on biz logic Unhandled error', async () => {
|
||||
try {
|
||||
// eslint-disable
|
||||
sandbox.stub(uut.logsApiLib, 'getLogs').returns(Promise.reject(new Error()))
|
||||
sandbox
|
||||
.stub(uut.logsApiLib, 'getLogs')
|
||||
.returns(Promise.reject(new Error()))
|
||||
|
||||
ctx.request.body = {
|
||||
password: 'test'
|
||||
|
||||
Reference in New Issue
Block a user