This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (120 loc) · 3.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
'use strict';
var express = require('express'),
config = require('./config.json').configuration,
client = require('./redis')(config),
development = config.development,
production = config.production,
routes = require('./api'),
path = require('path'),
cache = require('./cache'),
async = require('async');
var app = express(), utils = require('./utils');
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('crypti address', 'http://' + config.crypti.host + ':' + config.crypti.port);
app.set('freegeoip address', 'http://' + config.freegeoip.host + ':' + config.freegeoip.port);
app.set('fixed point', config.fixedPoint);
app.locals.redis = client;
app.use(function (req, res, next) {
req.redis = client;
return next();
});
app.use(express.logger());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.compress());
var methodOverride = require('method-override');
app.use(methodOverride('X-HTTP-Method-Override'));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
if (process.env.NODE_ENV === 'production') {
app.set('host', production.host);
app.set('port', production.port);
} else {
app.set('host', development.host);
app.set('port', development.port);
}
app.use(function (req, res, next) {
if (req.originalUrl.split('/')[1] !== 'api') {
return next();
}
console.log(req.originalUrl);
if (req.originalUrl === undefined) {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return next();
} else {
req.redis.get(req.originalUrl, function (err, json) {
if (err) {
console.log(err);
return next();
} else if (json) {
try {
json = JSON.parse(json);
} catch (e) {
return next();
}
return res.json(json);
} else {
return next();
}
});
}
});
console.log('Loading routes...');
routes(app);
console.log('Routes loaded');
app.use(function (req, res, next) {
console.log(req.originalUrl.split('/')[1]);
if (req.originalUrl.split('/')[1] !== 'api') {
return next();
}
if (req.originalUrl === undefined) {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return res.json(req.json);
} else {
req.redis.set(req.originalUrl, JSON.stringify(req.json), function (err) {
if (err) {
console.log(err);
} else {
var ttl = cache.cacheTTLOverride[req.originalUrl] || config.cacheTTL;
req.redis.send_command('EXPIRE', [req.originalUrl, ttl], function (err) {
if (err) {
console.log(err);
}
});
}
});
return res.json(req.json);
}
});
app.get('*', function (req, res, next) {
if (req.url.indexOf('api') !== 1) {
return res.sendfile(path.join(__dirname, 'public', 'index.html'));
} else {
return next();
}
});
async.parallel([
function (cb) { app.exchange.loadBTCUSD(cb); },
function (cb) { app.exchange.loadXCRBTC(cb); }
], function (err) {
var server = app.listen(app.get('port'), app.get('host'), function (err) {
if (err) {
console.log(err);
} else {
console.log('Crypti started at ' + app.get('host') + ':' + app.get('port'));
var io = require('socket.io').listen(server);
require('./sockets')(app, io);
}
});
});