<p>到现在为止,你可能已经明白了。为了让MWE运行,您需要在每个函数定义之前使用微服务的端点<a href="https://realpython.com/primer-on-python-decorators/" rel="nofollow noreferrer">decorators</a>。下面的代码片段应该可以解决您的问题。
它假定您具有以下结构:</p>
<pre class="lang-sh prettyprint-override"><code>.
+ main.py
+ static
| + favicon.ico
+ templates
| + index.html
</code></pre>
<pre class="lang-py prettyprint-override"><code>from fastapi import FastAPI
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get('/')
def hello_world():
return{'hello':'world'}
@app.get('/favicon.ico')
async def favicon():
file_name = "favicon.ico"
file_path = os.path.join(app.root_path, "static")
return FileResponse(path=file_path, headers={"Content-Disposition": "attachment; filename=" + file_name})
@app.get('/abc')
def abc_test():
return{'hello':'abc'}
</code></pre>
<p>因此,您可以使用FastAPI默认ASGI服务器运行第一个应用程序</p>
<p><code>(env)$: uvicorn main:app reload host 0.0.0.0 port ${PORT}</code></p>