forked from IBM-Blockchain-Archive/marbles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_router.js
94 lines (83 loc) · 3.2 KB
/
site_router.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
'use strict';
/* global process */
/*******************************************************************************
* Copyright (c) 2015 IBM Corp.
*
* All rights reserved.
*
*******************************************************************************/
var express = require('express');
var router = express.Router();
var helper = require(__dirname + '/../utils/helper.js')(process.env.creds_filename, console);
//anything in here gets passed to Pug template engine
function build_bag(req) {
return {
e: process.error, //send any setup errors
creds_filename: process.env.creds_filename,
jshash: process.env.cachebust_js, //js cache busting hash (not important)
csshash: process.env.cachebust_css, //css cache busting hash (not important)
marble_company: process.env.marble_company,
creds: get_credential_data()
};
}
//get cred data
function get_credential_data() {
const channel = helper.getChannelId();
const first_org = helper.getClientOrg();
const first_ca = helper.getFirstCaName(first_org);
const first_peer = helper.getFirstPeerName(channel);
const first_orderer = helper.getFirstOrdererName(channel);
var ret = {
admin_id: helper.getEnrollObj(first_ca, 0).enrollId,
admin_secret: helper.getEnrollObj(first_ca, 0).enrollSecret,
orderer: helper.getOrderersUrl(first_orderer),
ca: helper.getCasUrl(first_ca),
peer: helper.getPeersUrl(first_peer),
chaincode_id: helper.getChaincodeId(),
channel: helper.getChannelId(),
chaincode_version: helper.getChaincodeVersion(),
marble_owners: helper.getMarbleUsernames(),
};
for (var i in ret) {
if (ret[i] == null) ret[i] = ''; //set to blank if not found
}
return ret;
}
// ============================================================================================================================
// Root
// ============================================================================================================================
router.route('/').get(function (req, res) {
res.redirect('/home');
});
// ============================================================================================================================
// Login
// ============================================================================================================================
router.route('/login').get(function (req, res) {
res.render('login', { title: 'Marbles - Login', bag: build_bag(req) });
});
router.route('/login').post(function (req, res) {
req.session.user = { username: 'Admin' };
res.redirect('/home');
});
router.route('/logout').get(function (req, res) {
req.session.destroy();
res.redirect('/login');
});
// ============================================================================================================================
// Home
// ============================================================================================================================
router.route('/home').get(function (req, res) {
route_me(req, res);
});
router.route('/create').get(function (req, res) {
route_me(req, res);
});
function route_me(req, res) {
if (!req.session.user || !req.session.user.username) {
res.redirect('/login');
}
else {
res.render('marbles', { title: 'Marbles - Home', bag: build_bag(req) });
}
}
module.exports = router;