-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
58 lines (44 loc) · 1.77 KB
/
index.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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';
import SlowDown from 'express-slow-down';
import PollController from './controllers/PollController';
import VoteController from './controllers/VoteController';
import POAP from "./poap";
const app = express();
app.enable("trust proxy");
app.disable('x-powered-by');
app.use(bodyParser.json());
app.unsubscribe(bodyParser.urlencoded({extended: false}));
app.use(cors());
app.use(helmet());
app.use(compression());
const speedLimiter = SlowDown({
windowMs: 15 * 60 * 1000, // 15 minutes
delayAfter: 500, // allow 500 requests per 15 minutes, then...
delayMs: 100, // begin adding 100ms of delay per request above 100:
});
app.use(speedLimiter);
const port = process.env.POAP_VOTE_PORT || 3000;
app.get('/api/polls', PollController.fetchPolls);
app.get('/api/paginated-polls', PollController.fetchPaginatedPolls);
app.post('/api/polls', PollController.createPoll);
app.get('/api/polls/:id', PollController.fetchPoll);
app.get('/api/poll/:poll_id/votes', VoteController.fetchVotes);
const poapApiKey = process.env.POAP_API_KEY;
const poapService = new POAP(poapApiKey);
const voteController = new VoteController(poapService);
const voteHandler = async (request, response) => {
return await voteController.createVote(request, response);
};
app.post('/api/poll/:poll_id/votes', voteHandler);
const voteDelegatedHandler = async (request, response) => {
return await voteController.createVoteDelegated(request, response);
};
app.post('/api/polls/:poll_id/votes-delegated', voteDelegatedHandler);
app.listen(port, () => {
console.log('Server is running on PORT ', port);
});
export default app;