forked from cryptomeow/be
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·240 lines (185 loc) · 5.91 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
'use strict';
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var MemoryStore = require('memorystore')(session);
var env = require("./app/env.js");
var simpleGit = require('simple-git');
var utils = require("./app/utils.js");
var moment = require("moment");
var Decimal = require('decimal.js');
var bitcoinCore = require("bitcoin-core");
var pug = require("pug");
var momentDurationFormat = require("moment-duration-format");
var rpcApi = require("./app/rpcApi.js");
var coins = require("./app/coins.js");
var request = require("request");
var qrcode = require("qrcode");
var baseActionsRouter = require('./routes/baseActionsRouter');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
// ref: https://blog.stigok.com/post/disable-pug-debug-output-with-expressjs-web-app
app.engine('pug', (path, options, fn) => {
options.debug = false;
return pug.__express.call(null, path, options, fn);
});
app.set('view engine', 'pug');
app.use(logger('production'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(session({
// secret: env.cookiePassword,
// resave: false,
// saveUninitialized: false
// }));
app.use(session({
store: new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
}),
resave: false,
saveUninitialized: false,
secret: env.cookiePassword,
}))
app.use(express.static(path.join(__dirname, 'public')));
function refreshExchangeRate() {
if (coins[env.coin].exchangeRateData) {
request(coins[env.coin].exchangeRateData.jsonUrl, function(error, response, body) {
if (!error && response && response.statusCode && response.statusCode == 200) {
var responseBody = JSON.parse(body);
var exchangeRate = coins[env.coin].exchangeRateData.responseBodySelectorFunction(responseBody);
if (exchangeRate > 0) {
global.exchangeRate = exchangeRate;
global.exchangeRateUpdateTime = new Date();
console.log("Using exchange rate: " + global.exchangeRate + " " + coins[env.coin].exchangeRateData.exchangedCurrencyName + "/" + coins[env.coin].nameShort + " starting at " + global.exchangeRateUpdateTime);
} else {
console.log("Unable to get exchange rate data");
}
} else {
console.log("Error:");
console.log(error);
console.log("Response:");
console.log(response.body ? response.body : response);
}
});
}
}
app.runOnStartup = function() {
global.env = env;
global.coinConfig = coins[env.coin];
global.coinConfigs = coins;
console.log("Running RPC Explorer for coin: " + global.coinConfig.name);
if (env.donationAddresses) {
var getDonationAddressQrCode = function(coinId) {
qrcode.toDataURL(env.donationAddresses[coinId].address, function(err, url) {
global.donationAddressQrCodeUrls[coinId] = url;
});
};
global.donationAddressQrCodeUrls = {};
env.donationAddresses.coins.forEach(function(item) {
getDonationAddressQrCode(item);
});
}
if (global.sourcecodeVersion == null) {
simpleGit(".").log(["-n 1"], function(err, log) {
global.sourcecodeVersion = log.all[0].hash.substring(0, 10);
global.sourcecodeDate = log.all[0].date.substring(0, "0000-00-00".length);
});
}
if (global.exchangeRate == null) {
refreshExchangeRate();
}
// refresh exchange rate periodically
setInterval(refreshExchangeRate, 5 * 60 * 1000);
};
app.use(function(req, res, next) {
// make session available in templates
res.locals.session = req.session;
res.locals.debug = env.debug;
if (env.rpc) {
req.session.host = env.rpc.host;
req.session.port = env.rpc.port;
req.session.username = env.rpc.username;
global.client = new bitcoinCore({
host: env.rpc.host,
port: env.rpc.port,
username: env.rpc.username,
password: env.rpc.password,
timeout: 5000
});
}
res.locals.env = global.env;
res.locals.coinConfig = global.coinConfig;
res.locals.host = req.session.host;
res.locals.port = req.session.port;
res.locals.genesisBlockHash = rpcApi.getGenesisBlockHash();
res.locals.genesisCoinbaseTransactionId = rpcApi.getGenesisCoinbaseTransactionId();
// currency format type
if (!req.session.currencyFormatType) {
var cookieValue = req.cookies['user-setting-currencyFormatType'];
if (cookieValue) {
req.session.currencyFormatType = cookieValue;
} else {
req.session.currencyFormatType = "";
}
}
res.locals.currencyFormatType = req.session.currencyFormatType;
if (!["/", "/connect"].includes(req.originalUrl)) {
if (utils.redirectToConnectPageIfNeeded(req, res)) {
return;
}
}
if (req.session.userMessage) {
res.locals.userMessage = req.session.userMessage;
if (req.session.userMessageType) {
res.locals.userMessageType = req.session.userMessageType;
} else {
res.locals.userMessageType = "warning";
}
req.session.userMessage = null;
req.session.userMessageType = null;
}
if (req.session.query) {
res.locals.query = req.session.query;
req.session.query = null;
}
// make some var available to all request
// ex: req.cheeseStr = "cheese";
next();
});
app.use('/', baseActionsRouter);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('404: Page not found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (env.development) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.locals.moment = moment;
app.locals.Decimal = Decimal;
app.locals.utils = utils;
module.exports = app;