Skip to content
New issue

Have a question about this project?Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mounting sub-applications underAPIRouter #10180

Open
Kludexopened this issue Aug 31, 2023 Discussed in #8682 · 9 comments
Open

Mounting sub-applications underAPIRouter #10180

Kludexopened this issue Aug 31, 2023 Discussed in #8682 · 9 comments
Labels
bug Something isn't working

Comments

@Kludex
Copy link
Member

Kludex commented Aug 31, 2023

Discussed in#8682

Example Code

fromfastapiimportFastAPI,APIRouter
fromstarlette.testclientimportTestClient

app=FastAPI()
api_router=APIRouter(prefix="/api")


@api_router.get("/app")
defread_main():
return{"message":"Hello World from main app"}


app.include_router(api_router)

subapi=FastAPI()


@subapi.get("/sub")
defread_sub():
return{"message":"Hello World from sub API"}


api_router.mount("/subapi",subapi)

client=TestClient(app)

assertclient.get('/api/app').status_code==200

# this next assert fails
assertclient.get('/api/subapi/sub').status_code==200

Description

Is it possible to mount a sub-application under an APIRouter? APIRouter itself has a mount function and accepts similar arguments to mounting a sub-application on a FastAPI instance, but I can't get the routing to actually work (nor can i get the openapi docs or spec to come back from that I would assume are the correct URLs.

The docs for sub applications note that the sub-application will have it's root_path correctly set, and I've tried a few combinations of manually setting the root_path on the subapi instance, but to no avail.

Related

@Kludex Kludex added question Question or problem question-migrate bug Something isn't working and removed question Question or problem question-migrate labels Aug 31, 2023
@Kludex Kludex changed the title Mounting sub-applications under APIRouter Mounting sub-applications underAPIRouter Aug 31, 2023
@Kludex
Copy link
Member Author

Kludex commented Aug 31, 2023

Here we should either raise onAPIRouter.mountor make it work.

@achaayb
Copy link

achaayb commented Aug 31, 2023

I dont see a reason of binding two FastAPI instances, perhaps in your usecase you're trying to merge two applications, in this case you should have a service that extracts endpoints and mounts them into an APIRouter

@baskervilski
Copy link

baskervilski commented Aug 31, 2023

@Kludex,have you tried actually running the app and making a request to the endpoint of the subapp?

I have recently also struggled with a very similar setup, where I wanted to mount a v1 API and v2 API after a certain prefix, where I figured I should make a router to "hold" the/myapp/api/prefix, then just mount to/v1and/v2.

Long story short, I couldn't make it work that way, so I just mounted v1 app tomyapp/api/v1and v2 tomyapp/api/v2directly to the "root" app, no routers between.

EDIT: Also, I'm far from an expert, but I have a feeling that the TestClient class doesn't really "collaborate" with mounts... That's why I asked if you have tried to actually run the app and reach the endpoints.

@LorhanSohaky
Copy link
Contributor

Maybe related#4657

@darkb0ts
Copy link

the issue is likely related to the way you are defining the routes within the sub-application (subapi). The routing paths within subapi should not include the prefix that you intend to use when mounting it under api_router.

@LorhanSohaky
Copy link
Contributor

Even without including the prefix the problem occurs, as I reported on the issue#4656making the doc link to be wrong

@mdczaplicki
Copy link

Any update on this? It's needed.

@mdczaplicki
Copy link

https://github /tiangolo/fastapi/blob/master/fastapi/routing.py#L812
This if-elsology doesn't provide a scenario forMount.

@jfernandrezj
Copy link

Folllowing.
Would be useful to be able to serve dash dashboards by keeping the api routing logic in the routers and not an extra tree for the dashboards within dash and mount it as a sub app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants