Falcon§
使用 Unit 运行使用 Falcon Web 框架构建的应用
使用 Python 3.5+ 语言模块安装 Unit。
创建一个虚拟环境来安装 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 不会从环境中推断它。
我们尝试一下 快速入门应用 的更新版本
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。
运行以下命令,以便 Unit 可以访问 应用程序目录
# chown -R unit:unit /path/to/app/
有关更多详细信息,包括权限,请参阅 安全检查清单。
接下来,准备 Unit 的配置(对 type、home、module、protocol 和 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" } } }
上传更新后的配置。假设上述 JSON 已添加到
config.json
# curl -X PUT --data-binary @config.json --unix-socket \ /path/to/control.unit.sock http://localhost/config/
成功更新后,您的应用程序应可在侦听器的 IP 地址和端口上使用
$ curl http://localhost/unit Hello, Unit!