Comments (8)
There's something similar (but not the same) to handle those cases: APIRouter
.
It also allows having the routes in different files. And it allows including routers with path prefixes that apply to all of them.
It wasn't properly documented, but it is now: https://fastapi.tiangolo.com/tutorial/bigger-applications/
E.g. a file at app/routers/users.py
with:
from fastapi import APIRouter
router = APIRouter()
@router.get("/users/", tags=["users"])
async def read_users():
return [{"username": "Foo"}, {"username": "Bar"}]
And a file at app/main.py
with:
from fastapi import FastAPI
from .routers.users import router
app = FastAPI()
app.include_router(router)
It doesn't "mount" the route, but clones its routes. And the OpenAPI schema (and docs) end up together.
Does that work for your use case?
from fastapi.
Hey, thanks for the code example and suggestion.
Unfortunately it doesn't ft what I have in mind. The idea is to have two APIs each, with their own separate docs, OpenAPI files, etc, at different bases.
For example, you might have /public/*
to serve /public/docs
, /public/openapi.json
, etc, for public use.
For internal use, you might have have /internal/*
with the same set of resources. Essentially two FastAPI instances.
This could also be useful for doing something like versioning.
from fastapi.
@kkinder Can you share why do you want to server both public and internal stuff from the same instance? Isn't there a security contradiction?
from fastapi.
@haizaar No, that's not a security problem at all. Both APIs have access to the same database, same permissions, etc.
It's more that I don't want to expose methods I intend to change the signature of soon to potential end-users.
from fastapi.
@kkinder I see - so it's not internal (network-wise) API, but rather external API that serves both internal users and other users. If so, then I wonder why not having separate backends for those and route traffic to them using API GW? Should help with independent updates, etc.
from fastapi.
@haizaar Just more hassle. The model is the same, the authentication is the same, the middleware is the same, the update cycle is the same. I just want some methods listed on different swagger files, more or less.
from fastapi.
Guys, this should be solved by PR #26 by @kabirkhan.
It is released as 0.4.0
, the new docs are here: https://fastapi.tiangolo.com/tutorial/sub-applications-proxy/ 🎉 🚀
Let me know if it works for you.
As a side note, I think I tend to do the same as @haizaar, to set up different APIs in different backends (and different containers too), and then put them behind a load balancer (Traefik in my case).
But I also see how @kkinder's use case and others including having a reverse proxy make this feature relevant (so much there's another issue and this PR from above).
from fastapi.
I guess this was solved. I'll close this issue now. But feel free to add more comments or create new issues.
from fastapi.
Related Issues (20)
- No streaming interface can not support concurrency for two fastapi servers
- Using pydantic Json Type as Form data type doesn't work HOT 1
- Raw docstring (leading `r`) defeats form feed `\f` truncation HOT 3
- OpenAPI Example with multipart/form-data not showing up HOT 5
- [BUG] Using Nested Pydantic models and `params: MyModel = Depends()` forces OpenAPI docs GET methods to require a request body. HOT 6
- How to fix this bug? HOT 2
- [BUG] Upgrade python-mulipart==0.0.7 from low version fastapi upload file may be 400 HOT 4
- Use `RootModel` as query parameter HOT 2
- Context managers in `Depends` are broken after 0.106 HOT 17
- Middleware runs twice HOT 8
- Support for Pydantic deprecated fields HOT 1
- axios can't receive error response status code
- Potential footgun when using custom `Response(background=<Task()>)` in conjunction with injected `BackgroundTasks` - the custom response overwrites the tasks HOT 3
- lifespan
- Breaking change with path parameters when updating to pydantic>=2 from pydantic<2 HOT 1
- trying to live video stream using Fastapi
- Package on test.pypi.org is broken
- middleware type
- It throws an exception when I specify return Http status code
- When backend restart the frontend request a protected api cause an unhandle exception: Exception in ASGI application HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi.