2012-04-27 02:19:31 +00:00
|
|
|
http = require 'http'
|
|
|
|
|
path = require 'path'
|
|
|
|
|
express = require 'express'
|
|
|
|
|
gzippo = require 'gzippo'
|
|
|
|
|
derby = require 'derby'
|
|
|
|
|
app = require '../app'
|
|
|
|
|
serverError = require './serverError'
|
2012-08-27 14:23:33 +00:00
|
|
|
MongoStore = require('connect-mongo')(express)
|
2012-11-15 05:47:58 +00:00
|
|
|
auth = require 'derby-auth'
|
|
|
|
|
priv = require './private'
|
2012-07-10 18:56:43 +00:00
|
|
|
|
2012-07-17 14:20:41 +00:00
|
|
|
## RACER CONFIGURATION ##
|
2012-04-27 02:19:31 +00:00
|
|
|
|
2012-12-31 18:39:36 +00:00
|
|
|
racer = require 'racer'
|
2012-08-24 18:50:54 +00:00
|
|
|
racer.io.set('transports', ['xhr-polling'])
|
2012-08-06 14:41:58 +00:00
|
|
|
unless process.env.NODE_ENV == 'production'
|
|
|
|
|
racer.use(racer.logPlugin)
|
|
|
|
|
derby.use(derby.logPlugin)
|
2012-04-27 02:19:31 +00:00
|
|
|
|
|
|
|
|
## SERVER CONFIGURATION ##
|
|
|
|
|
|
2012-07-17 14:20:41 +00:00
|
|
|
expressApp = express()
|
|
|
|
|
server = http.createServer expressApp
|
|
|
|
|
module.exports = server
|
|
|
|
|
|
|
|
|
|
derby.use(require 'racer-db-mongo')
|
|
|
|
|
store = derby.createStore
|
2012-12-05 00:20:46 +00:00
|
|
|
db: {type: 'Mongo', uri: process.env.NODE_DB_URI, safe:true}
|
2012-07-17 14:20:41 +00:00
|
|
|
listen: server
|
|
|
|
|
|
2012-04-27 02:19:31 +00:00
|
|
|
ONE_YEAR = 1000 * 60 * 60 * 24 * 365
|
|
|
|
|
root = path.dirname path.dirname __dirname
|
|
|
|
|
publicPath = path.join root, 'public'
|
|
|
|
|
|
2012-08-09 15:22:29 +00:00
|
|
|
habitrpgMiddleware = (req, res, next) ->
|
2012-08-02 23:07:23 +00:00
|
|
|
model = req.getModel()
|
2012-09-07 21:21:09 +00:00
|
|
|
## Set _mobileDevice to true or false so view can exclude portions from mobile device
|
|
|
|
|
model.set '_mobileDevice', /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(req.header 'User-Agent')
|
2012-11-14 23:08:45 +00:00
|
|
|
model.set '_nodeEnv', process.env.NODE_ENV
|
2012-08-02 23:07:23 +00:00
|
|
|
next()
|
2012-11-15 05:47:58 +00:00
|
|
|
|
|
|
|
|
# Authentication setup
|
|
|
|
|
strategies =
|
|
|
|
|
facebook:
|
|
|
|
|
strategy: require("passport-facebook").Strategy
|
|
|
|
|
conf:
|
|
|
|
|
clientID: process.env.FACEBOOK_KEY
|
|
|
|
|
clientSecret: process.env.FACEBOOK_SECRET
|
|
|
|
|
options =
|
2013-01-02 05:25:58 +00:00
|
|
|
domain: process.env.NODE_BASE || 'http://localhost:3000'
|
2012-11-15 05:47:58 +00:00
|
|
|
allowPurl: true
|
|
|
|
|
schema: require('../app/schema').newUserObject()
|
2012-11-16 16:00:24 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
mongo_store = new MongoStore {url: process.env.NODE_DB_URI}, ->
|
|
|
|
|
expressApp
|
|
|
|
|
.use(express.favicon())
|
|
|
|
|
# Gzip static files and serve from memory
|
|
|
|
|
.use(gzippo.staticGzip publicPath, maxAge: ONE_YEAR)
|
|
|
|
|
# Gzip dynamically rendered content
|
|
|
|
|
.use(express.compress())
|
2012-04-27 02:19:31 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
# Uncomment to add form data parsing support
|
|
|
|
|
.use(express.bodyParser())
|
|
|
|
|
.use(express.methodOverride())
|
2012-04-27 02:19:31 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
# Uncomment and supply secret to add Derby session handling
|
|
|
|
|
# Derby session middleware creates req.session and socket.io sessions
|
|
|
|
|
.use(express.cookieParser())
|
|
|
|
|
.use(store.sessionMiddleware
|
|
|
|
|
secret: process.env.SESSION_SECRET || 'YOUR SECRET HERE'
|
|
|
|
|
cookie: {maxAge: ONE_YEAR}
|
|
|
|
|
store: mongo_store
|
|
|
|
|
)
|
2012-04-27 02:19:31 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
# Adds req.getModel method
|
|
|
|
|
.use(store.modelMiddleware())
|
|
|
|
|
# Middelware can be inserted after the modelMiddleware and before
|
|
|
|
|
# the app router to pass server accessible data to a model
|
|
|
|
|
.use(priv.middleware)
|
|
|
|
|
.use(habitrpgMiddleware)
|
|
|
|
|
.use(auth(store, strategies, options))
|
|
|
|
|
# Creates an express middleware from the app's routes
|
|
|
|
|
.use(app.router())
|
|
|
|
|
.use(expressApp.router)
|
|
|
|
|
.use(serverError root)
|
2012-11-14 22:34:18 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
priv.routes(expressApp)
|
|
|
|
|
require('./serverRoutes')(expressApp, root, derby)
|
2012-12-28 15:06:15 +00:00
|
|
|
|
2013-01-02 00:27:52 +00:00
|
|
|
# Errors
|
|
|
|
|
expressApp.all '*', (req) ->
|
|
|
|
|
throw "404: #{req.url}"
|