From 66d949e757bc8ba3ba0732158205151bbabebbd8 Mon Sep 17 00:00:00 2001 From: Northseadl Date: Thu, 5 Oct 2023 23:39:46 +0800 Subject: [PATCH] optimize plugin format --- internal/logic/registerpluginlogic.go | 6 ++++-- pkg/pluginx/manager.go | 19 ++++++++++++------- pkg/pluginx/plugin.go | 1 + pkg/pluginx/utils.go | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/internal/logic/registerpluginlogic.go b/internal/logic/registerpluginlogic.go index c35a6d00..5b589057 100644 --- a/internal/logic/registerpluginlogic.go +++ b/internal/logic/registerpluginlogic.go @@ -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 } diff --git a/pkg/pluginx/manager.go b/pkg/pluginx/manager.go index 7d4dfcf1..e861ed28 100644 --- a/pkg/pluginx/manager.go +++ b/pkg/pluginx/manager.go @@ -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 = StringToHyphen(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") } } diff --git a/pkg/pluginx/plugin.go b/pkg/pluginx/plugin.go index 3de019aa..2b63d68d 100644 --- a/pkg/pluginx/plugin.go +++ b/pkg/pluginx/plugin.go @@ -4,6 +4,7 @@ import "strings" type Plugin struct { BuildPluginItem + // todo auth Etc PluginEtcMap BackendRoutes []BackendRoute m *Manager diff --git a/pkg/pluginx/utils.go b/pkg/pluginx/utils.go index 919b2fb6..d267fbd8 100644 --- a/pkg/pluginx/utils.go +++ b/pkg/pluginx/utils.go @@ -146,3 +146,20 @@ func ReplaceFileString(filePath string, old string, new string, n int) error { err = os.WriteFile(filePath, []byte(replacedContent), 0644) return err } + +// StringToHyphen 将字符串转换为连字符字符串 +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 +}