from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app def run_app(url_mapping): application = webapp.WSGIApplication(url_mapping, debug=True) application = redirect_from_appspot(application) run_wsgi_app(application) def redirect_from_appspot(wsgi_app): """Handle redirect to my domain if called from appspot (and not SSL)""" from_server = "" to_server = "" def redirect_if_needed(env, start_response): # If we're calling on the appspot address, and we're not SSL (SSL only works on appspot) if env["HTTP_HOST"].endswith(from_server) and env["HTTPS"] == "off": # Parse the URL import webob, urlparse request = webob.Request(env) scheme, netloc, path, query, fragment = urlparse.urlsplit(request.url) url = urlparse.urlunsplit([scheme, to_server, path, query, fragment]) # Exclude /admin calls, since they're used by Cron, TaskQueues and will fail if they return a redirect if not path.startswith('/admin'): # Send redirect start_response("301 Moved Permanently", [("Location", url)]) return ["301 Moved Peramanently", "Click Here %s" % url] # Else, we return normally return wsgi_app(env, start_response) return redirect_if_needed