Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up rpc and add POST with parameters #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dateformat": "^3.0.2",
"isomorphic-fetch": "^2.2.1",
"koa": "^2.6.2",
"koa-bodyparser": "^4.2.1",
"koa-cors": "0.0.16",
"koa-mount": "^4.0.0",
"koa-router": "^7.4.0",
Expand Down
2 changes: 2 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Router = require('koa-router');
const Static = require('koa-static');
const Cors = require('koa-cors');
const Mount = require('koa-mount');
const BodyParser = require('koa-bodyparser');
const path = require('path');

const routePictures = require('./route/pictures');
Expand All @@ -20,6 +21,7 @@ routeCBeamRPC(router);

app
.use(Cors())
.use(BodyParser())
.use(router.routes())
.use(router.allowedMethods())
.use(Static(path.resolve(__dirname, '../dist'), {}))
Expand Down
35 changes: 26 additions & 9 deletions server/route/cbeamRpc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
require('isomorphic-fetch');

const rpcRequest = async (method, params = {}) => {
const body = JSON.stringify({
jsonrpc: '2.0',
id: +new Date(),
method,
params,
});
const res = await fetch('http://c-beam.cbrp3.c-base.org:4254/rpc/', {
headers: {
'content-type': 'application/json',
},
mode: 'cors',
credentials: 'omit',
method: 'POST',
body,
});
return res;
};

module.exports = (router) => {
router.get('/rpc/:method', async (ctx) => {
const res = await fetch('http://c-beam.cbrp3.c-base.org:4254/rpc/', {
credentials: 'omit',
headers: {
'content-type': 'application/json',
},
body: `{"jsonrpc":"2.0","id":1550009052773,"method":"${ctx.params.method}","params":""}`,
method: 'POST',
mode: 'cors',
});
const res = await rpcRequest(ctx.params.method);
ctx.assert((res.status === 200), res.status);
const body = await res.json();
ctx.body = body;
});
router.post('/rpc/:method', async (ctx) => {
const res = await rpcRequest(ctx.params.method, ctx.request.body);
ctx.assert((res.status === 200), res.status);
const body = await res.json();
ctx.body = body;
Expand Down