Skip to content

Commit

Permalink
optimize plugin format
Browse files Browse the repository at this point in the history
  • Loading branch information
northseadl committed Oct 5, 2023
1 parent 4161e7c commit 281ace2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
6 changes: 4 additions & 2 deletions internal/logic/registerpluginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ func (l *RegisterPluginLogic) RegisterPlugin(req *types.RegisterPluginRequest) (
})
}
l.Infof("plugin register request: %v", req)
err = l.svcCtx.Plugin.Register(req.Name, req.Addr, routes)
etc, err := l.svcCtx.Plugin.Register(req.Name, req.Addr, routes)
if err != nil {
return nil, err
}
l.Infof("plugin %s registered", req.Name)

return
return &types.RegisterPluginReply{
Etc: etc,
}, nil
}
19 changes: 12 additions & 7 deletions pkg/pluginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,19 @@ func (m *Manager) InitRoute() {
}
}

func (m *Manager) Register(name string, addr string, routes []BackendRoute) error {
name = strings.ToLower(name)
if _, ok := m.pluginMap[name]; ok {
m.pluginMap[name].BackendRoutes = routes
m.pluginMap[name].PluginHost = addr
return nil
// Register 注册插件并且返回配置
func (m *Manager) Register(name string, addr string, routes []BackendRoute) (PluginEtcMap, error) {
// name must be hyphenated
name = StringToHyphenCase(name)

// todo validate plugin licence and key, and active plugin auth config

if p, ok := m.pluginMap[name]; ok {
p.BackendRoutes = routes
p.PluginHost = addr
return p.Etc, nil
} else {
return errors.New("plugin not found, registration failed")
return nil, errors.New("plugin not found, registration failed")
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/pluginx/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "strings"

type Plugin struct {
BuildPluginItem
// todo auth
Etc PluginEtcMap
BackendRoutes []BackendRoute
m *Manager
Expand Down
33 changes: 33 additions & 0 deletions pkg/pluginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,36 @@ func ReplaceFileString(filePath string, old string, new string, n int) error {
err = os.WriteFile(filePath, []byte(replacedContent), 0644)
return err
}

// StringToHyphenCase 将字符串转换为连缀符格式
func StringToHyphenCase(s string) string {
var result string
for i, r := range s {
if i == 0 {
result += strings.ToLower(string(r))
} else {
if r >= 'A' && r <= 'Z' {
result += "-" + strings.ToLower(string(r))
} else {
result += string(r)
}
}
}
return result
}

func StringToHyphen(s string) string {
var result string
for i, r := range s {
if i == 0 {
result += strings.ToLower(string(r))
} else {
if r >= 'A' && r <= 'Z' {
result += "-" + strings.ToLower(string(r))
} else {
result += string(r)
}
}
}
return result
}

0 comments on commit 281ace2

Please sign in to comment.