Some checks failed
CI / Build & Test (push) Failing after 31s
fx.Invoke 执行顺序不确定导致插件路由未注册,改为在 New() 中自注册
77 lines
1.9 KiB
Go
77 lines
1.9 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 and registers it in the global plugin registry.
|
|
func New(handler *Handler) *ExamplePlugin {
|
|
p := &ExamplePlugin{handler: handler}
|
|
plugin.Register(p)
|
|
return p
|
|
}
|
|
|
|
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))
|
|
}
|
|
}),
|
|
)
|