mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-30 10:56:53 -05:00
Refactored server.coffee
This commit is contained in:
parent
36a46a07f2
commit
7534f47be4
3 changed files with 153 additions and 118 deletions
4
index.js
4
index.js
|
@ -1,3 +1,3 @@
|
||||||
require('coffee-script');
|
require('coffee-script');
|
||||||
var app = require('./server');
|
var server = require('./server');
|
||||||
app.startServer();
|
server.startServer();
|
||||||
|
|
143
server.coffee
143
server.coffee
|
@ -1,123 +1,38 @@
|
||||||
# Put lodash and underscore.string into the global namespace
|
do (setupLodash = this) ->
|
||||||
GLOBAL._ = require 'lodash'
|
GLOBAL._ = require 'lodash'
|
||||||
_.str = require 'underscore.string'
|
_.str = require 'underscore.string'
|
||||||
_.mixin _.str.exports()
|
_.mixin _.str.exports()
|
||||||
|
|
||||||
express = require 'express'
|
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'
|
http = require 'http'
|
||||||
fs = require 'graceful-fs'
|
winston = require 'winston'
|
||||||
|
serverSetup = require './server_setup'
|
||||||
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')
|
|
||||||
|
|
||||||
module.exports.startServer = ->
|
module.exports.startServer = ->
|
||||||
|
app = createAndConfigureApp()
|
||||||
http.createServer(app).listen(app.get('port'))
|
http.createServer(app).listen(app.get('port'))
|
||||||
winston.info("Express SSL server listening on port " + 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
|
return app
|
||||||
|
|
||||||
isOldBrowser = (req) ->
|
createAndConfigureApp = ->
|
||||||
# https://github.com/biggora/express-useragent/blob/master/lib/express-useragent.js
|
serverSetup.setupLogging()
|
||||||
return false unless ua = req.useragent
|
serverSetup.connectToDatabase()
|
||||||
return true if ua.isiPad or ua.isiPod or ua.isiPhone or ua.isOpera
|
serverSetup.setupMailchimp()
|
||||||
return false unless ua and ua.Browser in ["Chrome", "Safari", "Firefox", "IE"] and ua.Version
|
|
||||||
b = ua.Browser
|
app = express()
|
||||||
v = parseInt ua.Version.split('.')[0], 10
|
serverSetup.setExpressConfigurationOptions app
|
||||||
return true if b is 'Chrome' and v < 17
|
serverSetup.setupMiddleware app
|
||||||
return true if b is 'Safari' and v < 6
|
serverSetup.setupRoutes app
|
||||||
return true if b is 'Firefox' and v < 21
|
app
|
||||||
return true if b is 'IE' and v < 10
|
|
||||||
false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
120
server_setup.coffee
Normal file
120
server_setup.coffee
Normal 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 })
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue