Skip to content

Commit

Permalink
optimize plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
northseadl committed Oct 7, 2023
1 parent 3aa5f85 commit 97589b0
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/pluginx/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func ginCORS() gin.HandlerFunc {
method := c.Request.Method
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Allow-Methods", "*")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")

Expand Down
44 changes: 31 additions & 13 deletions pkg/pluginx/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pluginx
import (
"fmt"
"gopkg.in/yaml.v3"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -160,14 +161,15 @@ func (l *Loader) BuildPluginFrontend(opts BuildPluginFrontendOptions) error {
return err
}

// 将插件配置写入 .config/etc.yaml, 如果已存在则使用已存在的配置
etcFile, err := os.Create(filepath.Join(cacheDir, "etc.yaml"))
etcFilePath := filepath.Join(cacheDir, "etc.yaml")
etcFile, err := os.OpenFile(etcFilePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer etcFile.Close()

// 解析已存在的 etc.yaml
etcFileContent, err := os.ReadFile(filepath.Join(cacheDir, "etc.yaml"))
etcFileContent, err := io.ReadAll(etcFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -197,6 +199,12 @@ func (l *Loader) BuildPluginFrontend(opts BuildPluginFrontendOptions) error {
if err != nil {
return err
}
if err = etcFile.Truncate(0); err != nil {
return err
}
if _, err = etcFile.Seek(0, 0); err != nil {
return err
}
_, err = etcFile.Write(etcContent)

// 将插件前端配置写入 .config/frontend.yaml
Expand Down Expand Up @@ -254,8 +262,8 @@ func cleanFrontend(dir string) error {

func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData, err error) {
var buildInfos []BuildPluginItem
var PluginConfig PluginManagerEtc
var PluginFrontendConfig PluginFrontendInfo
var pluginManagerConfig PluginManagerEtc
var pluginFrontendConfig PluginFrontendInfo
var mergedNames []string

// 获取目录下的所有不以.开头的一级目录
Expand Down Expand Up @@ -302,21 +310,19 @@ func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData,
Enable: true,
Etc: make(PluginEtcMap),
}
etcRawWrapper := PluginEtcItemPluginWrapper{
Plugin: etc.Etc,
}
etcPath := filepath.Join(pluginDir, entry.Name(), "etc.yaml")
if _, err := os.Stat(etcPath); !os.IsNotExist(err) {
etcFile, err := os.ReadFile(etcPath)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(etcFile, &etcRawWrapper)
err = yaml.Unmarshal(etcFile, &etc.Etc)
fmt.Println(etc.Etc)
if err != nil {
return nil, err
}
}
PluginConfig.Plugins = append(PluginConfig.Plugins, etc)
pluginManagerConfig.Plugins = append(pluginManagerConfig.Plugins, etc)

// 解析目录下的 route.json 文件, 如果不存在返回错误
routePath := filepath.Join(pluginDir, entry.Name(), "route.json")
Expand All @@ -332,7 +338,7 @@ func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData,
if err != nil {
return nil, err
}
PluginFrontendConfig.Routes = append(PluginFrontendConfig.Routes, route)
pluginFrontendConfig.Routes = append(pluginFrontendConfig.Routes, route)

fmt.Println(info.Name)

Expand Down Expand Up @@ -369,6 +375,18 @@ func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData,
}
}

// 复制 frontend/assets/images/* 到 src/assets/images
imagesSrcDir := filepath.Join(pluginDir, entry.Name(), "frontend/assets/images")
imagesDestDir := filepath.Join(frontDir, "src/assets/images")
if _, err := os.Stat(imagesSrcDir); !os.IsNotExist(err) {
err = CopyDir(imagesSrcDir, imagesDestDir)
if err != nil {
return nil, err
}
}

// todo assert文件夹替换资源文件路径

logger.Info(fmt.Sprintf("merge plugin files: %s done", entry.Name()))

// 将插件名存入 mergedNames
Expand All @@ -378,8 +396,8 @@ func mergePluginFiles(pluginDir string, frontDir string) (buildFiles *BuildData,
}
return &BuildData{
Build: BuildInfo{Plugins: buildInfos, BuildDate: time.Now().Format("2006-01-02 15:04:05")},
Etc: PluginConfig,
Frontend: PluginFrontendConfig,
Etc: pluginManagerConfig,
Frontend: pluginFrontendConfig,
}, nil
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/pluginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ func (m *Manager) ProxyHandleFunc() http.HandlerFunc {

copyHeaders(request.Header, proxyReq.Header)

fmt.Println(proxyReq.Header)

resp, err := http.DefaultClient.Do(proxyReq)
if err != nil {
logx.Errorf("Error sending proxy request: %v", err)
Expand Down Expand Up @@ -320,10 +318,11 @@ func (m *Manager) Start() {
}

// 遍历启动插件
for _, plugin := range m.pluginList {
for _, p := range m.pluginList {
p := p
go func() {
logger.Info(fmt.Sprintf("starting plugin: %s", plugin.Name))
if err := plugin.Start(); err != nil {
logger.Info(fmt.Sprintf("starting plugin: %s", p.Name))
if err := p.Start(); err != nil {
return
}
}()
Expand Down Expand Up @@ -385,8 +384,10 @@ func (m *Manager) List() []*Plugin {
func (m *Manager) ListFrontendRoutes() []PluginFrontendRoute {
var routes []PluginFrontendRoute
for _, p := range m.List() {
if p.IsReady() {
routes = append(routes, p.m.frontendInfo.Routes...)
for _, route := range m.frontendInfo.Routes {
if p.IsReady() && p.Name == route.Name {
routes = append(routes, route)
}
}
}
return routes
Expand Down
4 changes: 3 additions & 1 deletion pkg/pluginx/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pluginx

import "strings"
import (
"strings"
)

type Plugin struct {
BuildPluginItem
Expand Down
2 changes: 1 addition & 1 deletion pkg/pluginx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type PluginEtc struct {
Enable bool `yaml:"enable"`
Etc PluginEtcMap `yaml:"etc"`
}
type PluginEtcMap map[string]any
type PluginEtcMap = map[string]any

type PluginEtcItemPluginWrapper struct {
Plugin PluginEtcMap `yaml:"plugin"`
Expand Down
75 changes: 75 additions & 0 deletions pkg/pluginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package pluginx

import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -125,6 +127,79 @@ func CopyAndRenameDir(src string, dest string) error {
return err
}

func CopyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()

dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()

_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}

srcInfo, err := os.Stat(src)
if err != nil {
return err
}

err = os.Chmod(dst, srcInfo.Mode())
if err != nil {
return err
}

return nil
}

func CopyDir(src, dst string) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}

if !srcInfo.IsDir() {
return fmt.Errorf("源路径不是一个目录: %s", src)
}

err = os.MkdirAll(dst, srcInfo.Mode())
if err != nil {
return err
}

entries, err := ioutil.ReadDir(src)
if err != nil {
return err
}

for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())

if entry.IsDir() {
err = CopyDir(srcPath, dstPath)
if err != nil {
return err
}
} else {
err = CopyFile(srcPath, dstPath)
if err != nil {
return err
}
}
}

return nil
}

// 复制目录下所有内容到目标目录

// ReplaceFileString 替换文件中的字符串为指定字符串, 次数为-1时替换所有
func ReplaceFileString(filePath string, old string, new string, n int) error {
// 读取文件内容
Expand Down

0 comments on commit 97589b0

Please sign in to comment.