From 0eb558787ffb3f85adb0ac8957581da4b9b2e36a Mon Sep 17 00:00:00 2001 From: chenjiekun Date: Tue, 28 Jun 2022 18:21:44 +0800 Subject: [PATCH 1/3] fix all CheckSkill of the output driver --- drivers/output/fileoutput/output.go | 3 ++- drivers/output/httpoutput/output.go | 3 ++- drivers/output/kafka/output.go | 3 ++- drivers/output/nsq/output.go | 3 ++- drivers/output/syslog/linux.go | 3 ++- output/output.go | 7 +++++++ 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/output/fileoutput/output.go b/drivers/output/fileoutput/output.go index 2375db0b..39523374 100644 --- a/drivers/output/fileoutput/output.go +++ b/drivers/output/fileoutput/output.go @@ -1,6 +1,7 @@ package fileoutput import ( + "github.com/eolinker/apinto/output" file_transport "github.com/eolinker/apinto/output/file-transport" "github.com/eolinker/eosc" "github.com/eolinker/eosc/formatter" @@ -72,5 +73,5 @@ func (a *FileOutput) Stop() error { } func (a *FileOutput) CheckSkill(skill string) bool { - return false + return output.CheckSkill(skill) } diff --git a/drivers/output/httpoutput/output.go b/drivers/output/httpoutput/output.go index 4836f8c8..aff71a45 100644 --- a/drivers/output/httpoutput/output.go +++ b/drivers/output/httpoutput/output.go @@ -1,6 +1,7 @@ package httpoutput import ( + "github.com/eolinker/apinto/output" http_transport "github.com/eolinker/apinto/output/http-transport" "github.com/eolinker/eosc" "github.com/eolinker/eosc/formatter" @@ -80,5 +81,5 @@ func (h *HttpOutput) Stop() error { } func (h *HttpOutput) CheckSkill(skill string) bool { - return false + return output.CheckSkill(skill) } diff --git a/drivers/output/kafka/output.go b/drivers/output/kafka/output.go index a4cbabbd..d53ea4a5 100644 --- a/drivers/output/kafka/output.go +++ b/drivers/output/kafka/output.go @@ -3,6 +3,7 @@ package kafka import ( "context" "github.com/Shopify/sarama" + "github.com/eolinker/apinto/output" "github.com/eolinker/eosc" "github.com/eolinker/eosc/formatter" "github.com/eolinker/eosc/log" @@ -85,7 +86,7 @@ func (o *Output) Stop() error { } func (o *Output) CheckSkill(skill string) bool { - return false + return output.CheckSkill(skill) } func (o *Output) write(msg *sarama.ProducerMessage) { diff --git a/drivers/output/nsq/output.go b/drivers/output/nsq/output.go index af6a1515..be83bff7 100644 --- a/drivers/output/nsq/output.go +++ b/drivers/output/nsq/output.go @@ -1,6 +1,7 @@ package nsq import ( + "github.com/eolinker/apinto/output" "github.com/eolinker/eosc" "github.com/eolinker/eosc/formatter" "sync" @@ -69,7 +70,7 @@ func (n *NsqOutput) Stop() error { } func (n *NsqOutput) CheckSkill(skill string) bool { - return false + return output.CheckSkill(skill) } func (n *NsqOutput) Output(entry eosc.IEntry) error { diff --git a/drivers/output/syslog/linux.go b/drivers/output/syslog/linux.go index 1b685843..8fe56e62 100644 --- a/drivers/output/syslog/linux.go +++ b/drivers/output/syslog/linux.go @@ -4,6 +4,7 @@ package syslog import ( + "github.com/eolinker/apinto/output" "github.com/eolinker/eosc" "github.com/eolinker/eosc/formatter" sys "log/syslog" @@ -87,7 +88,7 @@ func (s *SysWriter) Reset(conf interface{}, workers map[eosc.RequireId]interface } func (s *SysWriter) CheckSkill(skill string) bool { - return false + return output.CheckSkill(skill) } func newSysWriter(conf *Config, tag string) (*sys.Writer, error) { diff --git a/output/output.go b/output/output.go index 5056be9c..c3490686 100644 --- a/output/output.go +++ b/output/output.go @@ -2,6 +2,13 @@ package output import "github.com/eolinker/eosc" +const OutputSkill = "github.com/eolinker/apinto/http-entry.http-entry.IOutput" + type IEntryOutput interface { Output(entry eosc.IEntry) error } + +//CheckSkill 检查能力 +func CheckSkill(skill string) bool { + return skill == OutputSkill +} From 490b990247045dcd86d302802d387d04465c5165 Mon Sep 17 00:00:00 2001 From: chenjiekun Date: Thu, 30 Jun 2022 11:29:56 +0800 Subject: [PATCH 2/3] reset transport for cli log --- app/apinto/log.go | 16 ++++++++++++++++ app/apinto/main.go | 2 ++ 2 files changed, 18 insertions(+) create mode 100644 app/apinto/log.go diff --git a/app/apinto/log.go b/app/apinto/log.go new file mode 100644 index 00000000..d75ecf2d --- /dev/null +++ b/app/apinto/log.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/eolinker/eosc/log" + "os" +) + +func InitCLILog() { + formatter := &log.LineFormatter{ + TimestampFormat: "2006-01-02 15:04:05", + CallerPrettyfier: nil, + } + transport := log.NewTransport(os.Stdout, log.ErrorLevel) + transport.SetFormatter(formatter) + log.Reset(transport) +} diff --git a/app/apinto/main.go b/app/apinto/main.go index d947dac8..34f41ea4 100644 --- a/app/apinto/main.go +++ b/app/apinto/main.go @@ -29,6 +29,8 @@ func init() { process.Register(eosc.ProcessMaster, ProcessMaster) process.Register(eosc.ProcessHelper, ProcessHelper) process.Register(eosc.ProcessAdmin, ProcessAdmin) + + InitCLILog() } func main() { From de34f8ea420eb6989b83c06b3d43d363d758c2e2 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Fri, 1 Jul 2022 14:56:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=8F=92=E4=BB=B6value=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drivers/plugins/extra-params/config.go | 8 ++-- drivers/plugins/extra-params/extra-params.go | 13 ++++-- drivers/plugins/extra-params/util.go | 44 +++++++------------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/drivers/plugins/extra-params/config.go b/drivers/plugins/extra-params/config.go index 02bf9e24..30baca77 100644 --- a/drivers/plugins/extra-params/config.go +++ b/drivers/plugins/extra-params/config.go @@ -11,10 +11,10 @@ type Config struct { } type ExtraParam struct { - Name string `json:"name" label:"参数名"` - Position string `json:"position" enum:"header,query,body" label:"参数位置"` - Value interface{} `json:"value" label:"参数值"` - Conflict string `json:"conflict" label:"参数冲突时的处理方式" enum:"origin,convert,error"` + Name string `json:"name" label:"参数名"` + Position string `json:"position" enum:"header,query,body" label:"参数位置"` + Value string `json:"value" label:"参数值"` + Conflict string `json:"conflict" label:"参数冲突时的处理方式" enum:"origin,convert,error"` } func (c *Config) doCheck() error { diff --git a/drivers/plugins/extra-params/extra-params.go b/drivers/plugins/extra-params/extra-params.go index 8e74c4ce..6bbbf137 100644 --- a/drivers/plugins/extra-params/extra-params.go +++ b/drivers/plugins/extra-params/extra-params.go @@ -47,10 +47,16 @@ func (e *ExtraParams) access(ctx http_service.IHttpContext) (int, error) { headers := ctx.Proxy().Header().Headers() // 先判断参数类型 for _, param := range e.params { + var paramValue interface{} + err = json.Unmarshal([]byte(param.Value), ¶mValue) + if err != nil { + paramValue = param.Value + } switch param.Position { case "query": { - value, err := getQueryValue(ctx, param) + v, _ := json.Marshal(paramValue) + value, err := getQueryValue(ctx, param, string(v)) if err != nil { err = encodeErr(e.errorType, err.Error(), clientErrStatusCode) return clientErrStatusCode, err @@ -59,7 +65,8 @@ func (e *ExtraParams) access(ctx http_service.IHttpContext) (int, error) { } case "header": { - value, err := getHeaderValue(headers, param) + v, _ := json.Marshal(paramValue) + value, err := getHeaderValue(headers, param, string(v)) if err != nil { err = encodeErr(e.errorType, err.Error(), clientErrStatusCode) return clientErrStatusCode, err @@ -68,7 +75,7 @@ func (e *ExtraParams) access(ctx http_service.IHttpContext) (int, error) { } case "body": { - value, err := getBodyValue(bodyParams, formParams, param, contentType) + value, err := getBodyValue(bodyParams, formParams, param, contentType, paramValue) if err != nil { err = encodeErr(e.errorType, err.Error(), clientErrStatusCode) return clientErrStatusCode, err diff --git a/drivers/plugins/extra-params/util.go b/drivers/plugins/extra-params/util.go index bdd1b3ab..b1045ac3 100644 --- a/drivers/plugins/extra-params/util.go +++ b/drivers/plugins/extra-params/util.go @@ -60,12 +60,9 @@ func parseBodyParams(ctx http_service.IHttpContext, body []byte, contentType str return bodyParams, formParams, nil } -func getHeaderValue(headers map[string][]string, param *ExtraParam) (string, error) { +func getHeaderValue(headers map[string][]string, param *ExtraParam, value string) (string, error) { paramName := ConvertHeaderKey(param.Name) - if _, ok := param.Value.(string); !ok { - errInfo := "[extra_params] Header param " + param.Name + " must be a string" - return "", errors.New(errInfo) - } + if param.Conflict == "" { param.Conflict = paramConvert } @@ -79,12 +76,7 @@ func getHeaderValue(headers map[string][]string, param *ExtraParam) (string, err } if param.Conflict == paramConvert { - if value, ok := param.Value.(string); ok { - paramValue = value - } else { - errInfo := `[extra_params] Illegal "paramValue" in "` + param.Name + `"` - return "", errors.New(errInfo) - } + paramValue = value } else if param.Conflict == paramError { errInfo := `[extra_params] "` + param.Name + `" has a conflict.` return "", errors.New(errInfo) @@ -115,12 +107,8 @@ func hasQueryValue(rawQuery string, paramName string) bool { return false } -func getQueryValue(ctx http_service.IHttpContext, param *ExtraParam) (string, error) { - if _, ok := param.Value.(string); !ok { - errInfo := "[extra_params] Query param " + param.Name + " must be a string" - return "", errors.New(errInfo) - } - value := "" +func getQueryValue(ctx http_service.IHttpContext, param *ExtraParam, value string) (string, error) { + paramValue := "" if param.Conflict == "" { param.Conflict = paramConvert } @@ -129,49 +117,45 @@ func getQueryValue(ctx http_service.IHttpContext, param *ExtraParam) (string, er if !hasQueryValue(ctx.Proxy().URI().RawQuery(), param.Name) { param.Conflict = paramConvert } else { - value = ctx.Proxy().URI().GetQuery(param.Name) + paramValue = ctx.Proxy().URI().GetQuery(param.Name) } if param.Conflict == paramConvert { - value = param.Value.(string) + paramValue = value } else if param.Conflict == paramError { errInfo := `[extra_params] "` + param.Name + `" has a conflict.` return "", errors.New(errInfo) } - return value, nil + return paramValue, nil } -func getBodyValue(bodyParams map[string]interface{}, formParams map[string][]string, param *ExtraParam, contentType string) (interface{}, error) { - var value interface{} = nil +func getBodyValue(bodyParams map[string]interface{}, formParams map[string][]string, param *ExtraParam, contentType string, value interface{}) (interface{}, error) { + var paramValue interface{} = nil if param.Conflict == "" { param.Conflict = paramConvert } if strings.Contains(contentType, FormParamType) { - if _, ok := param.Value.(string); !ok { - errInfo := "[extra_params] Body param " + param.Name + " must be a string" - return "", errors.New(errInfo) - } if _, ok := formParams[param.Name]; !ok { param.Conflict = paramConvert } else { - value = formParams[param.Name][0] + paramValue = formParams[param.Name][0] } } else if strings.Contains(contentType, JsonType) { if _, ok := bodyParams[param.Name]; !ok { param.Conflict = paramConvert } else { - value = bodyParams[param.Name] + paramValue = bodyParams[param.Name] } } if param.Conflict == paramConvert { - value = param.Value + paramValue = value } else if param.Conflict == paramError { errInfo := `[extra_params] "` + param.Name + `" has a conflict.` return "", errors.New(errInfo) } - return value, nil + return paramValue, nil } func ConvertHeaderKey(header string) string {