diff --git a/drivers/router/http-router/config.go b/drivers/router/http-router/config.go index 3f7c5da2..f9349675 100644 --- a/drivers/router/http-router/config.go +++ b/drivers/router/http-router/config.go @@ -9,13 +9,13 @@ import ( //DriverConfig http路由驱动配置 type DriverConfig struct { - Listen int `json:"listen" yaml:"listen" title:"port" description:"使用端口" default:"80" label:"端口号" maximum:"65535"` - Method []string `json:"method" yaml:"method" enum:"GET,POST,PUT,DELETE,PATH,HEAD,OPTIONS" label:"请求方式"` - Host []string `json:"host" yaml:"host" label:"域名"` - Rules []DriverRule `json:"rules" yaml:"rules" label:"路由规则"` - Target eosc.RequireId `json:"target" yaml:"target" skill:"github.com/eolinker/apinto/service.service.IService" required:"true" label:"目标服务"` - Disable bool `json:"disable" yaml:"disable" label:"禁用路由"` - Plugins map[string]*plugin.Config `json:"plugins" yaml:"plugins" label:"插件配置"` + Listen int `json:"listen" yaml:"listen" title:"port" description:"使用端口" default:"80" label:"端口号" maximum:"65535"` + Method []string `json:"method" yaml:"method" enum:"GET,POST,PUT,DELETE,PATH,HEAD,OPTIONS" label:"请求方式"` + Host []string `json:"host" yaml:"host" label:"域名"` + Rules []DriverRule `json:"rules" yaml:"rules" label:"路由规则"` + Target eosc.RequireId `json:"target" yaml:"target" skill:"github.com/eolinker/apinto/service.service.IService" required:"true" label:"目标服务"` + Disable bool `json:"disable" yaml:"disable" label:"禁用路由"` + Plugins plugin.Plugins `json:"plugins" yaml:"plugins" label:"插件配置"` } //DriverRule http路由驱动配置Rule结构体 diff --git a/drivers/service/service-http/config.go b/drivers/service/service-http/config.go index 45ae9198..7e388e79 100644 --- a/drivers/service/service-http/config.go +++ b/drivers/service/service-http/config.go @@ -11,14 +11,14 @@ import ( //Config service_http驱动配置 type Config struct { - Timeout int64 `json:"timeout" label:"请求超时时间" default:"2000" minimum:"1" description:"单位:ms,最小值:1"` - Retry int `json:"retry" label:"失败重试次数"` - Scheme string `json:"scheme" label:"请求协议" enum:"HTTP,HTTPS"` - Discovery eosc.RequireId `json:"discovery" required:"false" empty_label:"使用匿名上游" label:"服务发现" skill:"github.com/eolinker/apinto/discovery.discovery.IDiscovery"` - Service string `json:"service" required:"false" label:"服务名 or 配置" switch:"discovery !==''"` - Nodes []string `json:"nodes" label:"静态配置" switch:"discovery===''"` - Balance string `json:"balance" enum:"round-robin" label:"负载均衡算法"` - PluginConfig map[string]*plugin.Config `json:"plugins" label:"插件"` + Timeout int64 `json:"timeout" label:"请求超时时间" default:"2000" minimum:"1" description:"单位:ms,最小值:1"` + Retry int `json:"retry" label:"失败重试次数"` + Scheme string `json:"scheme" label:"请求协议" enum:"HTTP,HTTPS"` + Discovery eosc.RequireId `json:"discovery" required:"false" empty_label:"使用匿名上游" label:"服务发现" skill:"github.com/eolinker/apinto/discovery.discovery.IDiscovery"` + Service string `json:"service" required:"false" label:"服务名 or 配置" switch:"discovery !==''"` + Nodes []string `json:"nodes" label:"静态配置" switch:"discovery===''"` + Balance string `json:"balance" enum:"round-robin" label:"负载均衡算法"` + Plugins plugin.Plugins `json:"plugins" label:"插件"` } func (c *Config) String() string { diff --git a/drivers/service/service-http/worker.go b/drivers/service/service-http/worker.go index d272ab17..7135c98b 100644 --- a/drivers/service/service-http/worker.go +++ b/drivers/service/service-http/worker.go @@ -83,8 +83,8 @@ func (s *serviceWorker) Reset(conf interface{}, workers map[eosc.RequireId]inter s.Service.retry = data.Retry - log.Debug("reset service:", data.PluginConfig) - s.Service.reset(data.Scheme, apps, balanceHandler, data.PluginConfig) + log.Debug("reset service:", data.Plugins) + s.Service.reset(data.Scheme, apps, balanceHandler, data.Plugins) return nil diff --git a/go.mod b/go.mod index 4e4783f6..755ac54a 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/coreos/go-semver v0.3.0 // indirect + github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.0 // indirect @@ -29,10 +30,15 @@ require ( github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect github.com/eapache/queue v1.1.0 // indirect github.com/fatih/color v1.9.0 // indirect + github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.4 // indirect + github.com/google/btree v1.0.1 // indirect + github.com/google/uuid v1.1.2 // indirect + github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect + github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect github.com/hashicorp/go-hclog v0.12.0 // indirect github.com/hashicorp/go-immutable-radix v1.0.0 // indirect @@ -45,6 +51,8 @@ require ( github.com/jcmturner/gofork v1.0.0 // indirect github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect github.com/jcmturner/rpc/v2 v2.0.3 // indirect + github.com/jonboulle/clockwork v0.2.2 // indirect + github.com/json-iterator/go v1.1.11 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/klauspost/compress v1.14.4 // indirect github.com/kr/fs v0.1.0 // indirect @@ -53,8 +61,10 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect github.com/pierrec/lz4 v2.6.1+incompatible // indirect - github.com/prometheus/client_golang v1.11.0 // indirect + github.com/prometheus/client_golang v1.11.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.26.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect @@ -64,11 +74,19 @@ require ( github.com/urfave/cli/v2 v2.3.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect - go.etcd.io/etcd/api/v3 v3.5.1 // indirect - go.etcd.io/etcd/client/pkg/v3 v3.5.1 // indirect - go.etcd.io/etcd/pkg/v3 v3.5.1 // indirect - go.etcd.io/etcd/raft/v3 v3.5.1 // indirect - go.etcd.io/etcd/server/v3 v3.5.1 // indirect + go.etcd.io/bbolt v1.3.6 // indirect + go.etcd.io/etcd/api/v3 v3.5.4 // indirect + go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect + go.etcd.io/etcd/client/v2 v2.305.4 // indirect + go.etcd.io/etcd/client/v3 v3.5.4 // indirect + go.etcd.io/etcd/pkg/v3 v3.5.4 // indirect + go.etcd.io/etcd/raft/v3 v3.5.4 // indirect + go.etcd.io/etcd/server/v3 v3.5.4 // indirect + go.opentelemetry.io/contrib v0.20.0 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0 // indirect + go.opentelemetry.io/otel v0.20.0 // indirect + go.opentelemetry.io/otel/metric v0.20.0 // indirect + go.opentelemetry.io/otel/trace v0.20.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.19.1 // indirect diff --git a/plugin-manager/config.go b/plugin-manager/config.go index c6ac8377..07bcbc8f 100644 --- a/plugin-manager/config.go +++ b/plugin-manager/config.go @@ -1,5 +1,9 @@ package plugin_manager +import ( + "reflect" +) + const ( StatusDisable = "disable" StatusEnable = "enable" @@ -18,3 +22,7 @@ type PluginConfig struct { Config interface{} `json:"config" yaml:"config"` InitConfig map[string]interface{} `json:"init_config" yaml:"init_config"` } + +func (p *PluginConfig) Reset(originVal reflect.Value, targetVal reflect.Value, params map[string]string) error { + return nil +} diff --git a/plugin/plugins.go b/plugin/plugins.go new file mode 100644 index 00000000..bc664777 --- /dev/null +++ b/plugin/plugins.go @@ -0,0 +1,89 @@ +package plugin + +import ( + "errors" + "github.com/eolinker/eosc/variable" + "reflect" +) + +var ( + ErrorVariableNotFound = errors.New("variable not found") + ErrorUnsupportedKind = errors.New("unsupported kind") +) + +type Plugins map[string]*Config + +func (p Plugins) Reset(originVal reflect.Value, targetVal reflect.Value, params map[string]string) error { + //if originVal.Kind() != reflect.Map { + // return fmt.Errorf("plugin map reset error:%w %s", ErrorUnsupportedKind, originVal.Kind()) + //} + //targetType := targetVal.Type() + //newMap := reflect.MakeMap(targetType) + //for _, key := range originVal.MapKeys() { + // // 判断是否存在对应的插件配置 + // cfgType, ok := typeMap[key.String()] + // if !ok { + // return fmt.Errorf("plugin %s not found", key.String()) + // } + // value := originVal.MapIndex(key) + // newValue := reflect.New(targetType.Elem()) + // + // err := pluginConfigSet(value, newValue, params, cfgType) + // if err != nil { + // return err + // } + // newMap.SetMapIndex(key, newValue.Elem()) + //} + //targetVal.Set(newMap) + return nil +} + +func pluginConfigSet(originVal reflect.Value, targetVal reflect.Value, params map[string]string, cfgType reflect.Type) error { + if targetVal.Kind() == reflect.Ptr { + if !targetVal.Elem().IsValid() { + targetType := targetVal.Type() + newVal := reflect.New(targetType.Elem()) + targetVal.Set(newVal) + } + targetVal = targetVal.Elem() + } + switch targetVal.Kind() { + case reflect.Struct: + { + targetType := targetVal.Type() + for i := 0; i < targetType.NumField(); i++ { + field := targetType.Field(i) + var fieldValue reflect.Value + switch field.Type.Kind() { + case reflect.Interface: + if cfgType.Kind() == reflect.Ptr { + cfgType = cfgType.Elem() + } + fieldValue = reflect.New(cfgType) + default: + fieldValue = reflect.New(field.Type) + } + + var value reflect.Value + switch originVal.Elem().Kind() { + case reflect.Map: + { + tag := field.Tag.Get("json") + value = originVal.Elem().MapIndex(reflect.ValueOf(tag)) + } + default: + value = originVal.Elem() + } + + err := variable.RecurseReflect(value, fieldValue, params) + if err != nil { + return err + } + targetVal.Field(i).Set(fieldValue.Elem()) + } + } + case reflect.Ptr: + return pluginConfigSet(originVal, targetVal, params, cfgType) + } + return nil +}