- FastAPI - Discussion
- FastAPI - Useful Resources
- FastAPI - Quick Guide
- FastAPI - Deployment
- FastAPI - Mounting Flast App
- FastAPI - Middleware
- FastAPI - Mounting A Sub-App
- FastAPI - FastAPI Event Handlers
- FastAPI - Websockets
- FastAPI - Using GraphQL
- FastAPI - Using MongoDB
- FastAPI - SQL Databases
- FastAPI - Crud Operations
- FastAPI - CORS
- FastAPI - Dependencies
- FastAPI - Nested Models
- FastAPI - Response Model
- FastAPI - Header Parameters
- FastAPI - Cookie Parameters
- FastAPI - Uploading Files
- FastAPI - Accessing Form Data
- FastAPI - HTML Form Templates
- FastAPI - Static Files
- FastAPI - Templates
- FastAPI - Request Body
- FastAPI - Pydantic
- FastAPI - Parameter Validation
- FastAPI - Query Parameters
- FastAPI - Path Parameters
- FastAPI - Rest Architecture
- FastAPI - IDE Support
- FastAPI - Type Hints
- FastAPI - Uvicorn
- FastAPI - OpenAPI
- FastAPI - Hello World
- FastAPI - Introduction
- FastAPI - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
FastAPI - Mounting a Sub-App
If you have two independent FastAPI apps, one of them can be mounted on top of the other. The one that is mounted is called a sub-apppcation. The app.mount() method adds another completely "independent" apppcation in a specific path of the main app. It then takes care of handpng everything under that path, with the path operations declared in that sub-apppcation.
Let us first declare a simple FastAPI apppcation object to be used as a top level apppcation.
from fastapi import FastAPI app = FastAPI() @app.get("/app") def mainindex(): return {"message": "Hello World from Top level app"}
Then create another apppcation object subapp and add its own path operations.
subapp = FastAPI() @subapp.get("/sub") def subindex(): return {"message": "Hello World from sub app"}
Mount this subapp object on the main app by using mount() method. Two parameters needed are the URL route and name of the sub apppcation.
app.mount("/subapp", subapp)
Both the main and sub apppcation will have its own docs as can be inspected using Swagger UI.
The sub apppcation’s docs are available at http://localhost:8000/subapp/docs
Advertisements