Adding RESTful web service support to an Apache2 + WSGI setup

authored by Frank Lynam at 18/02/2013 14:58:44

As part of my RDF triple store setup I need to be able to serve RESTful web services alongside static HTML files and SPARQL queries. RESTful web services can be thought of as analogous to networked function calls. They can be accessed in various ways but my favourite is to use AJAX from within JavaScript client-side code. This post outlines how I added a RESTful web service endpoint to my Apache2 + WSGI setup.

There a number of options available here (http://stackoverflow.com/questions/415192/best-way-to-create-a-simple-python-web-service) but in the end I chose to go with the web.py solution. This layer sits on top of the WSGI interface (which provides Python script endpoints to a server). Here’s how you add in web.py support.

Go to the conf file for the particular Apache2 virtual host that you are interested in modifying. For my setup, this file is located at /etc/apache2/sites-available/default. Add the following instructions inside your virtual host’s tag:


WSGIScriptAlias /myapp /var/www/python/web/ws.py/
<Directory /var/www/python/web>
                Order deny,allow
                Allow from all
                AllowOverride All
</Directory>

This tells Apache2 to serve up the /var/www/python/web/ws.py file whenever a request comes in to http://server_address/myapp. The Directory tag includes some default settings but, importantly, it also adds the ‘AllowOverride All’ setting. This allows you to add a .htaccess file to the /var/www/python/web directory. The .htaccess file contains the same types of configuration commands that are found in your virtual host’s configuration file and in any of the other various files that Apache2 uses to get its configuration. The .htaccess file is, however, only read and applied whenever its parent folder is accessed and its settings only apply to resources found within the folder.

Create a .htaccess file in /var/www/python/web. chmod it to something like 644 so that it is accessible to all users on the system and then add the following configuration commands.


<Files ws.py>
        SetHandler wsgi-script
        Options ExecCGI FollowSymLinks
</Files>

This instructs Apache2 to run the code contained in the /var/www/python/web/ws.py file through the WSGI interpreter before sending the output on to the client. Here’s the code for my ws.py file.


import web

urls = (
    '/.*', 'index'
)

class index:
    def GET(self):
        return "Hi there! Welcome to web services using web.py"

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

The code contained here should be fairly self-explanatory. Basically, web service calls that request the ws.py resource will be directed to the relevant function in the index class depending on which HTTP verb is used by the client.

When you have setup all that is detailed above, restart your Apache2 service and then try to access your server at http://server_address/myapp. If you try this in your browser, the GET request sent by the browser should return ‘Hi there! Welcome to web services using web.py’.

I’d recommend using the curl command line app to test drive the addition of different HTTP verbs.

Comments

submit