[英]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.