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