From 4161e7c11bc4adbb3b2c66014c41fa21b445ac0f Mon Sep 17 00:00:00 2001 From: Northseadl Date: Mon, 25 Sep 2023 02:03:13 +0800 Subject: [PATCH] optimize plugin connect --- cmd/devctl/plugin/gen.go | 2 +- cmd/server/powerx.go | 2 +- internal/logic/registerpluginlogic.go | 5 ++--- pkg/pluginx/loader.go | 9 +++++---- pkg/pluginx/manager.go | 7 ++++++- pkg/pluginx/plugin.go | 6 ++++-- pkg/pluginx/utils.go | 3 --- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/devctl/plugin/gen.go b/cmd/devctl/plugin/gen.go index 12876613..43f49946 100644 --- a/cmd/devctl/plugin/gen.go +++ b/cmd/devctl/plugin/gen.go @@ -196,7 +196,7 @@ func FormatPath(path string, handler string, pathParamsMap map[string]StringMap) var values []string if params, ok := pathParamsMap[handler]; ok { for key, value := range params { - path = strings.Replace(path, ":"+key, "%s", -1) + path = strings.Replace(path, ":"+key, "%v", -1) values = append(values, "req."+value) } } diff --git a/cmd/server/powerx.go b/cmd/server/powerx.go index 23ae1ed3..bb53dd72 100644 --- a/cmd/server/powerx.go +++ b/cmd/server/powerx.go @@ -35,7 +35,7 @@ func main() { server := rest.MustNewServer(c.Server, rest.WithRouter(r)) defer server.Stop() - plugin := pluginx.NewManager(context.Background(), r, fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)) + plugin := pluginx.NewManager(context.Background(), r, fmt.Sprintf("%s:%d", "127.0.0.1", c.Server.Port)) ctx := svc.NewServiceContext(c, svc.WithPlugin(plugin)) go func() { diff --git a/internal/logic/registerpluginlogic.go b/internal/logic/registerpluginlogic.go index 11984aa2..c35a6d00 100644 --- a/internal/logic/registerpluginlogic.go +++ b/internal/logic/registerpluginlogic.go @@ -5,8 +5,6 @@ import ( "PowerX/internal/types" "PowerX/pkg/pluginx" "context" - "fmt" - "github.com/zeromicro/go-zero/core/logx" ) @@ -32,11 +30,12 @@ func (l *RegisterPluginLogic) RegisterPlugin(req *types.RegisterPluginRequest) ( Path: route.Path, }) } - fmt.Println(req) + l.Infof("plugin register request: %v", req) err = l.svcCtx.Plugin.Register(req.Name, req.Addr, routes) if err != nil { return nil, err } + l.Infof("plugin %s registered", req.Name) return } diff --git a/pkg/pluginx/loader.go b/pkg/pluginx/loader.go index 94a92440..693d4365 100644 --- a/pkg/pluginx/loader.go +++ b/pkg/pluginx/loader.go @@ -355,8 +355,9 @@ func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData, } for _, entry := range entries { // 如果发现有不以 name 开头的文件, 则抛出错误 - if !strings.HasPrefix(entry.Name(), info.Name) { - return nil, fmt.Errorf("plugin %s api file name must start with %s", info.Name, entry.Name()) + lowerName := strings.ToLower(info.Name) + if !strings.HasPrefix(entry.Name(), lowerName) { + return nil, fmt.Errorf("plugin %s api file name must start with %s", info.Name, lowerName) } // 文件类型复制到 src/api 目录下 if !entry.IsDir() { @@ -442,8 +443,8 @@ func (l *Loader) UnArchives() error { continue } logger.Info(fmt.Sprintf("unarchive plugin: %s", entry.Name())) - err := UnzipFile(filepath.Join(archiveDir, entry.Name()), l.workdir) - if err != nil { + fileNameWithoutExt := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name())) + if err := UnzipFile(filepath.Join(archiveDir, entry.Name()), filepath.Join(l.workdir, strings.ToLower(fileNameWithoutExt))); err != nil { return err } logger.Info(fmt.Sprintf("unarchive plugin: %s done", entry.Name())) diff --git a/pkg/pluginx/manager.go b/pkg/pluginx/manager.go index b5fb076b..7d4dfcf1 100644 --- a/pkg/pluginx/manager.go +++ b/pkg/pluginx/manager.go @@ -319,7 +319,10 @@ func (m *Manager) Start() { // 遍历启动插件 for _, plugin := range m.pluginList { go func() { - plugin.Start() + logger.Info(fmt.Sprintf("starting plugin: %s", plugin.Name)) + if err := plugin.Start(); err != nil { + return + } }() } @@ -328,6 +331,7 @@ func (m *Manager) Start() { return e.Enable }) { go func() { + logger.Info("serving plugin frontend") err := m.frontendServer.Serve("./plugins/.plugin/dist", "5717") if err != nil { logger.Error("serving plugin frontend failed: ", err) @@ -351,6 +355,7 @@ 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 diff --git a/pkg/pluginx/plugin.go b/pkg/pluginx/plugin.go index a52a6323..3de019aa 100644 --- a/pkg/pluginx/plugin.go +++ b/pkg/pluginx/plugin.go @@ -1,5 +1,7 @@ package pluginx +import "strings" + type Plugin struct { BuildPluginItem Etc PluginEtcMap @@ -42,7 +44,7 @@ func LoadPlugin(m *Manager, name string) *Plugin { } plugin.backendServer = NewPluginBackendServer(plugin) m.pluginList = append(m.pluginList, plugin) - m.pluginMap[name] = plugin + m.pluginMap[strings.ToLower(name)] = plugin return plugin } @@ -52,7 +54,7 @@ func (p *Plugin) WithRoutes(routes []BackendRoute) *Plugin { } func (p *Plugin) Start() error { - if p.Enable { + if p.IsReady() { return nil } err := p.backendServer.Serve() diff --git a/pkg/pluginx/utils.go b/pkg/pluginx/utils.go index d8799011..919b2fb6 100644 --- a/pkg/pluginx/utils.go +++ b/pkg/pluginx/utils.go @@ -39,14 +39,12 @@ func DownloadFile(url string, dir string, filename string) error { // UnzipFile 解压zip文件到指定目录 func UnzipFile(zipPath string, dest string) error { - // 打开zip文件 reader, err := zip.OpenReader(zipPath) if err != nil { return err } defer reader.Close() - // 遍历zip文件中的每个文件和目录 for _, file := range reader.File { // 构建目标文件路径 destPath := filepath.Join(dest, file.Name) @@ -58,7 +56,6 @@ func UnzipFile(zipPath string, dest string) error { return err } } else { - // 如果是文件,则解压文件 err = os.MkdirAll(filepath.Dir(destPath), 0755) if err != nil { return err