r/FastAPI • u/123A321 • Jan 05 '23
Question Does API Router have an `on_mounted` callback?
Here is what I would like to do:
Router has directories it needs to mount local to that router. When the router is online, it needs to fetch a reference to the main app to mount a directory (since routers do not support file mounting)
Thoughts?
0
Upvotes
2
u/[deleted] Jan 06 '23
If I understand you correctly, you want to mount a directory from the router, which contains the
APIRouterclass and not theFastAPIclass. If this is the case then I'm not sure I understand the need to get theFastAPIapp context. I think you would mount the static directory just as it's shown in the docs, but in the respective file.I decided to try this out. I created two directories,
staticandstatic2and then mounted one from theFastAPIinstance and the other with theAPIRouterinstance and it worked fine. These instances were in separate files, so this means that the way shown in the docs is done in the context of the application or sub-application context, which is clarified on that same page.If you look at the source code,
APIRouteris of typestarlette.routing.Router, which provides themountmethod.