Skip to content

WSGI Integration

Dawid Kraczkowski edited this page Jan 26, 2022 · 4 revisions

Running application with build-in bjoern support

Fast And Ultra-Lightweight HTTP/1.1 WSGI Server, if you haven't heard about it, I highly recommend to check it out.

Keep in mind bjoern relies on libev-dev, make sure it is installed and avaialable in your system. More information about bjoern and libev can be found here.

main.py

import chocs

app = chocs.Application()


@app.get("/hello/{name}")
def hello(request: chocs.HttpRequest) -> chocs.HttpResponse:
    return HttpResponse(body=f"Hello {request.path_parameters['name']}!")

chocs.serve(app)

To run application with bjoern package, the following function can be used

chocs.serve(app: chocs.Application, host: str = "127.0.0.1", port:int = 80, debug: bool = False)

  • app argument should point to your application
  • host defines your host by default it is your localhost
  • port defined port on which server will listen for connections
  • debug turns on or off detailed error messages in the logs

Running application with WSGI server

This tutorial will use Gunicorn as example server, this tutorial also assumes you have installed on your local machine:

  • poetry
  • python 3.7 or greater

Setting up the project

poetry init

Follow the creator steps when asked for defining main dependencies answer no, same for interactively defining dependencies and confirm generation.

Adding dependencies

poetry add chocs
poetry add gunicorn

Creating example application

main.py

import chocs

http = chocs.Application()


@http.get("/hello/{name}")
def hello(request: chocs.HttpRequest) -> chocs.HttpResponse:
    return chocs.HttpResponse(f"Hello {request.path_parameters.get('name')}!")

app = chocs.create_wsgi_handler(http, debug=False)

Running your application

poetry shell
gunicorn main:app