Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added a gaerun method to start application for google app engine #178

Merged
merged 1 commit into from

3 participants

@Habel

I was trying to use google app engine for my project and ran into some difficulties with starting the web.py application in a mode that will run on GAE. I did some research and made this method that detects the version of python you're using (GAE is different depending on if you're using 2.5 or 2.7) and then does the correct thing for that version to run on GAE.

@travisbot

This pull request passes (merged 232de44 into 875ef3d).

@aaronsw
Owner

Is there some way to detect we're on GAE and build this into app.run()?

@aaronsw
Owner

Otherwise the commit is fine with me.

@Habel

I tried asking around on the GAE irc, but no one responded. I could try their issue tracker, but I can't really think of a way to detect it without some on disk stuff.

Projects are configured with an app.yaml file like this:

application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /static
  static_dir: static

- url: /.*s
  script: code.app

I could check if the file is present in the current directory and if so, read the runtime label.

@aaronsw aaronsw merged commit 4f0e7c6 into webpy:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 1 deletion.
  1. +43 −1 web/application.py
View
44 web/application.py
@@ -334,7 +334,49 @@ def cgirun(self, *middleware):
except ImportError:
# we're not running from within Google App Engine
return wsgiref.handlers.CGIHandler().run(wsgiapp)
-
+
+ def gaerun(self, *middleware):
+ """
+ Starts the program in a way that will work with Google app engine,
+ no matter which version you are using (2.5 / 2.7)
+
+ If it is 2.5, just normally start it with app.gaerun()
+
+ If it is 2.7, make sure to change the app.yaml handler to point to the
+ global variable that contains the result of app.gaerun()
+
+ For example:
+
+ in app.yaml (where code.py is where the main code is located)
+
+ handlers:
+ - url: /.*
+ script: code.app
+
+ Make sure that the app variable is globally accessible
+ """
+ wsgiapp = self.wsgifunc(*middleware)
+ try:
+ # check what version of python is running
+ version = sys.version_info[:2]
+ major = version[0]
+ minor = version[1]
+
+ if major != 2:
+ raise EnvironmentError("Google App Engine only supports python 2.5 and 2.7")
+
+ # if 2.7, return a function that can be run by gae
+ if minor == 7:
+ return wsgiapp
+ # if 2.5, use run_wsgi_app
+ elif minor == 5:
+ from google.appengine.ext.webapp.util import run_wsgi_app
+ return run_wsgi_app(wsgiapp)
+ else:
+ raise EnvironmentError("Not a supported platform, use python 2.5 or 2.7")
+ except ImportError:
+ return wsgiref.handlers.CGIHandler().run(wsgiapp)
+
def load(self, env):
"""Initializes ctx using env."""
ctx = web.ctx
Something went wrong with that request. Please try again.