Thursday, August 20, 2009

Using bobo on top of mod_wsgi.

From the little I have looked at it, I like bobo. I guess the reason is that it doesn't try and do too much. In that sense it has that simplicity that attracted many people to the publisher handler provided with mod_python, and which allows one to actually create an application, handling multiple URLs, completely within one code file. And because bobo uses WSGI to interface to the web server, it can of course be used in conjunction with mod_wsgi and doing so is surprisingly easy.

As example, consider the simple bobo application as follows:
import bobo

def hello():
return "Hello world!"
All one has to do to turn this into a WSGI script file suitable for use with mod_wsgi is add the single line:
application = bobo.Application(bobo_resources=__name__)
This one line creates an instance of the bobo WSGI application object and assigns it to 'application', the name of the WSGI application entry point which mod_wsgi expects to exist.

The '__name__' variable here is the name of the Python module containing the code. In other words, you are telling bobo to map requests back onto the same code file.

For normal Python modules the '__name__' variable, which is automatically put there by Python, would be the module name and or package path used when importing the module. For mod_wsgi the variable doesn't have quite the same meaning, but still exists, and is still used as the key into 'sys.modules' where the loaded module is placed.

To map the bobo application file at a specific URL, one uses WSGIScriptAlias or AddHandler mechanisms as normal for mod_wsgi.

In mod_wsgi 3.0 there is also a new feature that one could use to define a handler script for all bobo application script files and which transparently performs the job of creating the instance of 'bobo.Application()' for you. If this were used, one could place bobo application script files in a directory using a specific extension, just like with PHP, and not even have to worry about adding the line above. Any change to those files would also result in the process being reloaded if using mod_wsgi daemon mode. I'll talk about this trick another time.

No comments: