Tutorial

In this tutorial, we will build a new package containing routes for our WSGI applications and then deploy everything in a virtualenv.

Simple route

A simple route is just a function which takes two parameters:

  • the received request
  • the response to fill

For example:

def hello(req, resp):
    resp.status = 200
    resp.content = 'Hello world'

See the API of Request and Response objects for more informations.

Class-based view

A route is just a callable object, if a class is used, then it will be instantiated. The called handler is a method named after HTTP verbs:

class Hello(object):
    def get(self, req, resp):
        resp.status = 200
        resp.content = 'Hello world'

A simple middleware

Middlewares are objects which are applied to the request before and after the handling of the request.

It can be any objects respecting the API of the Middleware class.

Example:

from link.wsgi.middleware import Middleware


class MyMiddleware(Middleware):
   def before(self, req, resp, handler):
      # do something with request
      # do something with resp
      # do something with handler
      return False  # True to abort the request

   def after(self, req, resp, handler):
      # do something with request
      # do something with resp
      # do something with handler

Routes configuration

Configuration file for the router is stored in:

$B3J0F_CONF_DIR/link/wsgi/router.conf

Here is an example of configuration where the simple route hello() is in the package myapp.routes and the middleware MyMiddleware is in the package myapp.middlewares:

{
   "ROUTER": {
      "urlpatterns": {
         "^/hello$": {
            "GET": "mapp.routes.hello"
         }
      },
      "middlewares": {
         "myapp.middlewares.MyMiddleware"
      }
   }
}

Deploying

Prerequisites

Make sure the command virtualenv is available.

Creating virtualenv

Assuming you’re in your Python package folder:

$ virtualenv myapp-venv
$ . ./myapp-venv/bin/activate
(myapp-venv)$ pip install supervisord gunicorn link.wsgi
(myapp-venv)$ python setup.py install

Configuring the whole thing

We need the following supervisord service:

[program:myapp]

environment=B3J0F_CONF_DIR="%(ENV_VIRTUAL_ENV)s/etc"
command=gunicorn link.wsgi.app:application

Running everything

(myapp-venv)$ supervisord
(myapp-venv)$ supervisorctl start myapp
(myapp-venv)$ curl http://localhost:8000/hello
Hello world