Check all languages instead of just first for country servers

This commit is contained in:
Nick Winter 2015-11-03 09:42:17 -08:00
parent c6bb5b5329
commit 6773f1e876

View file

@ -80,14 +80,13 @@ setupPassportMiddleware = (app) ->
setupCountryRedirectMiddleware = (app, country="china", countryCode="CN", languageCode="zh", serverID="tokyo") -> setupCountryRedirectMiddleware = (app, country="china", countryCode="CN", languageCode="zh", serverID="tokyo") ->
shouldRedirectToCountryServer = (req) -> shouldRedirectToCountryServer = (req) ->
firstLanguage = req.acceptedLanguages[0] speaksLanguage = _.any req.acceptedLanguages, (language) -> language.indexOf languageCode isnt -1
speaksLanguage = firstLanguage and firstLanguage.indexOf(languageCode) isnt -1
unless config[serverID] unless config[serverID]
ip = req.headers['x-forwarded-for'] or req.connection.remoteAddress 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. ip = ip?.split(/,? /)[0] # If there are two IP addresses, say because of CloudFlare, we just take the first.
geo = geoip.lookup(ip) geo = geoip.lookup(ip)
#if speaksLanguage or geo?.country is countryCode #if speaksLanguage or geo?.country is countryCode
# log.info("Should we redirect to #{serverID} server? speaksLanguage: #{speaksLanguage}, firstLanguage: #{firstLanguage}, ip: #{ip}, geo: #{geo} -- so redirecting? #{geo?.country is 'CN' and speaksLanguage}") # log.info("Should we redirect to #{serverID} server? speaksLanguage: #{speaksLanguage}, acceptedLanguages: #{req.acceptedLanguages}, ip: #{ip}, geo: #{geo} -- so redirecting? #{geo?.country is 'CN' and speaksLanguage}")
return geo?.country is countryCode and speaksLanguage return geo?.country is countryCode and speaksLanguage
else else
#log.info("We are on #{serverID} server. speaksLanguage: #{speaksLanguage}, acceptedLanguages: #{req.acceptedLanguages[0]}") #log.info("We are on #{serverID} server. speaksLanguage: #{speaksLanguage}, acceptedLanguages: #{req.acceptedLanguages[0]}")