Ian Bicking: the old part of his blog

Composing WSGI Apps

Dave Warnock is feeling enthusiastic about WSGI and Paste, so I thought I'd give a concrete example of what you can do now...

To get an application running under Paste, the first thing is to tell Paste how to make the application. Let's say your setting up the filebrowser application:

framework = 'wareweb'
root_path = '.../Paste/examples/filebrowser'
publish_dir = os.path.join(root_path, 'web')
# This configuration is for the filebrowser app:
browse_path = '/site1/htdocs'

Let's say you put that in site1_fb.conf. From there, you create a server.conf file like:

server = 'wsgiutils'
port = 5000
urlmap['/'] = "site1_fb.conf"

Now you are serving that application alone. What if you want to support another application? Well, let's say you create site2_fb.conf that looks like the first configuration file but with browse_path = '/site2/htdocs'. Then you do:

urlmap['http://site2.com'] = "site2_fb.conf"

This sets up virtual hosting, so any requests to site2.com go to the application described by site2_fb.conf. Lets say you add a wiki application:

urlmap['/wiki'] = 'wiki.conf'

Now */wiki will go to the application you describe there. They'll all be run the same process (or the same set of worker processes if you are running a forking server).

I just added the virtual hosting feature to urlmap a couple days ago. Another feature that would be nice is a way of capturing variables as you go along. Maybe like:

urlmap['host-regex:^(?P<username>[^.]*).myuserblogs.com$'] = ...

Then I'm thinking that we'd add a key to the WSGI request in environ['paste.urlvars']['username'] (though I'd like to standardize the location where we put these kinds of variables that are found during URL resolution).

For the interested there's some more discussion about the configuration aspect on the Web-SIG mailing list; Chris McDonough recently wrote up a related proposal here.

Created 23 Jul '05