Python Configuration

From Ladon Webservice

Jump to: navigation, search

Configuration

Before Production State

When developing a Ladon webservice project you can test it easily any time you want using the subcommand testserve of ladonX.Yctl (where X.Y is the major.minor of your Python version). This is nice for instance if you are prototyping or just need to see some results fast. For instance if you have Ladon installed for Python 2.7 and want to test the service class implemented in mapservice.py using port 8080, you would issue the following command:

ladon2.7ctl testserve mapservice.py -p 8080
Request a feature

Help ease the development


However Ladon is not a webserver project and the testserve command will not scale properly, so as you move past the test/prototyping stages of your project you should configure a real webserver to handle requests. Ladon uses WSGI therefore any webserver that supports WSGI should be capable of running a Ladon webservice.

Production state with Apache2 and mod_wsgi

In the following section are shown some examples of how to configure Apache2 with mod_wsgi to run a Ladon webservice implemented in the module myservice.py.

Adding to an existing virtual host

Let's say you have an Apache2 server already configured with a virtual host and you just want to setup an alias to a script that can handle your Ladon Service. All you have to do is edit the configuration file containing the VirtualHost tag and add the following directive:

WSGIScriptAlias /myservice/ /srv/ladon/myservice/handler.py

In the above example your site will execute the handler script handler.py when you access the path /myservice. The handler script will instantiate the LadonWSGIApplication with information about which module your service is implemented in and where to locate it.

The LadonWSGIApplication class:

LadonWSGIApplication(service_list,[path_list,catalog_name,catalog_desc])
 
  service_list: A list of module names to be imported into the Ladon WSGI Application.
  path_list:    A list of paths to add to Python`s module search path
  catalog_name: The displayed name of the webservice catalog
  catalog_desc: The description for the webservice catalog

Let's say for this example that the Ladon webservice implemented in module myservice.py is located in the same directory as handler.py. So we have the following structure:

jakob@jakob-vaio:/$ tree /srv
/srv
├── ladon
│   └── myservice
│       ├── handler.py
│       └── myservice.py

In this case you can use the following example directly as handler:

handler.py:

from ladon.server.wsgi import LadonWSGIApplication
from os.path import abspath,dirname
 
application = LadonWSGIApplication(
  ['myservice'],
  [dirname(abspath(__file__))],
  catalog_name='My Ladon webservice catalog',
  catalog_desc='This is the root of my cool webservice catalog')

In the example above the absolute path to the location of handler.py (__file__) is used as item in the path_list.

Creating a new virtual host as a Ladon webservice

If you want to create a new site with a Ladon webservice at the root of the site, you can use the following example as template, it creates a virtual host that responds to incomming requests to hostname mysite.com:

<VirtualHost *:80>
 
        ServerName mysite.com
        ServerAdmin admin@mysite.com
 
        WSGIScriptAlias / /srv/ladon/myservice/handler.py
 
        LogLevel warn
 
        ErrorLog ${APACHE_LOG_DIR}/mysite.com_error.log
        CustomLog ${APACHE_LOG_DIR}/mysite.com_access.log combined
 
</VirtualHost>