mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-17 00:40:56 -05:00
Use host header to let any server serve any region
This commit is contained in:
parent
69b999866e
commit
e635396b8a
2 changed files with 12 additions and 9 deletions
|
@ -2,10 +2,8 @@ config = {}
|
|||
|
||||
config.unittest = global.testing
|
||||
|
||||
config.tokyo = process.env.TOKYO or false
|
||||
config.saoPaulo = process.env.SAOPAULO or false
|
||||
config.chinaDomain = "http://cn.codecombat.com"
|
||||
config.brazilDomain = "http://br.codecombat.com"
|
||||
config.chinaDomain = "cn.codecombat.com"
|
||||
config.brazilDomain = "br.codecombat.com"
|
||||
config.port = process.env.COCO_PORT or process.env.COCO_NODE_PORT or process.env.PORT or 3000
|
||||
config.ssl_port = process.env.COCO_SSL_PORT or process.env.COCO_SSL_NODE_PORT or 3443
|
||||
config.cloudflare =
|
||||
|
|
|
@ -105,10 +105,15 @@ setupPassportMiddleware = (app) ->
|
|||
else
|
||||
app.use(authentication.session())
|
||||
|
||||
setupCountryRedirectMiddleware = (app, country="china", countryCode="CN", languageCode="zh", serverID="tokyo") ->
|
||||
setupCountryRedirectMiddleware = (app, country="china", countryCode="CN", languageCode="zh", host="cn.codecombat.com") ->
|
||||
shouldRedirectToCountryServer = (req) ->
|
||||
speaksLanguage = _.any req.acceptedLanguages, (language) -> language.indexOf languageCode isnt -1
|
||||
unless config[serverID]
|
||||
|
||||
#Work around express 3.0
|
||||
reqHost = req.hostname
|
||||
reqHost ?= req.host
|
||||
|
||||
unless reqHost.toLowerCase() is host
|
||||
ip = req.headers['x-forwarded-for'] or req.connection.remoteAddress
|
||||
ip = ip?.split(/,? /)[0] # If there are two IP addresses, say because of CloudFlare, we just take the first.
|
||||
geo = geoip.lookup(ip)
|
||||
|
@ -122,7 +127,7 @@ setupCountryRedirectMiddleware = (app, country="china", countryCode="CN", langua
|
|||
|
||||
app.use (req, res, next) ->
|
||||
if shouldRedirectToCountryServer req
|
||||
res.writeHead 302, "Location": config[country + 'Domain'] + req.url
|
||||
res.writeHead 302, "Location": 'http://' + host + req.url
|
||||
res.end()
|
||||
else
|
||||
next()
|
||||
|
@ -159,8 +164,8 @@ setupPerfMonMiddleware = (app) ->
|
|||
|
||||
exports.setupMiddleware = (app) ->
|
||||
setupPerfMonMiddleware app
|
||||
setupCountryRedirectMiddleware app, "china", "CN", "zh", "tokyo"
|
||||
setupCountryRedirectMiddleware app, "brazil", "BR", "pt-BR", "saoPaulo"
|
||||
setupCountryRedirectMiddleware app, "china", "CN", "zh", config.chinaDomain
|
||||
setupCountryRedirectMiddleware app, "brazil", "BR", "pt-BR", config.brazilDomain
|
||||
setupMiddlewareToSendOldBrowserWarningWhenPlayersViewLevelDirectly app
|
||||
setupExpressMiddleware app
|
||||
setupPassportMiddleware app
|
||||
|
|
Loading…
Reference in a new issue