-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
198 lines (165 loc) · 5.19 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
/*
* LiskHQ/lisk-explorer
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const express = require('express');
let config = require('./config');
const routes = require('./api');
const path = require('path');
const cache = require('./cache');
const program = require('commander');
const async = require('async');
const packageJson = require('./package.json');
const split = require('split');
const logger = require('./utils/logger');
const app = express();
const utils = require('./utils');
program
.version(packageJson.version)
.option('-c, --config <path>', 'config file path')
.option('-p, --port <port>', 'listening port number')
.option('-h, --host <ip>', 'listening host name or ip')
.option('-rp, --redisPort <port>', 'redis port')
.parse(process.argv);
if (program.config) {
// eslint-disable-next-line import/no-dynamic-require
config = require(path.resolve(process.cwd(), program.config));
}
app.set('host', program.host || config.host);
app.set('port', program.port || config.port);
if (program.redisPort) {
config.redis.port = program.redisPort;
}
const client = require('./redis')(config);
app.candles = new utils.candles(config, client);
app.exchange = new utils.exchange(config);
app.knownAddresses = new utils.knownAddresses();
app.orders = new utils.orders(config, client);
app.set('version', '0.3');
app.set('strict routing', true);
app.set('lisk address', `http://${config.lisk.host}:${config.lisk.port}`);
app.set('freegeoip address', `http://${config.freegeoip.host}:${config.freegeoip.port}`);
app.set('exchange enabled', config.exchangeRates.enabled);
app.set('blockTime', config.blockTime);
app.use((req, res, next) => {
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-XSS-Protection', '1; mode=block');
const wsSrc = `ws://${req.get('host')} wss://${req.get('host')}`;
res.setHeader('Content-Security-Policy', `frame-ancestors 'none'; default-src 'self'; connect-src 'self' ${wsSrc}; img-src 'self' https://*.tile.openstreetmap.org; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com`);
return next();
});
app.use(express.static(path.join(__dirname, 'public')));
app.locals.redis = client;
app.use((req, res, next) => {
req.redis = client;
return next();
});
const morgan = require('morgan');
app.use(morgan('combined', {
skip(req, res) {
return parseInt(res.statusCode, 10) < 400;
},
stream: split().on('data', (data) => {
logger.error(data);
}),
}));
app.use(morgan('combined', {
skip(req, res) {
return parseInt(res.statusCode, 10) >= 400;
},
stream: split().on('data', (data) => {
logger.info(data);
}),
}));
const compression = require('compression');
app.use(compression());
const methodOverride = require('method-override');
app.use(methodOverride('X-HTTP-Method-Override'));
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
const allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
app.use((req, res, next) => {
logger.info(req.originalUrl);
if (req.originalUrl === undefined || req.originalUrl.split('/')[1] !== 'api') {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return next();
}
return req.redis.get(req.originalUrl, (err, json) => {
if (err) {
logger.info(err);
return next();
} else if (json) {
try {
json = JSON.parse(json);
} catch (e) {
return next();
}
return res.json(json);
}
return next();
});
});
logger.info('Loading routes...');
routes(app);
logger.info('Routes loaded');
app.use((req, res, next) => {
logger.info(req.originalUrl);
if (req.originalUrl === undefined || req.originalUrl.split('/')[1] !== 'api' || !req.json) {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return res.json(req.json);
}
const ttl = cache.cacheTTLOverride[req.originalUrl] || config.cacheTTL;
req.redis.setex(req.originalUrl, ttl, JSON.stringify(req.json), (err) => {
if (err) {
logger.info(err);
}
});
return res.json(req.json);
});
app.get('*', (req, res, next) => {
if (req.url.indexOf('api') !== 1) {
return res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
return next();
});
async.parallel([
(cb) => {
app.exchange.loadRates();
cb(null);
},
], () => {
const server = app.listen(app.get('port'), app.get('host'), (err) => {
if (err) {
logger.info(err);
} else {
logger.info(`Lisk Explorer started at ${app.get('host')}:${app.get('port')}`);
const io = require('socket.io').listen(server);
require('./sockets')(app, io);
}
});
});