| 1 |
# FastAPI CLI { #fastapi-cli } |
| 2 |
|
| 3 |
**FastAPI <abbr title="command line interface - 命令行接口">CLI</abbr>** 是一个命令行程序,你可以用它来部署和运行你的 FastAPI 应用、管理 FastAPI 项目,等等。 |
| 4 |
|
| 5 |
当你安装 FastAPI(例如使用 `pip install "fastapi[standard]"`)时,会附带一个可以在终端中运行的命令行程序。 |
| 6 |
|
| 7 |
要在开发环境中运行你的 FastAPI 应用,可以使用 `fastapi dev` 命令: |
| 8 |
|
| 9 |
<div class="termy"> |
| 10 |
|
| 11 |
```console |
| 12 |
$ <font color="#4E9A06">fastapi</font> dev |
| 13 |
|
| 14 |
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀 |
| 15 |
|
| 16 |
Searching for package file structure from directories with |
| 17 |
<font color="#3465A4">__init__.py</font> files |
| 18 |
Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font> |
| 19 |
|
| 20 |
<span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py |
| 21 |
|
| 22 |
<span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with the |
| 23 |
following code: |
| 24 |
|
| 25 |
<u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u> |
| 26 |
|
| 27 |
<span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font> |
| 28 |
|
| 29 |
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> |
| 30 |
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000/docs</u></font> |
| 31 |
|
| 32 |
<span style="background-color:#007166"><font color="#D3D7CF"> tip </font></span> Running in development mode, for production use: |
| 33 |
<b>fastapi run</b> |
| 34 |
|
| 35 |
Logs: |
| 36 |
|
| 37 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories: |
| 38 |
<b>[</b><font color="#4E9A06">'/home/user/code/awesomeapp'</font><b>]</b> |
| 39 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C to |
| 40 |
quit<b>)</b> |
| 41 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles |
| 42 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b> |
| 43 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup. |
| 44 |
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete. |
| 45 |
``` |
| 46 |
|
| 47 |
</div> |
| 48 |
|
| 49 |
/// tip | 提示 |
| 50 |
|
| 51 |
在生产环境中,你会使用 `fastapi run` 而不是 `fastapi dev`。🚀 |
| 52 |
|
| 53 |
/// |
| 54 |
|
| 55 |
在内部,**FastAPI CLI** 使用 [Uvicorn](https://www.uvicorn.dev),这是一个高性能、适用于生产环境的 ASGI 服务器。😎 |
| 56 |
|
| 57 |
`fastapi` CLI 会尝试自动检测要运行的 FastAPI 应用,默认假设它是文件 `main.py` 中名为 `app` 的对象(或少数其他变体)。 |
| 58 |
|
| 59 |
但你也可以显式配置要使用的应用。 |
| 60 |
|
| 61 |
## 在 `pyproject.toml` 中配置应用的 `entrypoint` { #configure-the-app-entrypoint-in-pyproject-toml } |
| 62 |
|
| 63 |
你可以在 `pyproject.toml` 文件中配置应用的位置,例如: |
| 64 |
|
| 65 |
```toml |
| 66 |
[tool.fastapi] |
| 67 |
entrypoint = "main:app" |
| 68 |
``` |
| 69 |
|
| 70 |
这个 `entrypoint` 会告诉 `fastapi` 命令按如下方式导入应用: |
| 71 |
|
| 72 |
```python |
| 73 |
from main import app |
| 74 |
``` |
| 75 |
|
| 76 |
如果你的代码结构如下: |
| 77 |
|
| 78 |
``` |
| 79 |
. |
| 80 |
├── backend |
| 81 |
│ ├── main.py |
| 82 |
│ ├── __init__.py |
| 83 |
``` |
| 84 |
|
| 85 |
那么你可以将 `entrypoint` 设置为: |
| 86 |
|
| 87 |
```toml |
| 88 |
[tool.fastapi] |
| 89 |
entrypoint = "backend.main:app" |
| 90 |
``` |
| 91 |
|
| 92 |
这等价于: |
| 93 |
|
| 94 |
```python |
| 95 |
from backend.main import app |
| 96 |
``` |
| 97 |
|
| 98 |
### 带路径或使用 `--entrypoint` CLI 选项的 `fastapi dev` { #fastapi-dev-with-path-or-with-entrypoint-cli-option } |
| 99 |
|
| 100 |
你也可以把文件路径传给 `fastapi dev` 命令,它会猜测要使用的 FastAPI 应用对象: |
| 101 |
|
| 102 |
```console |
| 103 |
$ fastapi dev main.py |
| 104 |
``` |
| 105 |
|
| 106 |
或者,你也可以给 `fastapi dev` 命令传入 `--entrypoint` 选项: |
| 107 |
|
| 108 |
```console |
| 109 |
$ fastapi dev --entrypoint main:app |
| 110 |
``` |
| 111 |
|
| 112 |
但每次运行 `fastapi` 命令都需要记得传入正确的路径或 entrypoint。 |
| 113 |
|
| 114 |
另外,其他工具可能找不到它,例如 [VS Code 扩展](editor-support.md) 或 [FastAPI Cloud](https://fastapicloud.com),因此推荐在 `pyproject.toml` 中使用 `entrypoint`。 |
| 115 |
|
| 116 |
## `fastapi dev` { #fastapi-dev } |
| 117 |
|
| 118 |
当你运行 `fastapi dev` 时,它将以开发模式运行。 |
| 119 |
|
| 120 |
默认情况下,它会启用**自动重载**,因此当你更改代码时,它会自动重新加载服务器。该功能是资源密集型的,且相较不启用时更不稳定,因此你应该仅在开发环境下使用它。它还会监听 IP 地址 `127.0.0.1`,这是你的机器仅与自身通信的 IP(`localhost`)。 |
| 121 |
|
| 122 |
## `fastapi run` { #fastapi-run } |
| 123 |
|
| 124 |
当你运行 `fastapi run` 时,它默认以生产环境模式运行。 |
| 125 |
|
| 126 |
默认情况下,**自动重载是禁用的**。它将监听 IP 地址 `0.0.0.0`,即所有可用的 IP 地址,这样任何能够与该机器通信的人都可以公开访问它。这通常是你在生产环境中运行它的方式,例如在容器中运行。 |
| 127 |
|
| 128 |
在大多数情况下,你会(且应该)有一个“终止代理”在上层为你处理 HTTPS,这取决于你如何部署应用程序,你的服务提供商可能会为你处理此事,或者你可能需要自己设置。 |
| 129 |
|
| 130 |
/// tip | 提示 |
| 131 |
|
| 132 |
你可以在[部署文档](deployment/index.md)中了解更多。 |
| 133 |
|
| 134 |
/// |