-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (73 loc) · 1.89 KB
/
index.js
File metadata and controls
84 lines (73 loc) · 1.89 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
var express = require('express')
var currentApp = express()
var path = require("path");
var bodyParser = require('body-parser');
import { registerAccount } from "./register";
import { GetLeaderboard } from "./leaderboard";
import { MarketsAPI } from "./markets_api";
currentApp.use(bodyParser.json());
currentApp.use("/public", express.static(path.join(__dirname, './public')))
let currentLeaderboard = null;
let lockFetch = false;
currentApp.get('/api/leaderboard', function (req, res) {
function fetchLeaderboard(cb) {
// previously locked, show the existing data
if (lockFetch) {
cb(currentLeaderboard)
return
}
lockFetch = true
// locking new call, show new data
if (currentLeaderboard) {
cb(currentLeaderboard)
}
// console.log("Fetching leaderboard.")
GetLeaderboard().then((result) => {
result.timestamp = new Date()
// if leaderboard was there already, don't send it
if (currentLeaderboard == null) {
cb(result)
}
currentLeaderboard = result
lockFetch = false
})
.catch(ex => {
console.log("error: ", ex);
})
}
if (currentLeaderboard == null) {
fetchLeaderboard((data) => {
res.json(data)
})
setInterval(() => {
fetchLeaderboard((data) => {})
}, 15*60*1000)
} else {
res.json(currentLeaderboard)
}
})
currentApp.post('/api/register', function (req, res) {
const data = req.body;
registerAccount(data.user_name, data.public_key)
.then((e) =>{
res.send(e);
})
.catch(e => {
console.log("exception",e.message);
res.status(500).send({message: e.message});
})
})
currentApp.get('/api', function (req, res) {
MarketsAPI(req.query).then((e) => {
res.json(e)
})
.catch(e => {
res.status(500).send({message: e.message});
})
})
currentApp.get('/*', function (req, res) {
const fileName = path.join(__dirname + '/public/index.html')
res.sendFile(fileName);
})
console.log("Listening to 3000")
currentApp.listen(3000);