Feature: Pass custom middlewares to the app
🚀 Feature
Add a parameter "middlewares" to the LitServer class that takes a list of fastapi middlewares that will be assigned to the self.app.
Motivation
It is useful to enrich the fastapi app with custom middlewares, for example for metrics updates. Similarly it can be done for exception handlers.
sounds good. do you want to contribute?
@lorenzomassimiani awesome idea. submit a PR and we'll help you land it!
Additional Context: Need for passing CORSMiddleware
A user reported an issue on Discord where he was unable to call the LitServe API from his frontend application due to CORS (Cross-Origin Resource Sharing) restrictions.
Passing the CORS middleware would resolve this issue, allowing the API to be accessed without being blocked.
Temporary Workaround: In the meantime, here's a quick hack to resolve the CORS issue (adding any middleware) until this feature is officially integrated:
server = LitServer(....)
server.app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Now that LitServer accepts middleware as argument, users can do the following for CORS support:
api = ls.examples.SimpleLitAPI()
server = ls.LitServer(api, middlewares=[(CORSMiddleware, {"allow_origins": ["*"], "allow_credentials": True, "allow_methods": ["*"], "allow_headers": ["*"]})])