cors

package module
v1.0.0 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

Gateway CORS Plugin

Developed based on the plugin at https://git.woa.com/trpc-go/trpc-filter/tree/master/cors , it supports the ability to dynamically load plugins in the gateway.

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/cors"
)
  • Configure the tRPC framework file to enable the CORS interceptor.

Note: Make sure to register it under server.service.filter, not server.filter.

global:                             # Global configuration
server: # Server configuration
  filter:                                          # Interceptor list for all service handlers
  service: # Business services provided, can have multiple
    - name: trpc.inews.trpc.gateway      # Route name of the service
      filter:
        - cors # Gateway plugin registered as a filter in the service, so that it can be dynamically loaded in router.yaml
plugins: # Plugin configuration
  log:                                            # Log configuration
  gateway: # Plugin type is gateway
    cors:  # Name of the CORS plugin

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

router: # Routing configuration
  - method: /v1/user/info
    target_service:
      - service: trpc.user.service
    plugins:
      - name: cors # Router-level plugin: only effective for the current interface
        props:
          allow_origins: # Supported domains, supports suffix matching; if not specified, allows cross-origin requests from all domains. Corresponds to: Access-Control-Allow-Origin
            - xxx.qq.com
          allow_methods: # Supported HTTP methods, if not specified, supports all methods. Corresponds to: Access-Control-Request-Method
            - GET
            - POST
          allow_headers: # Allowed request headers, if not specified, supports all headers in preflight requests. Corresponds to: Access-Control-Allow-Headers
            - my-allow-header
          allow_credentials: true # Whether to allow credentials to be included. Corresponds to: Access-Control-Allow-Credentials
          expose_headers: # Exposed response headers. Corresponds to: Access-Control-Expose-Headers
            - my-expose-header
          max_age: 99999 # Preflight cache time, default is 0. Corresponds to: Access-Control-Max-Age
client: # Upstream service configuration, consistent with the trpc protocol
  - name: trpc.user.service
    plugins:
      - name: cors # Service-level configuration, effective for all interfaces forwarded to this service
        props:
plugins:
  - name: cors # Global configuration, effective for all interfaces
    props:

References:

Documentation

Overview

Package cors provides cross-origin resource sharing for HTTP requests.

Index

Constants

View Source
const (

	// ErrCORS is the error code for cross-origin requests. The error codes in the gateway plugin are custom and use 5
	// digits.
	ErrCORS = 10000
)

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. Reference for CORS headers: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS

Types

type Options

type Options struct {
	// AllowOrigins is a list of allowed cross-origin request sources. If empty, it allows all cross-origin requests,
	// i.e., wildcard *.
	AllowOrigins []string `yaml:"allow_origins"`

	// AllowMethods is a list of allowed cross-origin request methods. If not specified, the default value is
	// []string{"GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"}.
	AllowMethods []string `yaml:"allow_methods"`

	// AllowHeaders is a list of headers that can be used in the actual request returned by the preflight request.
	// If not specified, it allows all preflight request headers.
	AllowHeaders []string `yaml:"allow_headers"`

	// AllowCredentials specifies whether to allow credentials to be carried, such as cookies.
	AllowCredentials bool `yaml:"allow_credentials"`

	// ExposeHeaders is a list of custom headers that can be obtained in cross-origin requests, such as trpc-ret.
	ExposeHeaders []string `yaml:"expose_headers"`

	// MaxAge is the cache time for preflight requests, in seconds.
	MaxAge int `yaml:"max_age"`
}

Options represents the parameter options for the cors plugin.

type Plugin

type Plugin struct {
}

Plugin implements the cors plugin for trpc.

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 cors plugin instance.

func (*Plugin) Type

func (p *Plugin) Type() string

Type returns the type of the cors plugin for trpc.

Jump to

Keyboard shortcuts

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