request

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: 17 Imported by: 0

README

tRPC-Gateway Request Transformation Plugin

The request transformation plugin allows for modifying the HTTP request headers and parameters during request forwarding. It eliminates the need for manual interface encapsulation work and improves efficiency.

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/request"
)
  • Configure the tRPC framework in the configuration file to enable the request transformer interceptor.

Note: Make sure to register it under server.service.filter and not 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 for the service
      filter:
        - request_transformer # Gateway plugin registered in the service's filter, so that it can be dynamically loaded in router.yaml
plugins:                            # Plugin configuration
  log:                                            # Log configuration
  gateway:                            # Plugin type is gateway
    request_transformer:  # Name of the request transformer plugin

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

router: # Router configuration
  - method: /v1/user/info
    target_service:
      - service: trpc.user.service
    plugins:
      - name: request_transformer
        props:
          add_headers: # Add request headers
            - key: val
          add_body: # Add request body parameters
            - key: val
          add_query_str: # Add URL query parameters
            - key: val
          reserve_headers: # Only keep specified request headers, higher priority than remove_headers, -1 means remove all
            - -1
          remove_headers: # Remove request headers
            - my_del_header
          reserve_body: # Only keep specified body parameters, higher priority than remove_body, -1 means remove all. Multipart form does not support this operation.
            - -1
          remove_body: # Remove request body parameters
            - del_query_param
          reserve_query_str: # Only keep specified query string parameters, higher priority than remove_query_str, -1 means remove all
            - -1
          remove_query_str: # Remove URL query parameters
            - my_del_query_param
          rename_headers: # Rename request headers, renamed headers will not be formatted
            - oldkey: newkey
          rename_body: # Rename request body parameters
            - oldkey: newkey
          rename_query_str: # Rename URL query parameters
            - oldkey: newkey
          rewrite_host: shizi.qq.com # Modify host
client: # Upstream service configuration, follows the trpc protocol
  - name: trpc.user.service
    plugins:
      - name: request_transformer # Service-level configuration, will be effective for all interfaces forwarded to this service
        props:
plugins:
  - name: request_transformer # Global configuration, will be effective for all interfaces
    props:

The plugin can also be used through the gateway console.

img.png

Documentation

Overview

Package request request transformer plugin

Index

Constants

View Source
const (

	// DelAllKey is a placeholder for deleting all operations
	DelAllKey = "-1"
)

Variables

This section is empty.

Functions

func ServerFilter

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

ServerFilter sets up server-side CORS validation

Types

type KV

type KV struct {
	Key string
	Val string
}

KV represents a key-value structure

type Options

type Options struct {
	// Rewrite host
	RewriteHost string `yaml:"rewrite_host" json:"rewrite_host"`
	// Delete operations
	// List of reserved headers, if configured, remove operations will not be executed. -1 indicates deleting all
	ReserveHeaders []string `yaml:"reserve_headers" json:"reserve_headers"`
	// Remove headers
	RemoveHeaders []string `yaml:"remove_headers" json:"remove_headers"`
	// List of reserved query parameters, if configured, remove operations will not be executed. -1 indicates deleting all
	ReserveQueryStr []string `yaml:"reserve_query_str" json:"reserve_query_str"`
	RemoveQueryStr  []string `yaml:"remove_query_str" json:"remove_query_str"`
	// List of reserved body parameters, if configured, remove operations will not be executed. -1 indicates deleting all
	ReserveBody []string `yaml:"reserve_body" json:"reserve_body"`
	RemoveBody  []string `yaml:"remove_body"`

	// Rename header configuration fields in the format key:val
	// Renamed headers will not be formatted
	RenameHeaders []string `yaml:"rename_headers" json:"rename_headers"`
	// Parsed key-value pairs
	RenameHeadersKV []*KV `yaml:"-" json:"-"`
	// Rename query parameters in the format key:val
	RenameQueryStr   []string `yaml:"rename_query_str" json:"rename_query_str"`
	RenameQueryStrKV []*KV    `yaml:"-" json:"-"`

	RenameQueryBodyKV []*KV `yaml:"-" json:"-"`

	// Rename body parameters in the format key:val
	RenameBody   []string `yaml:"rename_body" json:"rename_body"`
	RenameBodyKV []*KV    `yaml:"-" json:"-"`

	// Add headers in the format key:val
	AddHeaders   []string `yaml:"add_headers" json:"add_headers"`
	AddHeadersKV []*KV    `yaml:"-" json:"-"`
	// Add query parameters in the format key:val
	AddQueryStr    []string `yaml:"add_query_str" json:"add_query_str"`
	AddQueryStrKV  []*KV    `yaml:"-" json:"-"`
	AddQueryBodyKV []*KV    `yaml:"-" json:"-"`

	// Add body parameters in the format key:val. It will be appended to different request bodies based on
	// the content type.
	AddBody   []string `yaml:"add_body" json:"add_body"`
	AddBodyKV []*KV    `yaml:"-" json:"-"`
}

Options represents the parameter options

type Plugin

type Plugin struct {
}

Plugin implements the request transformer plugin

func (*Plugin) CheckConfig

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

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

func (*Plugin) Setup

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

Setup initializes the request transformer instance

func (*Plugin) Type

func (p *Plugin) Type() string

Type returns the request transformer plugin type

Jump to

Keyboard shortcuts

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