-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.js
75 lines (56 loc) · 1.85 KB
/
web.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
const express = require('express');
const app = express();
const cors = require('cors');
const port = process.env.PORT;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors())
const { getGameOptions, insertNewScore, getGame } = require('./scoreboard');
function registerScoreEndpoints() {
// This is removed because it's a security problem
// it exists in case you want to build an index of your scoreboards though
// app.get('/games', (req, res) => {
// getGameOptions().then(games => {
// res.send(games);
// });
// });
app.get('/', (req, res) => {
res.send(`
<html>
<div style="text-align:center">
<h2>EasyLeaderboard is running!</h2>
<br>
<a href="http://easyleaderboard.com" target="_blank">Learn more here</a>
<br>
<br>
<br>
<label style="font-size:80%;"><a href="https://twitter.com/garrett_makes" target="_blank">Made by Garrett (@garrett_makes)</a></label>
</div>
</html>
`)
});
app.get('/games/:game', async (req, res) => {
var game = req.params.game;
var asc = req.query.asc || "false";
var limit = req.query.pagesize || req.query.limit || 10;
limit = Math.min(100, limit); //only allow a max of 100 at a time
var page = req.query.page || 1;
res.send(await getGame(game, limit, asc == "true", page));
});
app.post('/games/submit', async (req, res) => {
if (req.body.name.length > 100) {
req.body.name = req.body.name.substr(0, 100);
}
res.send(await insertNewScore(req.body.game, req.body.name, req.body.score, req.body.metaData, req.body.validation));
});
}
function startWebServer() {
registerScoreEndpoints();
app.listen(port, () => {
console.log(`Easy Leaderboard is running on port ${port}`);
});
return app;
}
module.exports = {
startWebServer
}