This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (94 loc) · 3.22 KB
/
server.js
File metadata and controls
104 lines (94 loc) · 3.22 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express');
const next = require('next');
const bodyParser = require('body-parser');
const { updateScore, fetchScore } = require('./src/score/score');
const createImageFromApi = require('./src/create-png/createImageFromApi');
const createImageFromFirebase = require('./src/create-png/createImageFromFirebase');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
const port = process.env.PORT || 3000;
server.get('/test', (req, res) => res.json({ message: 'testing' }));
server.post('/api/update/:matchId', bodyParser.json(), (req, res) => {
const matchId = req.params.matchId;
if (matchId.length < 3) {
return res
.status(400)
.json({ message: 'Match ID must be string with at least three characters' });
}
return updateScore(matchId, {
pointsA: req.body.pointsA,
pointsB: req.body.pointsB,
setA: req.body.setA,
setB: req.body.setB,
nameA: req.body.nameA,
nameB: req.body.nameB,
logoA: req.body.logoA,
logoB: req.body.logoB,
colorA: req.body.colorA,
colorB: req.body.colorB,
showLogos: req.body.showLogos,
showColors: req.body.showColors,
isShowing: req.body.isShowing,
}).then(() => res.json({ message: 'Data received.' }));
});
server.get('/api/scores/:matchId', (req, res) => {
const matchId = req.params.matchId;
if (matchId.length < 3) {
return res
.status(400)
.json({ message: 'Match ID must be string with at least three characters' });
}
return fetchScore(matchId).then(data =>
res.json({
pointsA: data.pointsA,
pointsB: data.pointsB,
setA: data.setA,
setB: data.setB,
nameA: data.nameA,
nameB: data.nameB,
logoA: data.logoA,
logoB: data.logoB,
colorA: data.colorA,
colorB: data.colorB,
showLogos: data.showLogos,
showColors: data.showColors,
isShowing: data.isShowing,
}),
);
});
server.get('/scoreboard/png', async (req, res, nextFunction) => {
try {
const { matchId } = req.query;
if(!matchId) {
res.status(404).send("No query param matchId provided, was not able to find your match");
return;
}
await createImageFromApi(matchId);
req.url = `/static/score/${matchId}.png`;
nextFunction();
} catch (err) {
console.error(err);
res.status(503).json({ error: JSON.stringify(err.message || err, null, 2) });
}
});
server.get('/firebase/png', async (req, res, nextFunction) => {
try {
const { tournamentId, matchId } = req.query;
await createImageFromFirebase({ tournamentId, matchId });
req.url = `/static/score/firebase/${tournamentId}-${matchId}.png`;
nextFunction();
} catch (err) {
console.error(err);
res.status(503).json({ error: JSON.stringify(err.message || err, null, 2) });
}
});
server.use('/static/score', express.static('static/score'));
server.get('*', (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on port ${port}`);
});
});