zmidd

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zmidd

some gin middleware

Usage
Cors
func main() {
    s := gin.New()
    // default: *
    s.Use(zmidd.Cors())
    
    // options
    s.Use(zmidd.Cors(
        zmidd.WithAllowOrigin("*"),
        zmidd.WithAllowMethods("*"),
        zmidd.WithAllowHeaders("*"),
        zmidd.WithAllowCredentials("*"),
        zmidd.WithExposeHeaders("true"),
    ))
}
RequestId
func main() {
    s := gin.New()
    s.Use(zmidd.RequestId())
    
    // Get RequestId
    // c = *gin.Context
    rid := zmidd.GetRequestId(c)
}

Documentation

Index

Constants

View Source
const (
	DefaultAllowOrigin      = "*"
	DefaultAllowMethods     = "*"
	DefaultAllowHeaders     = "*"
	DefaultAllowCredentials = "*"
	DefaultExposeHeaders    = "true"
)

Variables

This section is empty.

Functions

func Cors

func Cors(_ ...Option) gin.HandlerFunc

Cors @Description: 开启跨域控制 @Param ops: *Options 可选参数 可以设置跨域的配置 默认值为* 允许所有域名访问 允许所有请求方法 跨域允许所有请求头 允许跨域携带cookie @Return gin.HandlerFunc 返回一个gin.HandlerFunc 可以直接使用gin.Use(Cors()) 或者在路由中使用Cors() 即可 如果需要设置跨域的配置 可以使用 WithAllowOrigin WithAllowMethods WithAllowHeaders WithAllowCredentials WithExposeHeaders

func GetRequestId

func GetRequestId(c *gin.Context) string

func Logger

func Logger(conf LoggerConfig, whitelist []string) gin.HandlerFunc

func RateLimiter

func RateLimiter(rate float64) gin.HandlerFunc

RateLimiter @Description: 请求速率限制 @return gin.HandlerFunc

func Recover

func Recover() gin.HandlerFunc

func RequestId

func RequestId() gin.HandlerFunc

func SizeLimiter

func SizeLimiter(size int64) gin.HandlerFunc

SizeLimiter @Description: 请求体大小限制 @return gin.HandlerFunc

func Timeout

func Timeout(timeout time.Duration, whitelist []string) gin.HandlerFunc

Timeout @Description: 超时中间件 @param timeout @param whitelist @return gin.HandlerFunc

Types

type CustomResponseWriter

type CustomResponseWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

func (CustomResponseWriter) Write

func (w CustomResponseWriter) Write(b []byte) (int, error)

func (CustomResponseWriter) WriteString

func (w CustomResponseWriter) WriteString(s string) (int, error)

type LogLinkField

type LogLinkField struct {
	Rid   string `json:"rid"`
	Time  int64  `json:"time"`
	Delay int64  `json:"delay"`
	Tag   string `json:"tag"`
	Auth  string `json:"auth"`
	Head  string `json:"head"`
	Param string `json:"param"`
	Data  string `json:"data"`
	Error string `json:"error"`
}

type LoggerConfig

type LoggerConfig struct {
	LogTag      string `yaml:"log_tag" mapstructure:"log_tag"`
	MaxBody     int    `yaml:"max_body" mapstructure:"max_body"`
	MaxResponse int    `yaml:"max_response" mapstructure:"max_response"`
	MaxFile     uint   `yaml:"max_file" mapstructure:"max_file"`
	Path        string `yaml:"path" mapstructure:"path"`
	PrintThird  bool   `yaml:"print_third" mapstructure:"print_third"`
}

type Option

type Option func(s string) *Options

type Options

type Options struct {
	AllowOrigin      string
	AllowMethods     string
	AllowHeaders     string
	AllowCredentials string
	ExposeHeaders    string
}

func WithAllowCredentials

func WithAllowCredentials(allowCredentials string) *Options

func WithAllowHeaders

func WithAllowHeaders(allowHeaders string) *Options

func WithAllowMethods

func WithAllowMethods(allowMethods string) *Options

func WithAllowOrigin

func WithAllowOrigin(allowOrigin string) *Options

func WithExposeHeaders

func WithExposeHeaders(exposeHeaders string) *Options

type SimpleBodyWriter

type SimpleBodyWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

func (SimpleBodyWriter) Write

func (w SimpleBodyWriter) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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