Working towards a JSON RPC

This commit is contained in:
Chris Troutner
2021-04-01 08:35:07 -07:00
parent 1de1f2a81f
commit 03543fb38d
2 changed files with 35 additions and 1 deletions
+15 -1
View File
@@ -2,23 +2,29 @@
This is the parent class library for the RPC controller.
*/
// Public npm libraries
const jsonrpc = require('jsonrpc-lite')
// Local support libraries
const UserController = require('./users')
let _this
class JSONRPC {
constructor (localConfig) {
// Encapsulate dependencies
this.jsonrpc = jsonrpc
this.userController = new UserController()
_this = this
}
// This method takes a raw string of data from IPFS, parses it, and determins
// which controller to route the instruction to.
router (str) {
router (str, from) {
try {
console.log('router str: ', str)
console.log('router from: ', from)
const parsedData = _this.jsonrpc.parse(str)
console.log('parsedData: ', parsedData)
@@ -28,6 +34,14 @@ class JSONRPC {
if (parsedData.type === 'invalid') {
return
}
switch (parsedData.payload.id) {
case 'users':
_this.userController.userRouter(parsedData)
break
// case default:
// TODO: Return an error
}
} catch (err) {
console.error('Error in rpc router(): ', err)
// Do not throw error. This is a top-level function.
+20
View File
@@ -6,6 +6,26 @@ class UsersRPC {
constructor (localConfig) {
console.log('instantiating UsersRPC')
}
userRouter (rpcData) {
try {
console.log('userRouter rpcData: ', rpcData)
if (rpcData.payload.method === 'getAll') this.getAll()
} catch (err) {
console.error('Error in UsersRPC/rpcRouter()')
throw err
}
}
async getAll () {
try {
console.log('Executing get all')
} catch (err) {
console.error('Error in getAll()')
throw err
}
}
}
module.exports = UsersRPC