-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
68 lines (61 loc) · 1.97 KB
/
api.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
const express = require('express');
const app = express();
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();
require('dotenv').config();
app.use(express.json());
// Get PORT from environment variable
const PORT = process.env.PORT;
const VOTE_URL = process.env.VOTE_URL;
const VOTES_URL = process.env.VOTES_URL;
// Middleware to set Access-Control-Allow-Origin header on all APIs
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
next();
});
// OPTIONS method to allow all requests
app.options('*', (req, res) => {
res.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.send();
});
// GET method for health check
app.get('/', (req, res) => {
res.send('OK');
});
// POST method for voting
app.post('/vote', async (req, res) => {
try {
const client = await auth.getIdTokenClient(VOTE_URL);
const response = await client.request({
url: VOTE_URL,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ vote: req.body.vote }),
});
return res.send(response.data);
} catch (error) {
console.error(error);
reponse = error.response;
return res.status(reponse.status).json({ message: `Couldn't reach backend: ${reponse.status} ${reponse.statusText}` });
}
});
// GET method for retrieving vote count
app.get('/votes', async (_, res) => {
try {
const client = await auth.getIdTokenClient(VOTES_URL);
const response = await client.request({ url: VOTES_URL });
return res.send(response.data);
} catch (error) {
console.error(error);
reponse = error.response;
return res.status(reponse.status).json({ message: `Couldn't reach backend: ${reponse.status} ${reponse.statusText}` });
}
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
console.log(`Vote URL is ${VOTE_URL}`);
console.log(`Votes URL is ${VOTES_URL}`);
});