This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
forked from olegabu/fabric-starter-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
212 lines (168 loc) · 7.66 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const logger = require('log4js').getLogger('app');
const jsonwebtoken = require('jsonwebtoken');
const jwt = require('express-jwt');
const cors = require('cors');
const path = require('path');
const SocketServer = require('socket.io');
const multer = require('multer');
const os = require('os');
const storage = os.tmpdir() || './upload';
const upload = multer({ dest: storage});
let cpUpload = upload.fields([{ name: 'file', maxCount: 1 }, { name: 'channelId', maxCount: 1 },{ name: 'targets'},{ name: 'version', maxCount: 1 },{ name: 'language', maxCount: 1 }]);
const FabricStarterClient = require('./fabric-starter-client');
let fabricStarterClient = new FabricStarterClient();
// parse json payload and urlencoded params in GET
app.use(bodyParser.json({ limit: '100MB', type:'application/json'}));
app.use(bodyParser.urlencoded({extended: true, limit: '100MB'}));
// allow CORS from all urls
app.use(cors());
app.options('*', cors());
// serve web app as static
const webappDir = process.env.WEBAPP_DIR || './webapp';
app.use('/webapp', express.static(webappDir));
logger.info('serving webapp at /webapp from ' + webappDir);
// serve msp directory with certificates as static
const mspDir = process.env.MSP_DIR || './msp';
const serveIndex = require('serve-index');
//TODO serveIndex should show directory listing to find certs but not working
app.use('/msp', express.static(mspDir), serveIndex('/msp', {'icons': true}));
logger.info('serving certificates at /msp from ' + mspDir);
// serve favicon
const favicon = require('serve-favicon');
app.use(favicon(path.join(webappDir, 'favicon.ico')));
// catch promise rejections and return 500 errors
const asyncMiddleware = fn =>
(req, res, next) => {
// logger.debug('asyncMiddleware');
Promise.resolve(fn(req, res, next))
.catch(e => {
logger.error('asyncMiddleware', e);
res.status(500).json(e.message);
next();
});
};
// require presence of JWT in Authorization Bearer header
const jwtSecret = fabricStarterClient.getSecret();
app.use(jwt({secret: jwtSecret}).unless({path: ['/', '/users', '/mspid', /\/consortium/]}));
// use fabricStarterClient for every logged in user
const mapFabricStarterClient = {};
app.use(async (req, res, next) => {
if (req.user) {
const login = req.user.sub;
let client = mapFabricStarterClient[login];
if(client) {
logger.debug('cached client for', login);
fabricStarterClient = client;
} else {
logger.debug('new client for', login);
await fabricStarterClient.init();
try {
await fabricStarterClient.loginOrRegister(login);
} catch(e) {
logger.error('loginOrRegister', e);
res.status(500).json(e.message);
}
mapFabricStarterClient[login] = fabricStarterClient;
}
}
next();
});
const appRouter = (app) => {
app.get('/', (req, res) => {
res.status(200).send('Welcome to fabric-starter REST server');
});
app.get('/mspid', (req, res) => {
res.json(fabricStarterClient.getMspid());
});
//TODO use for development only as it may expose sensitive data
app.get('/config', (req, res) => {
res.json(fabricStarterClient.getNetworkConfig());
});
app.get('/chaincodes', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryInstalledChaincodes());
}));
app.post('/chaincodes', cpUpload, asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.installChaincode(req.body.channelId, req.files['file'][0].originalname.substring(0, req.files['file'][0].originalname.length-4),
req.files['file'][0].path, req.body.version, req.body.language, req.body.targets.split(','), storage));
}));
app.post('/users', asyncMiddleware(async (req, res, next) => {
await fabricStarterClient.init();
await fabricStarterClient.loginOrRegister(req.body.username, req.body.password);
mapFabricStarterClient[req.body.username] = fabricStarterClient;
const token = jsonwebtoken.sign({sub: fabricStarterClient.user.getName()}, jwtSecret);
logger.debug('token', token);
res.json(token);
}));
app.get('/channels', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryChannels());
}));
app.post('/channels', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.createChannel(req.body.channelId));
}));
app.get('/channels/:channelId', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryInfo(req.params.channelId));
}));
app.get('/channels/:channelId/orgs', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.getOrganizations(req.params.channelId));
}));
app.get('/channels/:channelId/peers', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.getPeersForOrgOnChannel(req.params.channelId));
}));
app.get('/orgs/:org/peers', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.getPeersForOrg(req.params.org));
}));
app.post('/channels/:channelId/orgs', asyncMiddleware(async (req, res, next) => {
res.status(501).json('adding organization to channel not implemented');
}));
app.get('/channels/:channelId/blocks/:number', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryBlock(req.params.channelId, parseInt(req.params.number)));
}));
app.get('/channels/:channelId/transactions/:id', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryTransaction(req.params.channelId, req.params.id));
}));
app.get('/channels/:channelId/chaincodes', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.queryInstantiatedChaincodes(req.params.channelId));
}));
app.post('/channels/:channelId/chaincodes', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.instantiateChaincode(req.params.channelId, req.body.chaincodeId,
req.body.type, req.body.fcn, req.body.args, req.body.version, req.body.targets));
}));
app.get('/channels/:channelId/chaincodes/:chaincodeId', asyncMiddleware(async (req, res, next) => {
let ret = await fabricStarterClient.query(req.params.channelId, req.params.chaincodeId,
req.query.fcn, JSON.parse(req.query.args), req.query.targets);
if(ret[0].startsWith('Error')) {
throw new Error(ret[0]);
}
res.json(ret);
}));
app.post('/channels/:channelId/chaincodes/:chaincodeId', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.invoke(req.params.channelId, req.params.chaincodeId,
req.body.fcn, req.body.args, req.body.targets, req.query.waitForTransactionEvent));
}));
app.get('/consortium/members', asyncMiddleware(async (req, res, next) => {
res.json(await fabricStarterClient.getConsortiumMemberList());
}));
};
appRouter(app);
const server = app.listen(process.env.PORT || 3000, function () {
logger.info('started fabric-starter rest server on port', server.address().port);
});
async function startSocketServer() {
const io = new SocketServer(server, {origins: '*:*'});
const channels = await fabricStarterClient.queryChannels();
channels.map(c => {return c.channel_id;}).forEach(async channelId => {
await fabricStarterClient.registerBlockEvent(channelId, block => {
logger.debug(`block ${block.number} on ${block.channel_id}`);
io.emit('chainblock', block);
}, e => {
logger.error('registerBlockEvent', e);
});
logger.debug(`registered for block event on ${channelId}`);
});
}
startSocketServer().then(() => {
logger.info('started socket server');
});