redirect

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

README

Redirect Plugin

This plugin is used to configure redirection.

Features

  • Redirect to HTTPS
  • Specify the redirection URI
  • Configure redirection URI using regular expressions
  • Specify the HTTP status code for redirection
  • Support appending query parameters to the redirection URI

Plugin Usage

Import the Plugin in the Main.go File of the Gateway Project
  • Add the import statement
import (
    _ "trpc.group/trpc-go/trpc-gateway/plugin/redirect"
)
  • Configure the tRPC framework file to enable the mocking interceptor.

Note: Make sure to register it in server.service.filter instead of 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:
        - redirect # Register the gateway plugin 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
    redirect:                        # Redirect plugin
Configure the Plugin in the Router.yaml File of the Gateway Routing Configuration

Different level plugins are executed only once, with the priority order: Router plugin > Service plugin > Global plugin.

router:
  - method: /v1/user/info
    id: "xxxxxx"
    target_service:
      - service: trpc.user.service
    plugins:
      - name: redirect # Router-level plugin
        props:  # Note: Only one of uri, http_to_https, regex_uri can be filled
          uri: "" # Redirection URI
          http_to_https: false
          regex_uri: # Redirect based on regular expression match
            - ^/api/(.)/(.)/(.*) # Regular expression, e.g., /api/a/b/c
            - /$1-$2-$3 # Redirection URI, cannot be empty. This example redirects to /a-b-c
          append_query_string: true # Whether to append original query parameters to the redirection URI
          ret_code: 302 # Redirection status code, default is 302
Configure the Plugin Using the Console

img.png

Documentation

Overview

Package redirect implements redirection functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServerFilter

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

ServerFilter is the server interceptor.

Types

type Options

type Options struct {
	// When set to true and the request is HTTP, it will be redirected to HTTPS with the same URI and 301 status code.
	HTTPToHTTPS bool `yaml:"http_to_https"`
	// The URI to redirect to.
	URI string `yaml:"uri"`
	// Match the URL from the client with regular expressions and redirect. After a successful match, replace the
	// client's request URI with the redirect URI template.
	// For example: ["^/iresty/(.)/(.)/(.*)","/$1-$2-$3"]
	// The first element represents the regular expression to match the URI from the client request,
	// and the second element represents the URI template to send the redirect to the client after a successful match.
	RegexURI []string `yaml:"regex_uri"`

	// HTTP response code.
	RetCode int `yaml:"ret_code"`
	// When set to true, append the query string from the original request to the Location Header.
	// If the uri or regex_uri already contains a query string, the query string from the request will be appended
	// with an "&".
	AppendQueryString bool `yaml:"append_query_string"`
	// contains filtered or unexported fields
}

Options is the plugin configuration.

type Plugin

type Plugin struct {
}

Plugin is the plugin definition.

func (*Plugin) CheckConfig

func (p *Plugin) CheckConfig(_ 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 plugin.

func (*Plugin) Type

func (p *Plugin) Type() string

Type returns the plugin type.

Jump to

Keyboard shortcuts

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