This commit is contained in:
Scott Erickson 2014-01-28 12:27:21 -08:00
commit e89dc9103f
3 changed files with 153 additions and 118 deletions

View file

@ -1,3 +1,3 @@
require('coffee-script');
var app = require('./server');
app.startServer();
var server = require('./server');
server.startServer();

View file

@ -1,123 +1,38 @@
# Put lodash and underscore.string into the global namespace
GLOBAL._ = require 'lodash'
_.str = require 'underscore.string'
_.mixin _.str.exports()
do (setupLodash = this) ->
GLOBAL._ = require 'lodash'
_.str = require 'underscore.string'
_.mixin _.str.exports()
express = require 'express'
path = require 'path'
winston = require 'winston'
passport = require 'passport'
useragent = require 'express-useragent'
auth = require './server/routes/auth'
db = require './server/routes/db'
file = require './server/routes/file'
folder = require './server/routes/folder'
user = require './server/users/user_handler'
logging = require './server/commons/logging'
sprites = require './server/routes/sprites'
contact = require './server/routes/contact'
languages = require './server/routes/languages'
mail = require './server/routes/mail'
https = require 'https'
http = require 'http'
fs = require 'graceful-fs'
config = require './server_config'
logging.setup()
db.connectDatabase()
# MailChimp setup
mcapi = require 'mailchimp-api'
mc = new mcapi.Mailchimp(config.mail.mailchimpAPIKey)
GLOBAL.mc = mc
# Express server setup
app = express()
active_responses = []
oldBrowser = (req, res, next) ->
return next() if req.query['try-old-browser-anyway'] or not isOldBrowser(req)
res.sendfile(path.join(__dirname, 'public', 'index_old_browser.html'))
# determines order of middleware and request handling
app.configure(->
app.use (req, res, next) ->
req.setTimeout 15000, ->
console.log 'timed out!'
req.abort()
self.emit('pass',message)
next()
app.use(express.logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(useragent.express())
app.use '/play/', oldBrowser # When they go directly to play a level, they won't see our browser warning, so give it to 'em.
app.set('port', config.port)
app.set('views', __dirname + '/app/views')
app.set('view engine', 'jade')
app.set('view options', { layout: false })
app.use(express.favicon())
app.use(express.cookieParser(config.cookie_secret))
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.cookieSession({secret:'defenestrate'}))
app.use(passport.initialize())
app.use(passport.session())
if(config.slow_down)
app.use((req, res, next) -> setTimeout((-> next()), 1000))
user.setupMiddleware(app)
app.use(app.router)
)
app.configure('development', -> app.use(express.errorHandler()))
auth.setupRoutes(app)
db.setupRoutes(app)
sprites.setupRoutes(app)
contact.setupRoutes(app)
file.setupRoutes(app)
folder.setupRoutes(app)
languages.setupRoutes(app)
mail.setupRoutes(app)
# Some sort of cross-domain communication hack facebook requires
app.get('/channel.html', (req, res) ->
res.sendfile(path.join(__dirname, 'public', 'channel.html'))
)
# blitz.io (load tester) auth
app.get '/mu-a2a0832f-10763ae9-170d6c87-70a62423', (req, res) ->
res.send('42')
# Anything that isn't handled at this point gets index.html
app.get('*', (req, res) ->
res.sendfile(path.join(__dirname, 'public', 'index.html'))
)
#ssl_options =
# key: fs.readFileSync('ssl/key.pem')
# cert: fs.readFileSync('ssl/cert.pem')
winston = require 'winston'
serverSetup = require './server_setup'
module.exports.startServer = ->
app = createAndConfigureApp()
http.createServer(app).listen(app.get('port'))
winston.info("Express SSL server listening on port " + app.get('port'))
# https.createServer(ssl_options, app).listen(config['ssl_port']);
return app
isOldBrowser = (req) ->
# https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js
return false unless ua = req.useragent
return true if ua.isiPad or ua.isiPod or ua.isiPhone or ua.isOpera
return false unless ua and ua.Browser in ["Chrome", "Safari", "Firefox", "IE"] and ua.Version
b = ua.Browser
v = parseInt ua.Version.split('.')[0], 10
return true if b is 'Chrome' and v < 17
return true if b is 'Safari' and v < 6
return true if b is 'Firefox' and v < 21
return true if b is 'IE' and v < 10
false
createAndConfigureApp = ->
serverSetup.setupLogging()
serverSetup.connectToDatabase()
serverSetup.setupMailchimp()
app = express()
serverSetup.setExpressConfigurationOptions app
serverSetup.setupMiddleware app
serverSetup.setupRoutes app
app

