Skip to content

Commit

Permalink
optimize plugin connect
Browse files Browse the repository at this point in the history
  • Loading branch information
northseadl committed Sep 24, 2023
1 parent 54e2250 commit 4161e7c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/devctl/plugin/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/powerx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 2 additions & 3 deletions internal/logic/registerpluginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"PowerX/internal/types"
"PowerX/pkg/pluginx"
"context"
"fmt"

"github.com/zeromicro/go-zero/core/logx"
)

Expand All @@ -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
}
9 changes: 5 additions & 4 deletions pkg/pluginx/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()))
Expand Down
7 changes: 6 additions & 1 deletion pkg/pluginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/pluginx/plugin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pluginx

import "strings"

type Plugin struct {
BuildPluginItem
Etc PluginEtcMap
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions pkg/pluginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4161e7c

Please sign in to comment.