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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Pass custom middlewares to the app #228

Closed
lorenzomassimiani opened this issue Aug 27, 2024 · 5 comments · Fixed by #241
Closed

Feature: Pass custom middlewares to the app #228

lorenzomassimiani opened this issue Aug 27, 2024 · 5 comments · Fixed by #241
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@lorenzomassimiani
Copy link
Contributor

🚀 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.

@lorenzomassimiani lorenzomassimiani added enhancement New feature or request help wanted Extra attention is needed labels Aug 27, 2024
@lorenzomassimiani lorenzomassimiani changed the title Pass custom middlewares to the app Feature: Pass custom middlewares to the app Aug 27, 2024
@aniketmaurya
Copy link
Collaborator

sounds good. do you want to contribute?

@williamFalcon
Copy link
Contributor

@lorenzomassimiani awesome idea.
submit a PR and we'll help you land it!

@bhimrazy
Copy link
Contributor

bhimrazy commented Aug 29, 2024

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.

image

@bhimrazy
Copy link
Contributor

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=["*"],
)

@aniketmaurya
Copy link
Collaborator

aniketmaurya commented Aug 30, 2024

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": ["*"]})])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@williamFalcon @aniketmaurya @bhimrazy @lorenzomassimiani and others