We're Hiring C Developers to Work on Open Source

NGINX Unit
v. 1.26.1

Falcon§

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

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

  2. Create a virtual environment to install Falcon’s PIP package, for instance:

    $ cd /path/to/app/
    $ python --version
          Python X.Y.Z
    $ python -m venv venv
    $ source venv/bin/activate
    $ pip install falcon
    $ deactivate
    

    Warning

    Create your virtual environment with a Python version that matches the language module from Step 1 up to the minor number (X.Y in this example). Also, the app type in Step 5 must resolve to a similarly matching version; Unit doesn’t infer it from the environment.

  3. Let’s try an updated version of the quickstart app:

    import falcon
    
    
    # Falcon follows the REST architectural style, meaning (among
    # other things) that you think in terms of resources and state
    # transitions, which map to HTTP verbs.
    class HelloUnitResource:
        def on_get(self, req, resp):
            """Handles GET requests"""
            resp.status = falcon.HTTP_200  # This is the default status
            resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
            resp.text = ('Hello, Unit!')
    
    # falcon.App instances are callable WSGI apps
    # in larger applications the app is created in a separate file
    app = falcon.App()
    
    # Resources are represented by long-lived class instances
    hellounit = HelloUnitResource()
    
    # hellounit will handle all requests to the '/unit' URL path
    app.add_route('/unit', hellounit)
    

    Note that we’ve dropped the server code; save the file as /path/to/app/wsgi.py.

    import falcon
    import falcon.asgi
    
    
    # Falcon follows the REST architectural style, meaning (among
    # other things) that you think in terms of resources and state
    # transitions, which map to HTTP verbs.
    class HelloUnitResource:
        async def on_get(self, req, resp):
            """Handles GET requests"""
            resp.status = falcon.HTTP_200  # This is the default status
            resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
            resp.text = ('Hello, Unit!')
    
    # falcon.asgi.App instances are callable ASGI apps...
    # in larger applications the app is created in a separate file
    app = falcon.asgi.App()
    
    # Resources are represented by long-lived class instances
    hellounit = HelloUnitResource()
    
    # hellounit will handle all requests to the '/unit' URL path
    app.add_route('/unit', hellounit)
    

    Save the file as /path/to/app/asgi.py.

  1. Run the following command so Unit can access the application directory:

    # chown -R unit:unit /path/to/app/
    

    Note

    The unit:unit user-group pair is available only with official packages, Docker images, and some third-party repos. Otherwise, account names may differ; run the ps aux | grep unitd command to be sure.

    For further details, including permissions, see the security checklist.

  2. Next, prepare the configuration for Unit (use real values for type, home, module, protocol, and path):

    {
        "listeners": {
            "*:80": {
                "pass": "applications/falcon"
            }
        },
    
        "applications": {
            "falcon": {
                "type": "python X.Y",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "module_basename",
                "protocol": "wsgi_or_asgi",
                "callable": "app"
            }
        }
    }
    
  3. 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 -h or see Startup and Shutdown for details.

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

    $ curl http://localhost/unit
    
          Hello, Unit!