Merge pull request !77 from bandl/feat-pporf-plugins
This commit is contained in:
bandl 2021-10-25 07:49:21 +00:00 committed by Gitee
commit 2a69f393e5
5 changed files with 42 additions and 16 deletions

View File

@ -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() {

View File

@ -48,4 +48,6 @@ gateway:
host: '127.0.0.1'
port: 5891
target: ["127.0.0.1:5890"]
mock-plugin:
pprof-addr: "127.0.0.1:8000"

View File

@ -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,
}
}

View File

@ -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%}
}

View File

@ -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) {