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

Add register_middleware hook #50

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions fps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def _load_middlewares(app: FastAPI) -> None:
Config(FPSConfig).enabled_plugins
and p_name not in Config(FPSConfig).enabled_plugins
)
or p_name not in Config(FPSConfig).middlewares
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order of middlewares is not guaranteed here
I think you need to :

  • collect middlewares from enabled plugins
  • check middlewares listed in FPS config are found
  • add them on the app in the config order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the middlewares config should specify plugins, or middlewares inside plugins?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems natural to let each plugin specify its middlewares' order, and thus the middlewares config should rather specify plugins, but it looks like the middlewares' order we get in grouped_middlewares doesn't respect the registration order.

)
if not middlewares or disabled:
disabled_msg = " (disabled)" if disabled else ""
Expand All @@ -303,13 +304,6 @@ def _load_middlewares(app: FastAPI) -> None:
continue

for plugin_middleware, plugin_kwargs in middlewares:
if plugin_middleware in [
middleware.cls for middleware in app.user_middleware
]:
logger.error(
f"Redefinition of middleware '{plugin_middleware}' is not allowed."
)
exit(1)
app.add_middleware(
plugin_middleware,
**plugin_kwargs,
Expand Down
5 changes: 4 additions & 1 deletion fps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class FPSConfig(BaseModel):
enabled_plugins: List[str] = []
disabled_plugins: List[str] = []

@validator("enabled_plugins", "disabled_plugins")
# plugin middlewares
middlewares: List[str] = []

@validator("enabled_plugins", "disabled_plugins", "middlewares")
def plugins_format(cls, plugins):
warnings = [p for p in plugins if p.startswith("[") or p.endswith("]")]
if warnings:
Expand Down