120
server_setup.coffee Normal file
View file

@ -0,0 +1,120 @@
express = require 'express'
path = require 'path'
passport = require 'passport'
useragent = require 'express-useragent'
fs = require 'graceful-fs'
auth = require './server/routes/auth'
db = require './server/routes/db'
file = require './server/routes/file'
folder = require './server/routes/folder'
user = require './server/users/user_handler'
logging = require './server/commons/logging'
sprites = require './server/routes/sprites'
contact = require './server/routes/contact'
languages = require './server/routes/languages'
mail = require './server/routes/mail'
config = require './server_config'
###Middleware setup functions implementation###
setupRequestTimeoutMiddleware = (app) ->
app.use (req, res, next) ->
req.setTimeout 15000, ->
console.log 'timed out!'
req.abort()
self.emit('pass',message)
next()
setupExpressMiddleware = (app) ->
setupRequestTimeoutMiddleware app
app.use(express.logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(useragent.express())
app.use(express.favicon())
app.use(express.cookieParser(config.cookie_secret))
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.cookieSession({secret:'defenestrate'}))
setupPassportMiddleware = (app) ->
app.use(passport.initialize())
app.use(passport.session())
setupOneSecondDelayMiddlware = (app) ->
if(config.slow_down)
app.use((req, res, next) -> setTimeout((-> next()), 1000))
setupUserMiddleware = (app) ->
user.setupMiddleware(app)
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly = (app) ->
isOldBrowser = (req) ->
# https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js
return false unless ua = req.useragent
return true if ua.isiPad or ua.isiPod or ua.isiPhone or ua.isOpera
return false unless ua and ua.Browser in ["Chrome", "Safari", "Firefox", "IE"] and ua.Version
b = ua.Browser
v = parseInt ua.Version.split('.')[0], 10
return true if b is 'Chrome' and v < 17
return true if b is 'Safari' and v < 6
return true if b is 'Firefox' and v < 21
return true if b is 'IE' and v < 10
false
app.use '/play/', (req, res, next) ->
return next() if req.query['try-old-browser-anyway'] or not isOldBrowser req
res.sendfile(path.join(__dirname, 'public', 'index_old_browser.html'))
exports.setupMiddleware = (app) ->
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly app
setupExpressMiddleware app
setupPassportMiddleware app
setupOneSecondDelayMiddlware app
setupUserMiddleware app
###Routing function implementations###
setupFallbackRouteToIndex = (app) ->
app.get '*', (req, res) ->
res.sendfile path.join(__dirname, 'public', 'index.html')
setupFacebookCrossDomainCommunicationRoute = (app) ->
app.get '/channel.html', (req, res) ->
res.sendfile path.join(__dirname, 'public', 'channel.html')
exports.setupRoutes = (app) ->
app.use app.router
auth.setupRoutes app
db.setupRoutes app
sprites.setupRoutes app
contact.setupRoutes app
file.setupRoutes app
folder.setupRoutes app
languages.setupRoutes app
mail.setupRoutes app
setupFacebookCrossDomainCommunicationRoute app
setupFallbackRouteToIndex app
###Miscellaneous configuration functions###
exports.setupLogging = ->
logging.setup()
exports.connectToDatabase = ->
db.connectDatabase()
exports.setupMailchimp = ->
mcapi = require 'mailchimp-api'
mc = new mcapi.Mailchimp(config.mail.mailchimpAPIKey)
GLOBAL.mc = mc
exports.setExpressConfigurationOptions = (app) ->
app.set('port', config.port)
app.set('views', __dirname + '/app/views')
app.set('view engine', 'jade')
app.set('view options', { layout: false })