-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (42 loc) · 1.76 KB
/
server.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
const PORT = 8080;
const BASE_URL = '/api';
const redis = require('redis');
const express = require('express');
const http = require('http');
const morgan = require('morgan');
const cors = require('cors');
const { uuid } = require('uuidv4');
const bodyParser = require('body-parser');
const IOServer = require('socket.io');
const app = express();
const { MemoryDatabase } = require('./memory-database');
const { RoomsController } = require('./rooms-controller');
app.use(cors('*'));
app.use(bodyParser.json({}));
app.use(morgan('dev'));
const roomRouter = express.Router();
const memory = new MemoryDatabase({ redisClient: redis.createClient() });
const server = http.createServer(app);
const io = new IOServer(server);
const roomsController = new RoomsController({ memory, io, uuid });
roomRouter.route('/').post(roomsController.create);
roomRouter.route('/:id').get(roomsController.get);
roomRouter.route('/:id/config').post(roomsController.setConfig);
roomRouter.route('/:id/cards').post(roomsController.addCards);
roomRouter.route('/:roomId/cards/:cardId').delete(roomsController.deleteCard);
roomRouter.route('/join/:roomCode').post(roomsController.join);
roomRouter
.route('/:roomId/cards/:cardId/stage')
.post(roomsController.stageCardToVote);
roomRouter
.route('/:roomId/cards/:cardId/unstage')
.post(roomsController.unstageCardFromVoting);
roomRouter
.route('/:roomId/cards/:cardId/vote')
.put(roomsController.updateVote)
.post(roomsController.vote);
roomRouter.route('/:id/quit').post(roomsController.quit);
app.use(`${BASE_URL}/v1/ping`, (_, res) => res.send('pong'));
app.use(`${BASE_URL}/v1/rooms`, roomRouter);
app.use('*', (_, res) => res.status(404).json({ msg: 'Resource not found' }));
server.listen(PORT, () => console.log(`> 🚀 Server running at ${PORT}`));