forked from jchenibm/Car-Lease-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
392 lines (320 loc) · 14.1 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"use strict";
/* global process */
/* global __dirname */
/*eslint-env node*/
/*******************************************************************************
* Copyright (c) 2015 IBM Corp.
*
* All rights reserved.
*
*******************************************************************************/
/////////////////////////////////////////
///////////// Setup Node.js /////////////
/////////////////////////////////////////
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
var url = require('url');
var cors = require("cors");
var fs = require("fs");
//var cfenv = require('cfenv');
//Our own modules
var blocks = require(__dirname+'/Server_Side/blockchain/blocks/blocks.js');
var block = require(__dirname+'/Server_Side/blockchain/blocks/block/block.js');
var participants = require(__dirname+'/Server_Side/blockchain/participants/participants.js');
var identity = require(__dirname+'/Server_Side/admin/identity/identity.js');
var vehicles = require(__dirname+'/Server_Side/blockchain/assets/vehicles/vehicles.js')
var vehicle = require(__dirname+'/Server_Side/blockchain/assets/vehicles/vehicle/vehicle.js')
var vehicle_logs = require(__dirname+'/Server_Side/blockchain/vehicle_logs/vehicle_logs.js')
var demo = require(__dirname+'/Server_Side/admin/demo/demo.js')
var chaincode = require(__dirname+'/Server_Side/blockchain/chaincode/chaincode.js')
//User manager modules
var user_manager = require(__dirname+'/utils/user.js');
var configFile = require(__dirname+'/Server_Side/configurations/configuration.js');
console.log(__dirname+'/Server_Side/blockchain/blocks/block/block.js')
//// Set Server Parameters ////
var host = process.env.VCAP_APP_HOST;
var port = process.env.VCAP_APP_PORT;
console.log("Server info", host, port)
// For logging
var TAG = "app.js:";
//Connector stuff
var dataSource;
//////// Pathing and Module Setup ////////
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(session({secret: 'Somethignsomething1234!test', resave: true, saveUninitialized: true}));
// Enable CORS preflight across the board.
app.options('*', cors());
app.use(cors());
app.use(express.static(__dirname + '/Client_Side'));
//===============================================================================================
// Routing
//===============================================================================================
//-----------------------------------------------------------------------------------------------
// Admin - Identity
//-----------------------------------------------------------------------------------------------
app.post('/admin/identity', function(req, res) //Sets the session user to have the account address for the page they are currently on
{
identity.create(req, res);
});
//-----------------------------------------------------------------------------------------------
// Admin - Demo
//-----------------------------------------------------------------------------------------------
app.post('/admin/demo', function(req, res)
{
demo.create(req, res);
});
app.get('/admin/demo', function(req, res)
{
demo.read(req, res);
});
//-----------------------------------------------------------------------------------------------
// Blockchain - chaincode
//-----------------------------------------------------------------------------------------------
app.post('/blockchain/chaincode/vehicles', function(req, res){
chaincode.vehicles.create(req, res)
});
app.post('/blockchain/chaincode/vehicle_logs', function(req, res){
chaincode.vehicle_logs.create(req, res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Blocks
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/blocks', function(req, res){
blocks.read(req, res);
});
app.get('/blockchain/blocks/:blockNum(\\d+)', function(req, res){
block.read(req, res);
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles
//-----------------------------------------------------------------------------------------------
app.post('/blockchain/assets/vehicles' , function(req,res)
{
vehicles.create(req,res)
});
app.get('/blockchain/assets/vehicles' , function(req,res)
{
vehicles.read(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Owner
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/owner' , function(req,res)
{
vehicle.owner.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/owner' , function(req,res)
{
vehicle.owner.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - VIN
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/VIN' , function(req,res)
{
vehicle.VIN.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/VIN' , function(req,res)
{
vehicle.VIN.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Colour
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/colour' , function(req,res)
{
vehicle.colour.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/colour' , function(req,res)
{
vehicle.colour.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Make
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/make' , function(req,res)
{
vehicle.make.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/make' , function(req,res)
{
vehicle.make.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Model
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/model' , function(req,res)
{
vehicle.model.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/model' , function(req,res)
{
vehicle.model.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Reg
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/assets/vehicles/:v5cID/reg' , function(req,res)
{
vehicle.reg.read(req,res)
});
app.put('/blockchain/assets/vehicles/:v5cID/reg' , function(req,res)
{
vehicle.reg.update(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Assets - Vehicles - Vehicle - Scrapped
//-----------------------------------------------------------------------------------------------
app.delete('/blockchain/assets/vehicles/:v5cID' , function(req,res)
{
vehicle.delete(req,res)
});
app.get('/blockchain/assets/vehicles/:v5cID/scrap' , function(req,res)
{
vehicle.scrapped.read(req,res)
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Participants
//-----------------------------------------------------------------------------------------------
app.post('/blockchain/participants', function(req,res){
participants.create(dataSource, req.body.user,req.body.role, req.body.aff, res);
});
app.get('/blockchain/participants', function(req,res){
participants.read(req,res);
});
app.get('/blockchain/participants/regulators', function(req, res){
participants.regulators.read(req,res);
});
app.get('/blockchain/participants/manufacturers', function(req, res){
participants.manufacturers.read(req,res);
});
app.get('/blockchain/participants/dealerships', function(req, res){
participants.dealerships.read(req,res);
});
app.get('/blockchain/participants/lease_companies', function(req, res){
participants.lease_companies.read(req,res);
});
app.get('/blockchain/participants/leasees', function(req, res){
participants.leasees.read(req,res);
});
app.get('/blockchain/participants/scrap_merchants', function(req, res){
participants.scrap_merchants.read(req,res);
});
//-----------------------------------------------------------------------------------------------
// Blockchain - Vehicle Logs
//-----------------------------------------------------------------------------------------------
app.get('/blockchain/vehicle_logs', function(req,res)
{
vehicle_logs.read(req, res)
})
/////////// Configure Webserver ///////////
app.use(function (req, res, next) {
var keys;
console.log('------------------------------------------ incoming request ------------------------------------------');
console.log('New ' + req.method + ' request for', req.url);
req.bag = {}; //create my object for my stuff
req.session.count = eval(req.session.count) + 1;
req.bag.session = req.session;
var 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) {
var 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);
var errorCode = err.status || 500;
res.status(errorCode);
req.bag.error = {msg: err.stack, status: errorCode};
if (req.bag.error.status == 404) req.bag.error.msg = "Sorry, I cannot locate that file";
//res.render('template/error', {bag: req.bag});
res.send({"message":err})
});
// Track the application deployments
require("cf-deployment-tracker-client").track();
// ============================================================================================================================
// Launch Webserver
// ============================================================================================================================
var server = http.createServer(app).listen(port, function () {
});
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
process.env.NODE_ENV = 'production';
server.timeout = 2400000; // Ta-da.
console.log('------------------------------------------ Server Up - ' + host + ':' + port + ' ------------------------------------------');
// Track the application deployments
require("cf-deployment-tracker-client").track();
// ==================================
// load peers manually or from VCAP, VCAP will overwrite hardcoded list!
// ==================================
var manual = JSON.parse(fs.readFileSync('mycreds.json', 'utf8'));
var peers, users, ca;
if (manual.credentials.peers) {
console.log(TAG, 'loading', manual.credentials.peers.length, 'hardcoded peers');
peers = manual.credentials.peers;
}
if (manual.credentials.users) {
console.log(TAG, "loading", manual.credentials.users.length, "hardcoded users");
users = manual.credentials.users;
}
if (manual.credentials.ca) {
var ca_name = Object.keys(manual.credentials.ca)[0];
console.log(TAG, "loading ca:", ca_name);
ca = manual.credentials.ca[ca_name];
}
if (process.env.VCAP_SERVICES) { //load from vcap, search for service, 1 of the 3 should be found...
var servicesObject = JSON.parse(process.env.VCAP_SERVICES);
for (var i in servicesObject) {
if (i.indexOf('ibm-blockchain') >= 0) { // looks close enough (can be suffixed dev, prod, or staging)
if (servicesObject[i][0].credentials.error) {
console.log('!\n!\n! Error from Bluemix: \n', servicesObject[i][0].credentials.error, '!\n!\n');
peers = null;
users = null;
process.error = {
type: 'network',
msg: "Due to overwhelming demand the IBM Blockchain Network service is at maximum capacity. Please try recreating this service at a later date."
};
}
if (servicesObject[i][0].credentials && servicesObject[i][0].credentials.peers) {
console.log('overwritting peers, loading from a vcap service: ', i);
peers = servicesObject[i][0].credentials.peers;
var ca_name = Object.keys(servicesObject[i][0].credentials.ca)[0];
console.log(TAG, "loading ca:", ca_name);
ca = servicesObject[i][0].credentials.ca[ca_name];
if (servicesObject[i][0].credentials.users) {
console.log('overwritting users, loading from a vcap service: ', i);
users = servicesObject[i][0].credentials.users;
}
else users = null; //no security
break;
}
}
}
}
console.log("ENV VARIABLES", configFile.config.api_ip, configFile.config.api_port_external);
// Start up the network!!
//Set up connection to the CA
finalSetup();
/**
* Configures other parts of the app that depend on the blockchain network being configured and running in
* order to function.
*/
function finalSetup() {
console.log("Entering final setup")
dataSource = user_manager.setup(ca)
}