Search code examples
google-app-enginepython-2.7wsgiwebapp2

Is there a way to handle requests with locale-dependant wsgi handlers?


Is there a way to handle requests from different geographic locations with a different WSGI handler? Specifically I want to allow all requests from one local (US) and redirect all others to a holding page i.e. something like

application_us = webapp2.WSGIApplication([
    ('/.*', MainHandler)
    ], debug=DEBUG)

application_elsewhere = webapp2.WSGIApplication([
    ('/.*', HoldingPageHandler)
    ], debug=DEBUG)

I'm aware of X-AppEngine-Country however I'm not quite sure how to put it to use in this case?


Solution

  • Okay, building the answer by Sebastian Kreft I figured it's probably easiest to throw this into a base handler of which every other handler is a subclass as follows.

    class BaseHandler(webapp2.RequestHandler):
        def __init__(self, *args, **kwargs):
            super(BaseHandler, self).__init__(*args, **kwargs)
            country = self.request.headers.get('X-AppEngine-Country')
            if not country == "US" and not country == "" and not country == None: # The last two handle local development
                self.redirect('international_holding_page')
                logging.info(country)
    

    This is more in keeping with DRY though I'm not certain it's the most efficient way of doing this.