Skip to content

Commit

Permalink
Merge pull request #59 from eolinker/feature/fix-extra_param
Browse files Browse the repository at this point in the history
Feature/fix extra param
  • Loading branch information
Dot-Liu authored Jul 1, 2022
2 parents c92d4f5 + de34f8e commit 6667959
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 42 deletions.
16 changes: 16 additions & 0 deletions app/apinto/log.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions app/apinto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func init() {
process.Register(eosc.ProcessMaster, ProcessMaster)
process.Register(eosc.ProcessHelper, ProcessHelper)
process.Register(eosc.ProcessAdmin, ProcessAdmin)

InitCLILog()
}

func main() {
Expand Down
3 changes: 2 additions & 1 deletion drivers/output/fileoutput/output.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -72,5 +73,5 @@ func (a *FileOutput) Stop() error {
}

func (a *FileOutput) CheckSkill(skill string) bool {
return false
return output.CheckSkill(skill)
}
3 changes: 2 additions & 1 deletion drivers/output/httpoutput/output.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -80,5 +81,5 @@ func (h *HttpOutput) Stop() error {
}

func (h *HttpOutput) CheckSkill(skill string) bool {
return false
return output.CheckSkill(skill)
}
3 changes: 2 additions & 1 deletion drivers/output/kafka/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion drivers/output/nsq/output.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nsq

import (
"github.com/eolinker/apinto/output"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/formatter"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion drivers/output/syslog/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package syslog

import (
"github.com/eolinker/apinto/output"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/formatter"
sys "log/syslog"
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions drivers/plugins/extra-params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions drivers/plugins/extra-params/extra-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), &paramValue)
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
Expand All @@ -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
Expand All @@ -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
Expand Down
44 changes: 14 additions & 30 deletions drivers/plugins/extra-params/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6667959

Please sign in to comment.