NGINX Unit

Falcon§

使用 Unit 运行使用 Falcon Web 框架构建的应用

  1. 使用 Python 3.5+ 语言模块安装 Unit

  2. 创建一个虚拟环境来安装 Falcon 的 PIP 包,例如

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

    警告

    使用与步骤 1 中的语言模块相匹配的 Python 版本(本示例中为 X.Y)创建虚拟环境。此外,步骤 5 中的应用 类型 必须 解析 为类似的匹配版本;Unit 不会从环境中推断它。

  3. 我们尝试一下 快速入门应用 的更新版本

    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)
    

    请注意,我们已删除服务器代码;将文件另存为 /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)
    

    将文件另存为 /path/to/app/asgi.py

  1. 运行以下命令,以便 Unit 可以访问 应用程序目录

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

    注意

    unit:unit 用户组对仅适用于 官方软件包、Docker 映像 和一些 第三方存储库。否则,帐户名称可能不同;运行 ps aux | grep unitd 命令以确保这一点。

    有关更多详细信息,包括权限,请参阅 安全检查清单

  2. 接下来,准备 Unit 的配置(对 typehomemoduleprotocolpath 使用实际值)

    {
        "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. 上传更新后的配置。假设上述 JSON 已添加到 config.json

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

    注意

    控制套接字 路径可能有所不同;运行 unitd -h 或参阅 启动和关闭 了解详细信息。

    成功更新后,您的应用程序应可在侦听器的 IP 地址和端口上使用

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