[英]How the mount function works in fastapi when application are in two diffrent files?
我有兩個不同的 fastaoi 文件。我希望將 intergate 作為一個文件。 我已經發布了我的代碼。 請查看它,讓我知道您的想法和建議。
主文件
from fastapi import FastAPI
from app import submain as subapi
app = FastAPI()
app.mount("/subapi", subapi)
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
子主.py
from fastapi import FastAPI
subapi = FastAPI()
@subapi.get("/sub")
async def read_sub():
return {"message": "Hello World from sub API"}
錯誤
$ uvicorn app.main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [43148] using statreload
INFO: Started server process [43150]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:46334 - "GET /subapi/sub HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 394, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/fastapi/applications.py", line 190, in __call__
await super().__call__(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/routing.py", line 376, in handle
await self.app(scope, receive, send)
TypeError: 'module' object is not callable
請讓我知道如何在另一個位置或另一個文件中安裝 fastapi 應用程序?
您需要使用路由器,請在此處查看 FAST API 文檔from fastapi import APIRouter
見下文:
submain.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
from fastapi import APIRouter
subapi = APIRouter()
@subapi.get("/sub")
async def read_sub():
return {"message": "Hello World from sub API"}
main.py
:
#!/user/bin/env python
# -*- coding: utf-8 -*-
from fastapi import FastAPI
import submain
app = FastAPI()
app.mount("/subapi", submain)
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
運行代碼:
$ uvicorn main:app --reload
←[32mINFO←[0m: Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)
←[32mINFO←[0m: Started reloader process [←[36m←[1m6616←[0m] using ←[36m←[1mwatchgod←[0m
←[32mINFO←[0m: Started server process [←[36m1084←[0m]
←[32mINFO←[0m: Waiting for application startup.
←[32mINFO←[0m: Application startup complete.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.