forked from ComBba/etherchain-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
274 lines (245 loc) · 8.87 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
var serverStartTime = new Date().toLocaleString();
var express = require('express');
var compression = require('compression');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var blocks = require('./routes/blocks');
var tx_recent = require('./routes/tx_recent');
var block = require('./routes/block');
var tx = require('./routes/tx');
var account = require('./routes/account');
var accounts = require('./routes/accounts');
var contract = require('./routes/contract');
var signature = require('./routes/signature');
var search = require('./routes/search');
var top100 = require('./routes/top100');
var top100_settingup = require('./routes/top100_settingup');
var peers = require('./routes/peers');
var redisblock = require('./routes/redisblock');
var hashratechart = require('./routes/hashratechart');
var servercheckchart = require('./routes/servercheckchart');
var bitzcharts = require('./routes/bitzcharts');
var api = require('./routes/api');
var api_proxy = require('./api/proxy');
var api_account = require('./api/account');
var api_info = require('./api/info');
var test_batch = require('./routes/test_batch');
var config = new(require('./config/config.js'))();
var configERC20 = new(require('./config/configERC20.js'))();
var configNames = new(require('./config/configNames.js'))();
var level = require('level-rocksdb');
var db = level(config.dbPath);
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
var app = express();
app.use(compression({
filter: shouldCompress
}));
var async = require('async');
var tokenExporterService = require('./services/tokenExporter.js');
var serverPortCheckService = require('./services/serverPortCheck.js');
var contractAccountList = [];
var tokenExporter = {};
var serverPortCheck = {};
async.waterfall([
function (callback) {
client.hgetall('esn_contracts:eventslength', function (err, replies) {
callback(null, replies);
});
},
function (result, callback) {
//console.dir(result);
var sortable = [];
for (var adr in result) {
sortable.push([adr, result[adr]]);
}
sortable.sort(function (a, b) {
return Number(a[1]) - Number(b[1]);
});
async.eachSeries(sortable, function (iter, forEachOfCallback) {
var eventslength = iter[1],
account = iter[0];
contractAccountList.push(account);
var timeout = eventslength < 1 ? parseInt(config.tokenLoadDelay, 10) : parseInt(config.tokenLoadDelay, 10) + (eventslength * 3);
//console.log(account,"start", Date.now());
tokenExporter[account] = new tokenExporterService(config.providerIpc, configERC20.erc20ABI, account, 1, timeout);
//console.log("[timeout]", account, " : ", timeout);
sleep(timeout).then(() => {
forEachOfCallback();
});
}, function (err) {
if (err) {
console.log("[ERROR] exporter listing: ", err);
callback(err, null);
} else {
app.set('tokenExporter', tokenExporter);
callback(null, contractAccountList);
}
});
}
], function (err, accountList) {
//var db = levelup(leveldown('./data'));
// ~/esn_install/parity/chaindata/chains/ethersocial/db/dc73f323b4681272/archive/db
// ~/esn_install/parity/chaindata/chains/ethersocial/db/dc73f323b4681272/snapshot/current
// /root/esn_install/parity/chaindata/chains/ethersocial/db/dc73f323b4681272/snapshot/current
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('config', config);
app.set('configERC20', configERC20);
app.set('configNames', configNames);
app.set('db', db);
app.set('trust proxy', true);
//APIs
app.all('/api_proxy/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use('/api_proxy', api_proxy);
app.all('/api_account/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use('/api_account', api_account);
app.all('/api_info/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use('/api_info', api_info);
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger(config.logFormat, {
skip: function (req, res) {
return res.statusCode == 304;
}
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.locals.tokenformatter = new(require('./utils/tokenformatter.js'))();
app.locals.moment = require('moment');
app.locals.numeral = require('numeral');
app.locals.ethformatter = require('./utils/ethformatter.js');
app.locals.numberformatter = require('./utils/numberformatter.js');
app.locals.nameformatter = new(require('./utils/nameformatter.js'))(configNames);
app.locals.nodeStatus = new(require('./utils/nodeStatus.js'))(config);
app.locals.config = config;
app.locals.configERC20 = configERC20;
app.locals.configNames = configNames;
app.use('/', index);
app.use('/block', block);
app.use('/tx', tx);
app.use('/account', account);
app.use('/accounts', accounts);
app.use('/contract', contract);
app.use('/signature', signature);
app.use('/search', search);
app.use('/top100', top100);
app.use('/blocks', blocks);
app.use('/tx_recent', tx_recent);
app.use('/top100_settingup', top100_settingup);
app.use('/peers', peers);
app.use('/redisblock', redisblock);
app.use('/hashratechart', hashratechart);
app.use('/servercheckchart', servercheckchart);
app.use('/bitzcharts', bitzcharts);
app.use('/api', api);
app.use('/test_batch', test_batch);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = {};
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
//console.dir(req.url);
var isJson = false;
var surl = req.url.split("/");
//console.dir(surl);
if (surl && surl.length > 1 && surl[1]) {
var sparam = surl[1].split("_");
//console.dir(sparam);
if (sparam && sparam.length > 1 && sparam[0] === 'api') {
isJson = true;
}
}
if (isJson) {
res.json(resultToJson(err, null));
} else {
res.render('error');
}
});
console.log("┌───────────────────────────────────────┐");
console.log("│ Token Load complete │");
console.log("└───────────────────────────────────────┘");
console.log("[Loading Start]\t", serverStartTime);
console.log("[Loading End]\t", new Date().toLocaleString());
//console.dir(accountList);
if (config.serverPortCheck) {
var serverPortCheckList = config.serverPortCheckList;
async.eachSeries(serverPortCheckList, function (server, forEachOfCallback) {
var splitIP = server.split(":");
if (splitIP.length > 1) {
serverPortCheck[server] = new serverPortCheckService(splitIP[0], parseInt(splitIP[1], 10), config.serverPortCheckDelay);
console.log('serverPortCheckService(', splitIP[0], parseInt(splitIP[1], 10), ')');
}
sleep(100).then(() => {
forEachOfCallback();
});
}, function (err) {
if (err) {
console.log("[ERROR] serverPortCheck listing: ", err);
} else {
app.set('serverPortCheck', serverPortCheck);
app.set('serverPortCheckList', serverPortCheckList);
}
});
}
});
function shouldCompress(req, res) {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function resultToJson(err, param) {
var result = {};
result.jsonrpc = '2.0';
if (err) {
result.result = err;
result.success = false;
} else if (param) {
result.result = param;
result.success = true;
} else {
result.result = NaN;
result.success = false;
}
return result;
}
module.exports = app;