mengstack-api/internal/modules/example/interfaces/plugin.go
MengStack Dev 9bb2c447ec
Some checks failed
CI / Build & Test (push) Failing after 40s
feat: 插件接口预留 — Plugin 接口 + Registry + 示例插件
- kernel/plugin: 定义 Plugin 接口(Metadata/FxOption/SetupRoutes/Init)
- kernel/plugin: 全局 Registry(Register/All/InitAll/Reset)
- modules/example: 完整 4 层示例插件,实现 Plugin 接口
- config: 新增 PluginConfig(enabled 列表)
- app.go: 集成插件系统 — fx.Invoke 注册 + plugin.All() 路由 + InitAll 生命周期
2026-10-03 02:07:46 +08:00

76 lines
1.8 KiB
Go

package interfaces
import (
"mengstack/internal/app/middleware"
"mengstack/internal/kernel/plugin"
"mengstack/internal/modules/example/application"
"mengstack/internal/modules/example/infrastructure"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"go.uber.org/zap"
"gorm.io/gorm"
)
// ExamplePlugin implements plugin.Plugin.
type ExamplePlugin struct {
handler *Handler
}
// New creates the example plugin.
// Call this during application bootstrap to register the plugin.
func New(handler *Handler) *ExamplePlugin {
return &ExamplePlugin{handler: handler}
}
func (p *ExamplePlugin) Metadata() plugin.Metadata {
return plugin.Metadata{
Name: "example",
Version: "0.1.0",
Description: "示例插件 — 演示插件开发模式",
}
}
func (p *ExamplePlugin) FxOption() fx.Option {
return fx.Module("example-plugin",
fx.Provide(
infrastructure.NewRepository,
application.NewService,
NewHandler,
),
fx.Invoke(func(db *gorm.DB, log *zap.Logger) {
if err := infrastructure.Migrate(db); err != nil {
log.Fatal("example plugin migration failed", zap.Error(err))
}
}),
)
}
func (p *ExamplePlugin) SetupRoutes(engine *gin.Engine, authMW gin.HandlerFunc) {
g := engine.Group("/api/v1/examples", authMW, middleware.MultiTenant())
g.POST("", p.handler.Create)
g.GET("", p.handler.List)
g.GET("/:id", p.handler.Get)
g.DELETE("/:id", p.handler.Delete)
}
func (p *ExamplePlugin) Init() error {
return nil
}
// Module is the fx module for the example plugin.
// It provides all dependencies and the ExamplePlugin itself.
var Module = fx.Module("example-plugin",
fx.Provide(
infrastructure.NewRepository,
application.NewService,
NewHandler,
New,
),
fx.Invoke(func(db *gorm.DB, log *zap.Logger) {
if err := infrastructure.Migrate(db); err != nil {
log.Fatal("example plugin migration failed", zap.Error(err))
}
}),
)