Werkzeug

wsgi utility collection


Werkzeug based WSGI application example

The following is the application() we use on our site. It routes endpoints like 'main.index' to 'main.views.index' and 'admin.main.index' to 'main.adminviews.index'. It automatically imports the modules and stores them in views for later lookup. It also constructs a Request and stores it into a Werkzeug local store, and exposes it through lib.http.request. This way, views need not pass around request everywhere, as is the case with for example django. I've included a subset of our URL routes and a view example below.

from werkzeug.exceptions import HTTPException, NotFound
from werkzeug.routing import RequestRedirect

from .lib import local
from .lib.http import Response, request, create_request
from .lib.utils import dynamic_import
from .account.utils import request_init as account_request_init
from .main.views import page_not_found, server_error
from .urls import url_map
from . import settings

views = {}

@local.manager.middleware
def application(environ, start_response):
    create_request(environ, url_map) # simply does local.request = Request(*args, **kwargs)
    account_request_init()
    try:
        # Map url to view and call it
        endpoint, params = request.url_map.match(environ.get('PATH_INFO') or '/')
        if endpoint not in views:
            module, view = endpoint.rsplit('.', 1)
            views[endpoint] = dynamic_import(settings.APP_NAME + '.' + (module[6:] + '.adminviews.' if module.startswith('admin.') else module + '.views.') + view)
        response = views[endpoint](**params)

        # Disable caching in admin
        if request.path.startswith("/admin"):
            response.headers["Pragma"] = "no-cache"
    except RequestRedirect, e:
        response = e        
    except NotFound:
        response = page_not_found()
    except:
        if not settings.DEBUG:
            response = server_error()
        else: raise
    return response(environ, start_response)

URL Routes example:

url_map = Map([
    EndpointPrefix('main.', [
        Rule('/', endpoint='index'), 
        Rule('/about', endpoint='about'),
        # ...
    ]),
    # ...
    Submount('/admin', [EndpointPrefix('admin.', [
        EndpointPrefix('main.', [
            Rule('/', endpoint='index'),
            # ...
        ]), 
        # ...
    ])
])

Example views:

def page_not_found():
    r = render('404.html')
    r.status_code = 404
    return r

def about():
    admins = Group.objects.get(name='Administrators').primary_members.order_by('username')
    staff = Group.objects.get(name='Staff').primary_members.order_by('username')
    return render('main/about.html', admins=admins, staff=staff)

I've omitted various startup details such as ORM configuration, dev server and enabling debugging. If there is any interest in seeing these bits or any questions, I'm on IRC using the nick prencher, in the #pocoo channel on freenode.