diff --git a/server/initialize/router.go b/server/initialize/router.go index 213a3bbe4c88b1d25a8fb5b3f91c62799f9801a1..abc54e9847ec6530b41877ea090c9aab7b31756b 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -5,6 +5,7 @@ import ( "gin-vue-admin/global" "gin-vue-admin/middleware" "gin-vue-admin/router" + "gin-vue-admin/router/example" "net/http" "github.com/gin-gonic/gin" @@ -35,6 +36,9 @@ func Routers() *gin.Engine { systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权 systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关 } + + example.PluginInit(PublicGroup) + PrivateGroup := Router.Group("") PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()) { diff --git a/server/router/example/plugin.go b/server/router/example/plugin.go index c2b61fef37ed208df921d13503e893938306181b..6136cf9dc6892ce171ab9241aa6076c910791caa 100644 --- a/server/router/example/plugin.go +++ b/server/router/example/plugin.go @@ -1,16 +1,31 @@ package example -import "github.com/gin-gonic/gin" +import ( + "gin-vue-admin/utils/plugin" -type Plugin struct { + "github.com/gin-gonic/gin" +) + +var Plugin []plugin.Plugin = []plugin.Plugin{&PluginExample} + +var PluginExample = pluginExample{} + +type pluginExample struct { } -func (*Plugin) Register(group *gin.RouterGroup) { +func (*pluginExample) Register(group *gin.RouterGroup) { group.GET("hello", func(context *gin.Context) { context.JSON(200, "hello world") }) } -func (*Plugin) RouterPath() string { +func (*pluginExample) RouterPath() string { return "group" } + +func PluginInit(group *gin.RouterGroup) { + for i := range Plugin { + PluginGroup := group.Group(Plugin[i].RouterPath()) + Plugin[i].Register(PluginGroup) + } +} diff --git a/server/utils/plugin/plugin.go b/server/utils/plugin/plugin.go index f5e7a5360136b0c6061f828346bb2edb7b5ef4d8..a59d5b52a9336d7f8a32812b6931bbbd8ab41698 100644 --- a/server/utils/plugin/plugin.go +++ b/server/utils/plugin/plugin.go @@ -1,9 +1,6 @@ package plugin import ( - "plugin" - "sync" - "github.com/gin-gonic/gin" ) @@ -11,26 +8,6 @@ const ( OnlyFuncName = "Plugin" ) -var ManagementPlugin = managementPlugin{mp: make(map[string]*plugin.Plugin)} - -type managementPlugin struct { - mp map[string]*plugin.Plugin - sync.Mutex -} - -func (m *managementPlugin) SetPlugin(key string, p *plugin.Plugin) { - m.Lock() - defer m.Unlock() - m.mp[key] = p -} - -func (m *managementPlugin) GetPlugin(key string) (p *plugin.Plugin, ok bool) { - m.Lock() - defer m.Unlock() - p, ok = m.mp[key] - return -} - // Plugin 插件模式接口化 type Plugin interface { // Register 注册路由 diff --git a/server/utils/plugin/plugin_uinx.go b/server/utils/plugin/plugin_uinx.go index 69a9d1b6ee8c8a73cbcaa9b1d70e7d04485d5e22..dd42aa7dd390585ceefff5e04faaa1bcf8d23297 100644 --- a/server/utils/plugin/plugin_uinx.go +++ b/server/utils/plugin/plugin_uinx.go @@ -10,8 +10,29 @@ import ( "os" "path/filepath" "plugin" + "sync" ) +var ManagementPlugin = managementPlugin{mp: make(map[string]*plugin.Plugin)} + +type managementPlugin struct { + mp map[string]*plugin.Plugin + sync.Mutex +} + +func (m *managementPlugin) SetPlugin(key string, p *plugin.Plugin) { + m.Lock() + defer m.Unlock() + m.mp[key] = p +} + +func (m *managementPlugin) GetPlugin(key string) (p *plugin.Plugin, ok bool) { + m.Lock() + defer m.Unlock() + p, ok = m.mp[key] + return +} + // LoadPlugin 加载插件 传入path func LoadPlugin(path string) error { path, err := filepath.Abs(path)