Skip to content

Commit 4561d86

Browse files
committed
feat:插件注册&差分so
1 parent 32327fe commit 4561d86

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

server/initialize/router.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"gin-vue-admin/global"
66
"gin-vue-admin/middleware"
77
"gin-vue-admin/router"
8+
"gin-vue-admin/router/example"
89
"net/http"
910

1011
"github.com/gin-gonic/gin"
@@ -35,6 +36,9 @@ func Routers() *gin.Engine {
3536
systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
3637
systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关
3738
}
39+
40+
example.PluginInit(PublicGroup)
41+
3842
PrivateGroup := Router.Group("")
3943
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
4044
{

server/router/example/plugin.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package example
22

3-
import "github.com/gin-gonic/gin"
3+
import (
4+
"gin-vue-admin/utils/plugin"
45

5-
type Plugin struct {
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
var Plugin []plugin.Plugin = []plugin.Plugin{&PluginExample}
10+
11+
var PluginExample = pluginExample{}
12+
13+
type pluginExample struct {
614
}
715

8-
func (*Plugin) Register(group *gin.RouterGroup) {
16+
func (*pluginExample) Register(group *gin.RouterGroup) {
917
group.GET("hello", func(context *gin.Context) {
1018
context.JSON(200, "hello world")
1119
})
1220
}
1321

14-
func (*Plugin) RouterPath() string {
22+
func (*pluginExample) RouterPath() string {
1523
return "group"
1624
}
25+
26+
func PluginInit(group *gin.RouterGroup) {
27+
for i := range Plugin {
28+
PluginGroup := group.Group(Plugin[i].RouterPath())
29+
Plugin[i].Register(PluginGroup)
30+
}
31+
}

server/utils/plugin/plugin.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
package plugin
22

33
import (
4-
"plugin"
5-
"sync"
6-
74
"github.com/gin-gonic/gin"
85
)
96

107
const (
118
OnlyFuncName = "Plugin"
129
)
1310

14-
var ManagementPlugin = managementPlugin{mp: make(map[string]*plugin.Plugin)}
15-
16-
type managementPlugin struct {
17-
mp map[string]*plugin.Plugin
18-
sync.Mutex
19-
}
20-
21-
func (m *managementPlugin) SetPlugin(key string, p *plugin.Plugin) {
22-
m.Lock()
23-
defer m.Unlock()
24-
m.mp[key] = p
25-
}
26-
27-
func (m *managementPlugin) GetPlugin(key string) (p *plugin.Plugin, ok bool) {
28-
m.Lock()
29-
defer m.Unlock()
30-
p, ok = m.mp[key]
31-
return
32-
}
33-
3411
// Plugin 插件模式接口化
3512
type Plugin interface {
3613
// Register 注册路由

server/utils/plugin/plugin_uinx.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,29 @@ import (
1010
"os"
1111
"path/filepath"
1212
"plugin"
13+
"sync"
1314
)
1415

16+
var ManagementPlugin = managementPlugin{mp: make(map[string]*plugin.Plugin)}
17+
18+
type managementPlugin struct {
19+
mp map[string]*plugin.Plugin
20+
sync.Mutex
21+
}
22+
23+
func (m *managementPlugin) SetPlugin(key string, p *plugin.Plugin) {
24+
m.Lock()
25+
defer m.Unlock()
26+
m.mp[key] = p
27+
}
28+
29+
func (m *managementPlugin) GetPlugin(key string) (p *plugin.Plugin, ok bool) {
30+
m.Lock()
31+
defer m.Unlock()
32+
p, ok = m.mp[key]
33+
return
34+
}
35+
1536
// LoadPlugin 加载插件 传入path
1637
func LoadPlugin(path string) error {
1738
path, err := filepath.Abs(path)

0 commit comments

Comments
 (0)