From 19562221f9bab4950b8430c08f702de71ed70531 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 25 Oct 2021 15:23:36 +0800 Subject: [PATCH 1/4] chore(pluigin): init pluugins --- plugins/config/middle.gen.go | 3 ++- plugins/config/middle.template | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/config/middle.gen.go b/plugins/config/middle.gen.go index 8030d69..c989fd7 100644 --- a/plugins/config/middle.gen.go +++ b/plugins/config/middle.gen.go @@ -12,8 +12,9 @@ import ( func GetMiddlewareMap() map[string]plugins.PluginInterface { mockPlugin := mockPlugin.NewPlugin() - return map[string]plugins.PluginInterface{ + mockPlugin.Init() + return map[string]plugins.PluginInterface{ mockPlugin.Name(): mockPlugin, } } diff --git a/plugins/config/middle.template b/plugins/config/middle.template index a11e1d6..b90bd12 100644 --- a/plugins/config/middle.template +++ b/plugins/config/middle.template @@ -14,9 +14,10 @@ import ( func GetMiddlewareMap() map[string]plugins.PluginInterface { {%for dir in dirs %} {{dir[0]}}:={{dir[0]}}.NewPlugin() - {%- endfor%} + {{dir[0]}}.Init() + {% endfor%} return map[string]plugins.PluginInterface{ - {%for dir in dirs %} + {%for dir in dirs -%} {{dir[0]}}.Name():{{dir[0]}}, {%- endfor%} } From 06042b778cb56df1cd0239bab12b640acf98e57e Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 25 Oct 2021 15:34:23 +0800 Subject: [PATCH 2/4] feat(conf): ones load conf --- conf/public_conf.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/conf/public_conf.go b/conf/public_conf.go index 533b64a..d5f0791 100644 --- a/conf/public_conf.go +++ b/conf/public_conf.go @@ -2,6 +2,7 @@ package conf import ( "log" + "sync" "github.com/spf13/viper" ) @@ -10,18 +11,23 @@ const ( linuxPath = "/etc/wheat-cache/" ) -func init() { - setDefaultConfValue() - err := LoadConf("") - switch err.(type) { - case nil: - case viper.ConfigFileNotFoundError: - formatPath := []string{linuxPath} - log.Fatalf("the profile could not be read, read path:%v", formatPath) - default: - log.Fatalf("the resolution of the profile failed, err: %v", err) +var confLock sync.Once - } +func init() { + confLock.Do(func() { + setDefaultConfValue() + err := LoadConf("") + switch err.(type) { + case nil: + case viper.ConfigFileNotFoundError: + formatPath := []string{linuxPath} + log.Fatalf("the profile could not be read, read path:%v", formatPath) + default: + log.Fatalf("the resolution of the profile failed, err: %v", err) + + } + }, + ) } func setDefaultConfValue() { From d78cb874ed99a08a57d5fc45c67144ec0e632c02 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 25 Oct 2021 15:47:21 +0800 Subject: [PATCH 3/4] feat(conf): add conf --- conf/wheat-cache.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml index d0a7950..9d89429 100644 --- a/conf/wheat-cache.yaml +++ b/conf/wheat-cache.yaml @@ -48,4 +48,6 @@ gateway: host: '127.0.0.1' port: 5891 target: ["127.0.0.1:5890"] - \ No newline at end of file + +mock-plugin: + pprof-addr: "127.0.0.1:8000" \ No newline at end of file From 417ddf1ccff7f11e520a80516a3c2e44e701eb96 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 25 Oct 2021 15:48:21 +0800 Subject: [PATCH 4/4] feat(plugins): add plugins --- plugins/mock-plugin/mock.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/plugins/mock-plugin/mock.go b/plugins/mock-plugin/mock.go index 0c87df2..07ad8e5 100644 --- a/plugins/mock-plugin/mock.go +++ b/plugins/mock-plugin/mock.go @@ -1,11 +1,27 @@ package mockplugin -import "fmt" +import ( + "fmt" + "net/http" + _ "net/http/pprof" + + _ "gitee.com/timedb/wheatCache/conf" + "gitee.com/timedb/wheatCache/pkg/logx" + "github.com/spf13/viper" +) type MockPlugin struct { } func (m *MockPlugin) Init() { + addr := viper.GetString("mock-plugin.pprof-addr") + if addr != "" { + go func() { + logx.Debugln(http.ListenAndServe(addr, nil)) + }() + } + + logx.Debug("mock-plugins status: start") } func (m *MockPlugin) Exec(msg interface{}) (interface{}, error) {