link.wsgi
latest
  • Tutorial
  • API documentation
link.wsgi
  • Docs »
  • link.wsgi 0.4 documentation
  • Edit on GitHub

Welcome to link.wsgi’s documentation!¶

link.wsgi is a fully configurable WSGI microframework. The only needed developments are routes and eventually middlewares.

The rest is done via the configuration of the library, allowing the reuse of the code.

Check out the source code on Github.

License Development Status Latest release Supported Python versions Supported Python implementations Download format Build status Code test coverage Downloads Code Health

Installation¶

pip install link.wsgi

Contents¶

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

API documentation¶

link.wsgi package¶

Submodules¶
link.wsgi.app module¶
class link.wsgi.app.Application(*args, **kwargs)[source]¶

Bases: object

WSGI Application class.

link.wsgi.middleware module¶
class link.wsgi.middleware.Middleware(*args, **kwargs)[source]¶

Bases: object

Middleware class.

Applied before and after requests are handled.

after(req, resp, handler)[source]¶

Called after request is handled.

Parameters:
  • req (link.wsgi.req.Request) – request that was handled
  • resp (link.wsgi.resp.Response) – response that was returned
  • handler (callable) – handler that was used
before(req, resp, handler)[source]¶

Called before request is handled.

Parameters:
  • req (link.wsgi.req.Request) – request that will be handled
  • resp (link.wsgi.resp.Response) – response that will be returned
  • handler (callable) – handler that will be used
Returns:

True to abort request handling

Return type:

boolean

link.wsgi.req module¶
class link.wsgi.req.Request(environ, *args, **kwargs)[source]¶

Bases: object

Request object encapsulating WSGI environ dict.

charsets¶
content¶
content_length¶
content_type¶
method¶
path¶
query¶
link.wsgi.resp module¶
class link.wsgi.resp.Response(start_response, *args, **kwargs)[source]¶

Bases: object

Response object encapsulating WSGI response handler.

content¶
headers¶
status¶
link.wsgi.router module¶
class link.wsgi.router.Router(urlpatterns=None, middlewares=None, *args, **kwargs)[source]¶

Bases: object

Request dispatcher.

Contains URL patterns as dict:

  • a regex to match the URL as key
  • a dict associated HTTP methods to Python callable objects

Also contains list of middlewares (Python classes) to apply.

Example of configuration:

{
   "ROUTER": {
      "urlpatterns": {
         "^/hello$": {
            "GET": "python.path.to.hello_function"
         }
      },
      "middlewares": [
         "python.path.to.MiddlewareClass"
      ]
   }
}
dispatch(req, resp)[source]¶

Dispatch request to handler, which will fill response.

Parameters:
  • req (link.wsgi.req.Request) – request object
  • resp (link.wsgi.resp.Response) – response object
middlewares¶
urlpatterns¶
link.wsgi.url module¶
link.wsgi.url.parse_qs(query)[source]¶

Override six.moves.urllib.parse.parse_qs to handle array parameters

Module contents¶

Donating¶

Support via Liberapay

© Copyright 2016, David Delassus. Revision 8328c9c8.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: latest
Versions
latest
stable
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.