response

package module
v0.0.0-...-5c29987 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 18 Imported by: 0

README

Response Transformation Plugin

When forwarding requests, the response headers and parameters of the HTTP response can be modified, added, or deleted through plugins. This can avoid the inefficient work of wrapping interfaces.

Note the performance impact when modifying the response body, as it requires parsing and then filling it back.

Usage Instructions

Import the Plugin in the main.go file of the Gateway Project
  • Add the import statement
import (
_ "trpc.group/trpc-go/trpc-gateway/plugin/transformer/response"
)
  • Configure the tRPC framework in the configuration file to enable the response transformer interceptor.

Note: Make sure to register it in server.service.filter and not in server.filter.

global:                             # Global configuration
server: # Server configuration
  filter:                          # Interceptor list for all service handler functions
  service: # Business services provided, can have multiple
    - name: trpc.inews.trpc.gateway      # Route name of the service
      filter:
        - response_transformer # Register the gateway plugin in the service filter so that it can be dynamically loaded in router.yaml
plugins: # Plugin configuration
  log:                              # Log configuration
  gateway: # Plugin type is gateway
    request_transformer:           # Cross-origin plugin name

Configure the plugin in the gateway routing configuration file router.yaml. It also supports global, service, and router-level plugin configurations.

router: # Router configuration
  - method: /v1/user/info
    target_service:
      - service: trpc.user.service
    plugins:
      - name: response_transformer
        props:
          # Remove headers
          remove_headers:
            - keys:
                # Headers to remove
                - header_to_remove
              status_codes:
                # Limit by HTTP status code
                - 401
              trpc_codes:
                # Limit by tRPC code
                - 5000
          # Remove JSON keys
          remove_json:
            - keys:
                # Supports multi-level removal, e.g., common.suid
                - json_key_to_remove
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Rename headers (renamed headers will not be formatted)
          rename_headers:
            - keys:
                # Rename header
                - old_name:new_name
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Rename JSON keys
          rename_json:
            - keys:
                # Supports hierarchical operations, e.g., common.suid:common.suidv2
                - old_key:new_key
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Add headers
          add_headers:
            - keys:
                - header_key:val
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Add JSON keys
          add_json:
            - keys:
                # Supports hierarchical operations, supports specifying types, e.g., number, string, bool
                - key:true:bool
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Replace headers
          replace_headers:
            - keys:
                # Replace if exists, skip if not
                - new_key:val
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Replace JSON keys
          replace_json:
            - keys:
                # Supports hierarchical operations, supports specifying types
                - key:123:number
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Append headers
          append_headers:
            - keys:
                # Append if exists, skip if not
                - k:v
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Append JSON keys
          append_json:
            - keys:
                # Append if exists, skip if not; supports hierarchical operations, supports specifying types
                - k:v:string
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Replace JSON body
          replace_body:
            - keys:
                # Replace JSON body
                - '{"code":401}'
              status_codes:
                - 401
              trpc_codes:
                - 5000
          # Limit the returned keys
          allow_json:
            - keys:
                # Supports hierarchical operations
                - code
                - msg
                - data
              status_codes:
                - 401
              trpc_codes:
                - 5000
client: # Upstream service configuration, consistent with the tRPC protocol
  - name: trpc.user.service
    plugins:
      - name: request_transformer # Service-level configuration, will take effect for all interfaces forwarded to this service
        props:
plugins:
  - name: request_transformer # Global configuration, will take effect for all interfaces
    props:

Console Usage

This plugin can also be used through the Gateway Console. img.png

Documentation

Overview

Package response response transformer plugin

Index

Constants

View Source
const (
	// PluginName response transformer
	PluginName = "response_transformer"

	// ErrInvalidJSONType invalid value type
	ErrInvalidJSONType = 10002
)

Variables

This section is empty.

Functions

func ServerFilter

