Skip to content

Add plugin system hook function mechanism#1142

Merged
wu-clan merged 6 commits intofastapi-practices:masterfrom
yuWorm:plugin-setup
Apr 8, 2026
Merged

Add plugin system hook function mechanism#1142
wu-clan merged 6 commits intofastapi-practices:masterfrom
yuWorm:plugin-setup

Conversation

@yuWorm
Copy link
Copy Markdown
Contributor

@yuWorm yuWorm commented Apr 5, 2026

主要添加lifespan全局注册器,方便插件注入lifespan,添加setup初始化支持,方便插件在启动的时候对fastapi进行一些操作,比如apikey的替换中间件,这样几乎大部分都无须修改原项目,通过插件即可实现。添加一些补丁函数

  • Add global LifespanRegistry for dynamic lifespan registration via decorator or direct call
  • Implement setup_plugins to auto-invoke plugin init.setup(app) with logging
  • Add patching module for runtime middleware and route replacement
  • Integrate lifespan registry and setup_plugins into app registrar

yuWorm and others added 3 commits April 5, 2026 14:11
…ng utilities

- Add global LifespanRegistry for dynamic lifespan registration via decorator or direct call
- Implement setup_plugins to auto-invoke plugin __init__.setup(app) with logging
- Add patching module for runtime middleware and route replacement
- Integrate lifespan registry and setup_plugins into app registrar

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@wu-clan wu-clan changed the title Enhance plugin system with lifespan registry, setup hooks, and patchi… Add plugin system hook function mechanism Apr 7, 2026
@wu-clan
Copy link
Copy Markdown
Member

wu-clan commented Apr 7, 2026

优化了部分实现,以更加符合当前以标题为目标的方向

@yuWorm
Copy link
Copy Markdown
Contributor Author

yuWorm commented Apr 7, 2026

测试了下,没啥问题,lifespan hook和startup hook都ok,然后patching中的replace_middleware好像只有在startup中调用才行,因为到lifespan的时候,fastapi已经可以算是启动了,middleware已经处理了,替换逻辑得换,所以我直接移除函数注释了,本身这个场景就在启动之前最简单。等合并了我就去改下api key plugin

@wu-clan wu-clan self-requested a review April 8, 2026 02:33
@wu-clan wu-clan merged commit b71179c into fastapi-practices:master Apr 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants