-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
143 lines (120 loc) · 4.63 KB
/
routes.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
const next = require('next')
const express = require('express')
const axios = require("axios");
const mongoose = require('mongoose');
const FactomBlocks = require('./models/FactomBlocksSchema');
const NotificationsOff = require('./models/NotificationsOff');
const PendingNotifications = require('./models/PendingNotifications');
require('dotenv').config();
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
mongoose.connection
.on('connected', () => { console.log(`Connected to database`)/*, CallHarm()*/ })
.on('error', (err) => { console.log(`Connection error: ${err.message}`) });
app.prepare().then(() => {
const server = express()
server.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
FindFactomBlocks = () => {
return FactomBlocks.find({ btc_conf: { $exists: false } }, null, { sort: { height: -1 } })
}
FindLastNotificationSetOff = () => {
return NotificationsOff.find({}, null, { sort: { time: -1 } })
}
FindLastPendingNoti = () => {
return PendingNotifications.find({}, null, { sort: { time: -1 } })
}
FindFactomsBitcoinBalance = () => {
return axios({ method: "get", url: `https://blockchain.info/q/addressbalance/1K2SXgApmo9uZoyahvsbSanpVWbzZWVVMF` })
.then(res => { return res.data })
.catch(err => console.log("Address Balance Error ", err))
}
FindBlocks = () => {
return FactomBlocks.find({}, null, { sort: { height: -1 } })
}
server.get('/', async (req, res) => {
let blockList = await Promise.resolve(FindFactomBlocks());
let last = 0;
if (!blockList.length) {
let allBlocks = await Promise.resolve(FindBlocks());
if (allBlocks.length) {
last = allBlocks[0].height
}
} else {
last = blockList[0].height
for (let i = 1; i < blockList.length; i++) {
if (last - 1 !== blockList[i].height) { break; }
last = blockList[i].height;
}
}
let BTC_Balance = await Promise.resolve(FindFactomsBitcoinBalance());
let lastOff = await Promise.resolve(FindLastNotificationSetOff());
let pendingNoti = await Promise.resolve(FindLastPendingNoti());
return app.render(req, res, '/', {
name: "BTC",
data: blockList,
lastConf: last - 1,
balance: BTC_Balance,
lastOff: lastOff.length ? lastOff[0].notificationtime : "30 Minutes",
pendingNoti: pendingNoti.length ? pendingNoti[0].notificationtime : "1 Block"
})
})
server.get('/BTC', async (req, res) => {
let blockList = await Promise.resolve(FindFactomBlocks());
let last = 0;
if (!blockList.length) {
let allBlocks = await Promise.resolve(FindBlocks());
last = allBlocks[0].height
} else {
last = blockList[0].height
for (let i = 1; i < blockList.length; i++) {
if (last - 1 !== blockList[i].height) { break; }
last = blockList[i].height;
}
}
let BTC_Balance = await Promise.resolve(FindFactomsBitcoinBalance());
let lastOff = await Promise.resolve(FindLastNotificationSetOff());
let pendingNoti = await Promise.resolve(FindLastPendingNoti());
return app.render(req, res, '/', {
name: "BTC",
data: blockList,
lastConf: last - 1,
balance: BTC_Balance,
lastOff: lastOff.length ? lastOff[0].notificationtime : "30 Minutes",
pendingNoti: pendingNoti.length ? pendingNoti[0].notificationtime : "1 Block"
})
})
server.get('/ETH', (req, res) => {
return app.render(req, res, '/', { name: "ETH", data: "Coming Soon..." })
})
server.post('/offnotificationchange', (req, res) => {
let SaveData = new NotificationsOff({
notificationtime: req.query.time,
time: new Date(),
})
SaveData.save().catch(err => err.code === 11000 ? null : console.log("offnotificationchange Save Error: ", err));
res.status(200).send("all good")
})
server.post('/pendingtimenotification', (req, res) => {
let SaveData = new PendingNotifications({
notificationtime: req.query.time,
time: new Date(),
})
SaveData.save().catch(err => err.code === 11000 ? null : console.log("offnotificationchange Save Error: ", err));
res.status(200).send("all good")
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})