func ServerFilter(ctx context.Context, req interface{}, handler filter.ServerHandleFunc) (interface{}, error)

ServerFilter sets server-side CORS verification

Types

type KV

type KV struct {
	Key string
	Val string
	// ConvertedVal for Val
	ConvertedVal interface{}
}

KV key-value structure

type KeysConfig

type KeysConfig struct {
	Keys          []string                        `yaml:"keys,omitempty" json:"keys,omitempty"`
	KVs           []*KV                           `yaml:"-"`
	StatusCodes   []int                           `yaml:"status_codes,omitempty" json:"status_codes,omitempty"`
	StatusCodeMap map[int]struct{}                `yaml:"-"`
	TRPCCodes     []trpcpb.TrpcRetCode            `yaml:"trpc_codes,omitempty" json:"trpc_codes,omitempty"`
	TRPCCodeMap   map[trpcpb.TrpcRetCode]struct{} `yaml:"-"`
}

KeysConfig configuration

type Options

type Options struct {
	// Remove operations
	// Remove response headers, in the format of header:status_code, e.g., traceid:401,404 means removing traceid when
	// the HTTP status code is 401	// If status_code is not specified, it means all statuses
	RemoveHeaders []*KeysConfig `yaml:"remove_headers,omitempty" json:"remove_headers,omitempty"`

	// Remove keys in the JSON body, in the format of key:status_code, e.g., suid:401 means removing suid when the HTTP
	// status code is 401
	// If status_code is not specified, it means all statuses
	// Key supports hierarchical configuration, e.g., common.suid
	RemoveJSON []*KeysConfig `yaml:"remove_json,omitempty" json:"remove_json,omitempty"`

	// Rename header configuration fields, in the format of old_header:new_header:status_code
	// Renamed headers will not be formatted
	RenameHeaders []*KeysConfig `yaml:"rename_headers,omitempty" json:"rename_headers,omitempty"`

	// Rename JSON body parameters, in the format of key:val:status_code, e.g., suid:xxx
	RenameJSON []*KeysConfig `yaml:"rename_json,omitempty" json:"rename_json,omitempty"`

	// Add headers, in the format of key:val
	AddHeaders []*KeysConfig `yaml:"add_headers,omitempty" json:"add_headers,omitempty"`

	// Add JSON body parameters, in the format of key:val:type, e.g., suid:xxx:string
	AddJSON []*KeysConfig `yaml:"add_json,omitempty" json:"add_json,omitempty"`

	// Replace operations
	ReplaceHeaders []*KeysConfig `yaml:"replace_headers,omitempty" json:"replace_headers,omitempty"`

	// key:val:type format, e.g., suid:xxx:string
	ReplaceJSON []*KeysConfig `yaml:"replace_json,omitempty" json:"replace_json,omitempty"`

	// Append operations
	AppendHeaders []*KeysConfig `yaml:"append_headers,omitempty" json:"append_headers,omitempty"`

	// key:val:type format, e.g., suid:xxx:string
	AppendJSON []*KeysConfig `yaml:"append_json,omitempty" json:"append_json,omitempty"`

	// Replace response body
	ReplaceBody []*KeysConfig `yaml:"replace_body,omitempty" json:"replace_body,omitempty"`

	// Allowed parameters
	AllowJSON []*KeysConfig `yaml:"allow_json,omitempty" json:"allow_json,omitempty"`
}

Options parameter options

type Plugin

type Plugin struct {
}

Plugin response transformer plugin implementation

func (*Plugin) CheckConfig

func (p *Plugin) CheckConfig(name string, decoder plugin.Decoder) error

CheckConfig validates the plugin configuration and returns the parsed configuration object with types. Used in the ServerFilter method for parsing.

func (*Plugin) Setup

func (p *Plugin) Setup(string, plugin.Decoder) error

Setup response transformer instance initialization

func (*Plugin) Type

func (p *Plugin) Type() string

Type response transformer plugin type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL