Commits

Dan Colish committed 021808f

init simple bouncer

  • Participants

Comments (0)

Files changed (3)

+application: pypy-bouncer
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+- url: .*
+  script: main.py
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run.  If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED").  If you want to manage some indexes
+# manually, move them above the marker line.  The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
+from urlparse import urljoin
+
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp import util, WSGIApplication
+
+
+class Bouncer(webapp.RequestHandler):
+
+    def __init__(self, mapping=()):
+        super(Bouncer, self).__init__(self)
+        self.mapping = mapping
+
+    @classmethod
+    def from_mapping(self, mapping):
+        return lambda: Bouncer(mapping)
+
+    def get(self, path):
+        newloc, oldbasepath, newbasepath = self.mapping
+        newpath = path.replace(oldbasepath, newbasepath)
+        newurl = urljoin(newloc, newpath)
+        #: Uncomment this line and comment the one below for testing
+        # self.response.out.write(newurl)
+        self.redirect(newurl, permanent=True)
+
+    def handle_exception(self, exception, debug_mode):
+        self.response.out.write(exception)
+
+
+def main():
+    mapping = ("https://bitbucket.org",
+               "svn/pypy/trunk",
+               "pypy/pypy/src/default")
+    application = WSGIApplication(
+        [(r'/(.*)', Bouncer.from_mapping(mapping))],
+        debug=True)
+    util.run_wsgi_app(application)
+
+
+if __name__ == '__main__':
+    main()