Sanic§
使用 Unit 运行使用 Sanic Web 框架构建的应用
使用 Python 3.7+ 语言模块安装 Unit。
创建虚拟环境以安装 Sanic 的 PIP 包
$ cd /path/to/app/ $ python3 --version Python 3.Y.Z $ python3 -m venv venv $ source venv/bin/activate $ pip install sanic $ deactivate
警告
使用与步骤 1 中的语言模块匹配的 Python 版本创建虚拟环境,精确到次要版本号(此示例中为 3.Y)。此外,步骤 5 中的应用 类型必须 解析 为类似的匹配版本;Unit 不会从环境中推断它。
让我们尝试一个 教程应用 的版本,将其另存为 /path/to/app/asgi.py
from sanic import Sanic from sanic.response import json app = Sanic() @app.route("/") async def test(request): return json({"hello": "world"})
运行以下命令,以便 Unit 可以访问 应用程序目录
# chown -R unit:unit /path/to/app/
有关包括权限在内的更多详细信息,请参阅 安全检查表。
接下来,准备 Unit 的 Sanic 配置(对 type、home 和 path 使用真实值)
{ "listeners": { "*:80": { "pass": "applications/sanic" } }, "applications": { "sanic": { "type": "python 3.Y", "path": "/path/to/app/", "home": "/path/to/app/venv/", "module": "asgi", "callable": "app" } } }
上传更新的配置。假设上述 JSON 已添加到
config.json
# curl -X PUT --data-binary @config.json --unix-socket \ /path/to/control.unit.sock http://localhost/config/
成功更新后,您的应用应可在侦听器的 IP 地址和端口上使用
$ curl http://localhost {"hello":"world"}