forked from bufferapp/buzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (71 loc) · 2.34 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
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
const http = require('http')
const express = require('express')
const socketio = require('socket.io')
const app = express();
const server = http.Server(app);
const io = socketio(server);
const title = 'Buffer Buzzer'
let data = {
users: new Set(),
buzzes: new Set(),
}
const getData = () => ({
users: [...data.users],
buzzes: [...data.buzzes].map(b => {
const [ name, team ] = b.split('-')
return { name, team }
})
})
app.use(express.static('public'))
app.set('view engine', 'pug')
// authentication middleware
app.use((req, res, next) => {
const secureUrls = [
'/host',
]
if (process.env.BASIC_AUTH_USER && process.env.BASIC_AUTH_PWD && secureUrls.indexOf(req.url) > -1) {
const auth = {login: process.env.BASIC_AUTH_USER, password: process.env.BASIC_AUTH_PWD}
// parse login and password from headers
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')
// Verify login and password are set and correct
if (login && password && login === auth.login && password === auth.password) {
// Access granted...
return next()
} else {
// Access denied...
res.set('WWW-Authenticate', 'Basic realm="401"')
res.status(401).send('Authentication required.')
}
} else {
return next()
}
})
app.get('/', (req, res) => res.render('index', Object.assign({ title }, getData())))
app.get('/host', (req, res) => res.render('host', Object.assign({ title }, getData())))
const join = (user) => {
data.users.add(user.id)
io.emit('active', [...data.users].length)
console.log(`${user.name} joined!`)
}
const buzz = (user) => {
data.buzzes.add(`${user.name}-${user.team}`)
io.emit('buzzes', [...data.buzzes])
console.log(`${user.name} buzzed in!`)
if (!data.clearTimeout && process.env.CLEAR_BUZZES_TIMEOUT) {
data.clearTimeout = setTimeout(clear, parseInt(process.env.CLEAR_BUZZES_TIMEOUT))
}
}
const clear = () => {
data.buzzes = new Set()
io.emit('buzzes', [...data.buzzes])
clearTimeout(data.clearTimeout)
delete(data.clearTimeout)
console.log(`Clear buzzes`)
}
io.on('connection', (socket) => {
socket.on('join', join)
socket.on('buzz', buzz)
socket.on('clear', clear)
})
server.listen(process.env.PORT || 8090, () => console.log('Listening on http://localhost:8090/'))