forked from BinaryMuse/convocate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.coffee
92 lines (81 loc) · 2.74 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
http = require 'http'
sio = require 'socket.io'
redis = require 'redis'
cookie = require 'cookie'
connect = require 'connect'
express = require 'express'
webpack = require 'webpack'
webpackDev = require 'webpack-dev-middleware'
MemoryStore = express.session.MemoryStore
redisConfig = require './config/redis'
Chatroom = require './lib/chatroom'
process.env.NODE_ENV ?= 'development'
app = express()
server = http.createServer(app)
io = sio.listen(server)
sessionStore = new MemoryStore()
# redisClient = redis.createClient(redisConfig.port, redisConfig.host)
# redisClient.select(redisConfig.database) if redisConfig.database?
# redisClient.on 'error', (err) ->
# console.error "Error in redisClient:"
# console.error err
redisClient = null
chatroom = new Chatroom(redisClient, io)
app.configure ->
app.set 'port', process.env.PORT || '3000'
app.set 'views', "#{__dirname}/views"
app.set 'view engine', 'jade'
app.use express.favicon()
app.use express.logger()
app.use express.bodyParser()
app.use express.methodOverride()
app.use express.cookieParser()
app.use express.session(secret: 'convocateisawesome', key: 'convocate.sid', store: sessionStore)
app.use express.static("#{__dirname}/public")
app.configure 'development', ->
app.use webpackDev "#{__dirname}/assets/index.js",
webpack:
watch: true
publicPrefix: 'http://localhost:3000/assets/'
output: 'bundle.js'
app.use express.errorHandler()
app.get '/', (req, res) ->
if req.session.authenticated
res.render 'index'
else
res.redirect '/login'
app.get '/login', (req, res) ->
if req.session.authenticated
res.redirect '/'
else
res.render 'login'
app.post '/login', (req, res) ->
if req.body.username && req.body.username not in chatroom.members
req.session.authenticated = true
req.session.username = req.body.username
res.redirect '/'
else
res.redirect '/login'
app.get '/logout', (req, res) ->
req.session.destroy ->
res.redirect '/'
server.listen app.get('port'), ->
console.log "Server running on port #{app.get('port')}"
io.set 'authorization', (handshakeData, accept) ->
try
if handshakeData.headers.cookie
handshakeCookie = cookie.parse(handshakeData.headers.cookie)
sessionId = connect.utils.parseSignedCookie(handshakeCookie['convocate.sid'], 'convocateisawesome')
sessionStore.get sessionId, (err, session) ->
if err? || !session
accept 'Could not get session from session ID', false
else
if session.authenticated
handshakeData.session = session
accept null, true
else
accept 'Not authenticated', false
else
accept 'No cookie transmitted', false
catch e
accept 'Uncaught exception while authorizing', false