-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
87 lines (74 loc) · 2.48 KB
/
main.js
File metadata and controls
87 lines (74 loc) · 2.48 KB
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
var express = require('express');
var fs = require('fs');
var app = express(express.logger());
var indexPage;
var libRequest = require('request');
var mongoose = require('mongoose');
var config = require('./config.js');
var Stats30s;
var Stats1hr;
// Max number of statistic elements that can be sent through /stats/*
var num30s6hElements = 360; // 6hr period
var num1hr24hElements = 24; // 24hr period
var num1hr3dElements = 72; // 72hr (3 day) period
var getLastNElements = function(arr, n) {
return arr.slice(Math.max(arr.length - n, 0));
};
var indexLoadFail = function(e) {
console.log('Problem loading index.html -- "' + e + '"');
process.exit(1);
};
var loadIndex = function() {
try {
indexPage = fs.readFileSync('index.html').toString();
} catch(e) {
indexLoadFail(e);
}
};
app.get('/', function(request, response) {
if(indexPage === undefined) {
indexLoadFail('EXCEPTION NOT CAUGHT');
}
response.send(indexPage);
});
app.get('/stats/30s/6h', function(request, response) {
Stats30s.find({}, {_id:0, __v:0}, function (err, stats) {
if (!err) {
response.send(getLastNElements(stats, num30s6hElements));
} else {
response.send("Error: Bityank Coinbase relay down. Please try again shortly.");
}
});
});
app.get('/stats/1hr/24h', function(request, response) {
Stats1hr.find({}, {_id:0, __v:0}, function (err, stats) {
if (!err) {
response.send(getLastNElements(stats, num1hr24hElements));
} else {
response.send("Error: Bityank Coinbase relay down. Please try again shortly.");
}
});
});
app.get('/stats/1hr/3d', function(request, response) {
Stats1hr.find({}, {_id:0, __v:0}, function (err, stats) {
if (!err) {
response.send(getLastNElements(stats, num1hr3dElements));
} else {
response.send("Error: Bityank Coinbase relay down. Please try again shortly.");
}
});
});
var port = process.env.PORT || 8080;
app.listen(port, function() {
loadIndex();
console.log("Listening on " + port);
mongoose.connect(config.dburl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
var statsSchema30s = mongoose.Schema({"time":Number, buytotal: Number, selltotal: Number});
var statsSchema1hr = mongoose.Schema({"timestart":Number, "timeend":Number, buytotal: Number, selltotal: Number});
Stats30s = mongoose.model('stats30s', statsSchema30s);
Stats1hr = mongoose.model('stats1hr', statsSchema1hr);
});
});