2013-08-17 07:17:32 +08:00
|
|
|
# Module dependencies
|
|
|
|
express = require("express")
|
|
|
|
RedisStore = require("connect-redis")(express)
|
|
|
|
redis = require("redis")
|
2013-08-17 09:37:41 +08:00
|
|
|
|
|
|
|
config = require("./config")
|
2013-10-29 03:54:51 +08:00
|
|
|
Logger = require("./lib/logger")
|
2013-08-20 09:43:18 +08:00
|
|
|
MainRouter = require("./routes/main_router")
|
2013-09-16 07:10:19 +08:00
|
|
|
Modules = require("./lib/modules")
|
2013-08-20 10:51:26 +08:00
|
|
|
RedisAction = require("./lib/redis_action")
|
2013-10-31 04:04:43 +08:00
|
|
|
RedisPublisher = require("./lib/redis_publisher")
|
|
|
|
RedisWebsocketBridge = require("./lib/redis_websocket_bridge")
|
2013-08-30 22:29:00 +08:00
|
|
|
Utils = require("./lib/utils")
|
2013-08-17 07:17:32 +08:00
|
|
|
|
2013-09-16 07:10:19 +08:00
|
|
|
# Module to store the modules registered in the application
|
|
|
|
config.modules = modules = new Modules()
|
|
|
|
|
|
|
|
config.modules.register "RedisAction", new RedisAction()
|
2013-10-31 04:04:43 +08:00
|
|
|
config.modules.register "RedisPublisher", new RedisPublisher()
|
|
|
|
|
|
|
|
# @todo This is only as a module because this app still changes data on redis, but it shouldn't.
|
|
|
|
# When this is fixed, redisStore can probably become an internal variable in RedisAction.
|
2013-09-16 07:10:19 +08:00
|
|
|
config.modules.register "RedisStore", redis.createClient()
|
|
|
|
|
|
|
|
# The application, exported in this module
|
|
|
|
app = config.modules.register "App", express.createServer()
|
|
|
|
module.exports = app
|
2013-08-17 07:17:32 +08:00
|
|
|
|
2013-08-30 22:29:00 +08:00
|
|
|
# configure the application
|
2013-08-17 07:17:32 +08:00
|
|
|
app.configure ->
|
|
|
|
app.set "views", __dirname + "/views"
|
|
|
|
app.set "view engine", "jade"
|
|
|
|
app.use express["static"](__dirname + "/public")
|
2013-10-31 22:50:52 +08:00
|
|
|
app.use require('connect-assets')()
|
2013-08-17 07:17:32 +08:00
|
|
|
app.use express.bodyParser()
|
|
|
|
app.use express.methodOverride()
|
|
|
|
app.use express.cookieParser()
|
|
|
|
|
|
|
|
# redis
|
|
|
|
app.use express.session(
|
2013-08-30 22:29:00 +08:00
|
|
|
secret: config.app.sessionSecret
|
2013-08-17 07:17:32 +08:00
|
|
|
cookie:
|
|
|
|
secure: true
|
|
|
|
store: new RedisStore(
|
2013-08-30 22:29:00 +08:00
|
|
|
host: config.redis.host
|
|
|
|
port: config.redis.port
|
2013-08-17 07:17:32 +08:00
|
|
|
)
|
|
|
|
key: "express.sid"
|
|
|
|
)
|
|
|
|
app.use app.router
|
|
|
|
app.configure "development", ->
|
|
|
|
app.use express.errorHandler(
|
|
|
|
dumpExceptions: true
|
|
|
|
showStack: true
|
|
|
|
)
|
|
|
|
app.configure "production", ->
|
|
|
|
app.use express.errorHandler()
|
|
|
|
|
2013-08-30 22:29:00 +08:00
|
|
|
# view helpers
|
2013-08-17 07:17:32 +08:00
|
|
|
app.helpers
|
|
|
|
h_environment: app.settings.env
|
|
|
|
|
2013-08-20 09:43:18 +08:00
|
|
|
# Router
|
2013-09-16 07:10:19 +08:00
|
|
|
config.modules.register "MainRouter", new MainRouter()
|
2013-08-17 07:17:32 +08:00
|
|
|
|
2013-08-30 22:29:00 +08:00
|
|
|
# Socket.IO
|
2013-08-17 09:37:41 +08:00
|
|
|
io = require("socket.io").listen(app)
|
2013-08-17 07:17:32 +08:00
|
|
|
io.configure ->
|
2013-09-16 07:10:19 +08:00
|
|
|
|
2013-08-30 22:29:00 +08:00
|
|
|
# Authorize a session before it given access to connect to SocketIO
|
2013-08-17 07:17:32 +08:00
|
|
|
io.set "authorization", (handshakeData, callback) ->
|
2013-09-16 07:10:19 +08:00
|
|
|
redisAction = config.modules.get("RedisAction")
|
2013-08-30 22:29:00 +08:00
|
|
|
sessionID = Utils.getCookieVar(handshakeData.headers.cookie, "sessionid")
|
|
|
|
meetingID = Utils.getCookieVar(handshakeData.headers.cookie, "meetingid")
|
2013-10-30 04:53:00 +08:00
|
|
|
redisAction.isValidSession meetingID, sessionID, (err, isValid) ->
|
2013-08-17 07:17:32 +08:00
|
|
|
unless isValid
|
2013-10-29 03:54:51 +08:00
|
|
|
Logger.error "Invalid sessionID/meetingID"
|
2013-08-30 22:29:00 +08:00
|
|
|
callback(null, false) # failed authorization
|
2013-08-17 07:17:32 +08:00
|
|
|
else
|
2013-10-30 04:53:00 +08:00
|
|
|
redisAction.getUserProperties meetingID, sessionID, (err, properties) ->
|
2013-08-17 07:17:32 +08:00
|
|
|
handshakeData.sessionID = sessionID
|
|
|
|
handshakeData.username = properties.username
|
|
|
|
handshakeData.meetingID = properties.meetingID
|
2013-08-30 22:29:00 +08:00
|
|
|
callback(null, true) # good authorization
|
2013-08-17 07:17:32 +08:00
|
|
|
|
2013-10-31 04:04:43 +08:00
|
|
|
# Bridge used to interact between redis and socket clients
|
|
|
|
config.modules.register "RedisWebsocketBridge", new RedisWebsocketBridge(io)
|
2013-11-19 03:36:27 +08:00
|
|
|
|
|
|
|
# reduce logging
|
|
|
|
io.set('log level', 1);
|