NGINX Unit
v. 1.21.0

Guillotina§

To run apps built with the Guillotina web framework using Unit:

  1. Install Unit with a Python 3.7+ language module.

  2. Create a virtual environment to install Guillotina’s PIP package:

    $ cd /path/to/app/
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install guillotina
    $ deactivate
    
  3. Let’s try a version of the tutorial app, saving it as /path/to/app/asgi.py:

    from guillotina import configure
    from guillotina import content
    from guillotina import schema
    from guillotina.factory import make_app
    from zope import interface
    
    
    class IMyType(interface.Interface):
        foobar = schema.TextLine()
    
    
    @configure.contenttype(
        type_name="MyType",
        schema=IMyType,
        behaviors=["guillotina.behaviors.dublincore.IDublinCore"],
    )
    class MyType(content.Resource):
        pass
    
    
    @configure.service(
        context=IMyType,
        method="GET",
        permission="guillotina.ViewContent",
        name="@foobar",
    )
    async def foobar_service(context, request):
        return {"foobar": context.foobar}
    
    
    application = make_app(
            settings={
                "applications": ["__main__"],
                "root_user": {"password": "root"},
                "databases": {
                    "db": {"storage": "DUMMY_FILE", "filename": "dummy_file.db",}
                },
            }
        )
    

    Note that all server calls and imports are removed.

  4. Set installation path permissions to secure access, for example:

    # chown -R app_user:app_group /path/to/app/
    

    Update the placeholders above with actual values, noting them for later use in Unit configuration.

  5. Next, put together the Guillotina configuration for Unit:

    {
        "listeners": {
            "*:80": {
                "pass": "applications/guillotina"
            }
        },
    
        "applications": {
            "guillotina": {
                "type": "python 3",
                "user": "app_user",
                "group": "app_group",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "asgi",
                "protocol": "asgi"
            }
        }
    }
    
  6. Upload the updated configuration. Assuming the JSON above was added to config.json:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    Note

    The control socket path may vary; run unitd --help or see Startup for details.

  7. After a successful update, your app should be available on the listener’s IP address and port:

    $ curl -XPOST --user root:root http://localhost/db \
           -d '{ "@type": "Container", "id": "container" }'
    
          {"@type":"Container","id":"container","title":"container"}
    
    $ curl --user root:root http://localhost/db/container
    
          {
              "@id": "http://localhost/db/container",
              "@type": "Container",
              "@name": "container",
              "@uid": "84651300b2f14170b2b2e4a0f004b1a3",
              "@static_behaviors": [
              ],
              "parent": {
              },
              "is_folderish": true,
              "creation_date": "2020-10-16T14:07:35.002780+00:00",
              "modification_date": "2020-10-16T14:07:35.002780+00:00",
              "type_name": "Container",
              "title": "container",
              "uuid": "84651300b2f14170b2b2e4a0f004b1a3",
              "__behaviors__": [
              ],
              "items": [
              ],
              "length": 0
          }