Google has a document that describes how to
run the application. At the bottom of the page is a tip that states, "You could have all non-static URLs go to a single Python script, and have the script dispatch all dynamic URLs to handlers." In this post I will expand on this statement by showing the implementation I came up with for
etapic.name. Here is the basic structure of my main method.
def main():
url_path=r'%s(.*)'
url_mappings=[]
for handler in [FirstRequestHandler,
SecondRequestHandler,
…,
RootRequestHandler]:
url_mappings.append((url_path%handler.app_url, handler))
from google.appengine.ext.webapp import WSGIApplication
application=WSGIApplication(url_mappings, debug=True)
from wsgiref.handlers import CGIHandler
CGIHandler().run(application)
First I setup a format string for the url_path associated with each handler. Each of my handlers has a field named app_url which is the prefix for all URLs associated with the handler. The final handler in the list in the RootRequestHandler and its app_url is ‘/’. I iterate over a list of handlers and set up the url_mappings list.
All of my handlers extend my BaseHandler. The BaseHandler has methods to create the base template dictionary, to handle get and post requests and to handle exceptions. Each of the Handlers implements the get method and imports their module and call the base classes handle method.