-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
222 lines (180 loc) · 7.86 KB
/
Copy pathapp.js
File metadata and controls
222 lines (180 loc) · 7.86 KB
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
213
214
215
216
217
218
219
220
221
222
'use strict';
/*******************************************************************************
* Copyright (c) 2017 TokenID .
*
* All rights reserved.
*
*******************************************************************************/
let express = require('express');
let bodyParser = require('body-parser');
let app = express();
let url = require('url');
let cors = require('cors');
let fs = require('fs');
let path = require('path');
let hfc = require('hfc');
let tracing = require(__dirname + '/server/tools/traces/trace.js');
let configFile = require(__dirname + '/server/configurations/configuration.js');
//Our own modules
let blocks = require(__dirname + '/server/blockchain/blocks/blocks.js');
let block = require(__dirname + '/server/blockchain/blocks/block/block.js');
let identity = require(__dirname + '/server/blockchain/identity/identity.js');
let issuers = require(__dirname + '/server/blockchain/issuers/issuers.js');
let startup = require(__dirname + '/server/configurations/startup/startup.js');
let http = require('http');
const SecurityContext = require(__dirname + '/server/tools/security/securitycontext');
// Object of users' names linked to their security context
let usersToSecurityContext = {};
let port = process.env.VCAP_APP_PORT || configFile.config.appPort;
//////// Pathing and Module Setup ////////
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// Enable CORS preflight across the board.
app.options('*', cors());
app.use(cors());
app.use(express.static(__dirname + '/Client_Side'));
app.use('/node_modules', express.static(__dirname + '/node_modules'));
//===============================================================================================
// Routing
//===============================================================================================
//-----------------------------------------------------------------------------------------------
// Blockchain - Identity
//-----------------------------------------------------------------------------------------------
app.post('/blockchain/identity/:providerEnrollmentID', function (req, res, next) {
identity.create(req, res, next);
});
app.post('/blockchain/identity/initialize/new', function (req, res, next) {
identity.initialize(req, res, next);
});
app.delete('/blockchain/identity/:providerEnrollmentID/:identityCode', function (req, res, next) {
identity.removeIdentity(req, res, next);
});
app.get('/blockchain/identity/:providerEnrollmentID', function (req, res, next) {
identity.getIdentities(req, res, next);
});
app.get('/blockchain/identity/:providerEnrollmentID/publicKey', function (req, res, next) {
identity.getPublicKey(req, res, next);
});
app.get('/blockchain/identity/:providerEnrollmentID/:identityCode', function (req, res, next) {
identity.getIdentity(req, res, next);
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Blocks8d55b8daf0
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/blocks', function (req, res, next) {
blocks.read(req, res, next, usersToSecurityContext);
});
app.get('/blockchain/blocks/:blockNum(\\d+)', function (req, res, next) {
block.read(req, res, next, usersToSecurityContext);
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Issuers
//-----------------------------------------------------------------------------------------------
app.post('/blockchain/issuers', function (req, res, next) {
issuers.create(req, res, next);
});
/////////// Configure Webserver ///////////
app.use(function (req, res, next) {
let keys;
console.log('------------------------------------------ incoming request ------------------------------------------');
console.log('New ' + req.method + ' request for', req.url);
let url_parts = url.parse(req.url, true);
req.parameters = url_parts.query;
keys = Object.keys(req.parameters);
if (req.parameters && keys.length > 0) { console.log({ parameters: req.parameters }); } //print request parameters
keys = Object.keys(req.body);
if (req.body && keys.length > 0) { console.log({ body: req.body }); } //print request body
next();
});
////////////////////////////////////////////
////////////// Error Handling //////////////
////////////////////////////////////////////
app.use(function (req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function (err, req, res, next) { // = development error handler, print stack trace
console.log('Error Handler -', req.url, err);
let errorCode = err.status || 500;
res.status(errorCode);
//res.render('template/error', {bag: req.bag});
res.send({ 'message': err });
});
// Track the application deployments
require('cf-deployment-tracker-client').track();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
process.env.NODE_ENV = 'production';
process.env.GOPATH = path.resolve(__dirname, 'Chaincode');
let vcapServices;
let pem;
let server;
let registrar;
let credentials;
let webAppAdminPassword = configFile.config.registrar_password;
if (process.env.VCAP_SERVICES) {
console.log('\n[!] VCAP_SERVICES detected');
port = process.env.PORT;
} else {
port = configFile.config.appPort;
}
// Setup HFC
let chain = hfc.newChain(configFile.config.chainName);
//This is the location of the key store HFC will use. If running locally, this directory must exist on your machine
chain.setKeyValStore(hfc.newFileKeyValStore(configFile.config.keyStoreLocation));
//TODO: Change this to be a boolean stating if ssl is enabled or disabled
//Retrieve the certificate if grpcs is being used
if (configFile.config.hfcProtocol === 'grpcs') {
chain.setECDSAModeForGRPC(true);
pem = fs.readFileSync(__dirname + '/' + configFile.config.certificateFileName, 'utf8');
}
if (pem) { // We are running outside bluemix, connecting to bluemix fabric
console.log('\n[!] Running locally with bluemix fabric');
credentials = fs.readFileSync(__dirname + '/credentials.json');
credentials = JSON.parse(credentials);
webAppAdminPassword = configFile.config.bluemix_registrar_password;
startup.connectToPeers(chain, credentials.peers, pem);
startup.connectToCA(chain, credentials.ca, pem);
//startup.connectToEventHub(chain, credentials.peers[0], pem);
} else { // We are running locally
let credentials = fs.readFileSync(__dirname + '/credentials.json');
credentials = JSON.parse(credentials);
startup.connectToPeers(chain, credentials.peers);
startup.connectToCA(chain, credentials.ca);
//startup.connectToEventHub(chain, credentials.peers[0]);
}
//chain.getEventHub().disconnect();
server = http.createServer(app).listen(port, function () {
console.log('Server Up');
tracing.create('INFO', 'Startup complete on port', server.address().port);
});
server.timeout = 2400000;
let demoStatus = {
status: 'IN_PROGRESS',
success: false,
error: null
};
let eventEmitter;
let io = require('socket.io')(server);
io.sockets.on('connection', (socket) => {
eventEmitter = socket;
console.log('connected');
eventEmitter.emit('setup', demoStatus);
});
let chaincodeID;
return startup.enrollRegistrar(chain, configFile.config.registrar_name, webAppAdminPassword)
.then(function (r) {
chain.setRegistrar(r);
tracing.create('INFO', 'Startup', 'Set registrar');
return startup.loadEnrolledMember(chain, configFile.config.enrollment.name)
})
.then(function (member) {
let sc = new SecurityContext(member);
//If deployed in bluemix
configFile.config.certPath = configFile.config.certPath;
configFile.config.securityContext = sc;
})
.catch(function (err) {
console.log(err);
tracing.create('ERROR', 'Startup', err);
});