My project directory structure is like this:
.
├── .env
│ ├── Include
│ ├── Lib
│ ├── Scripts
│ └── pyvenv.cfg
├── .vscode
│ └── launch.json
└── src
├── __pycache__
└── main.py
I write some code in the main.py file:
import fastapi
from fastapi import FastAPI
app=FastAPI()
@app.get("/")
def health():
return "ok"
@app.get("/version")
def version():
return fastapi.__version__
Then launch the project: uvicorn main:app --port 9090 --root-path src --reload
I can request APIs successed, but when I try to browser the swagger documents, I will get a fetch error:
INFO: 127.0.0.1:50659 - "GET /docs HTTP/1.1" 200 OK
INFO: 127.0.0.1:50659 - "GET /src/openapi.json HTTP/1.1" 404 Not Found
Should I do some configuration to resolve this problem? I use python v3.10.5
and fastapi v0.85.0
, thanks!
CodePudding user response:
The root-path
parameter is used in situations where you use a reverse proxy. If your reverse proxy adds a path to your URL, your code can still ignore that path.
From the docs:
Having a proxy with a stripped path prefix, in this case, means that you could declare a path at /app in your code, but then, you add a layer on top (the proxy) that would put your FastAPI application under a path like /api/v1.
In this case, the original path /app would actually be served at /api/v1/app.
Even though all your code is written assuming there's just /app.
It is not used to point to a folder in your project structure. So, remove the root-path param in your uvicorn command and you should be good to go.