-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.coffee
132 lines (100 loc) · 3.33 KB
/
app.coffee
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Bootstrapping
onHeroku = process.env.PORT
if onHeroku
dbString = process.env.MONGODB_URI
port = process.env.PORT
else
dbString = "mongodb://localhost:27017/crb_register"
port = 3000
express = require('express')
stylus = require('stylus')
mongo = require('mongoskin')
coffee = require('coffee-script')
db = mongo.db(dbString)
coll = db.collection('entries')
routes = require('./routes')
app = module.exports = express.createServer()
io = require('socket.io').listen(app)
coffeeDir = __dirname + '/coffee'
publicDir = __dirname + '/public'
cssDir = publicDir + '/stylesheets'
# jsDir = publicDir + '/javascripts'
password = "crb!"
escape = require('escape-html')
# Configuration
app.configure ->
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(app.router)
app.use(express.favicon(publicDir + '/favicon.ico', { maxAge: 2592000000 }))
app.use(express.compiler({src: coffeeDir, dest: publicDir, enable: ['coffeescript']}))
app.use(stylus.middleware({src: publicDir, compress: true}))
app.use express.static(publicDir)
if onHeroku
io.configure ->
io.set("transports", ["xhr-polling"])
io.set("polling duration", 10)
app.configure 'development', ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
app.configure 'production', ->
app.use(express.errorHandler())
# Routes
app.get '/', (req, res) ->
coll.find({old: false}).toArray (error, entries) ->
data = JSON.stringify(sanitizedEntries(entries))
res.render('index.jade', {length: password.length, entries: data})
app.post '/unlock', (req, res) ->
res.send(req.param('code') is password)
# Sanitization helpers
sanitizedEntries = (entries) ->
newEntries = []
for entry in entries
newEntries.push sanitizedEntry(entry)
newEntries
sanitizedEntry = (entry) ->
newEntry = {}
for key, value of entry
newEntry[key] = sanitizedValue(value)
newEntry
sanitizedValue = (value) ->
if typeof value is 'string' then escape(value) else value
# Database
insertEntry = (data, callback) ->
data.old = false
coll.insert data, ->
updateCount()
callback()
consumeEntries = (callback) ->
coll.update {old: false}, {$set: {old: true}}, {multi: true}, ->
updateCount()
callback()
setWinner = (data, callback) ->
clearWinner ->
coll.update {old: false, email: data.email}, {$set: {winner: true}}, ->
callback()
clearWinner = (callback) ->
coll.update {old: false}, {$set: {winner: false}}, {multi: true}, ->
callback()
updateCount = ->
coll.count {old: false}, (err, count) ->
io.sockets.emit('updatedEntryCount', count)
io.sockets.emit('updateProgressBar', (count / 30) * 100)
# Sockets
io.sockets.on 'connection', (socket) ->
updateCount()
socket.on 'submitEntry', (data) ->
insertEntry data, ->
io.sockets.emit('newEntryPosted', data)
socket.on 'consumeEntries', ->
consumeEntries ->
io.sockets.emit('clearEntries')
socket.on 'winnerChosen', (data) ->
setWinner data, ->
io.sockets.emit('showWinner', data)
socket.on 'winnerCleared', ->
clearWinner ->
io.sockets.emit('hideWinner')
app.listen(port)
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)