middleware

package
v0.0.0-...-d32eb86 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Copyright 2023 Talhuang<talhuang1231@gmail.com>. All rights reserved. Use of this source code is governed by a MIT style license that can be found in the LICENSE file.

Index

Constants

View Source
const UsernameKey = "username"

UsernameKey defines the key in gin context which represents the owner of the secret.

View Source
const (
	// XRequestIDKey is the header field used to store the request ID.
	XRequestIDKey = "X-Request-ID"
)

Variables

View Source
var Middlewares = defaultMiddlewares()

Middlewares store registered middlewares.

Functions

func Context

func Context() gin.HandlerFunc

Context is a middleware that injects common prefix fields to gin.Context.

func Cors

func Cors() gin.HandlerFunc

func GetDefaultLogFormatterWithRequestID

func GetDefaultLogFormatterWithRequestID() gin.LogFormatter

GetDefaultLogFormatterWithRequestID creates a log formatter function that includes the request ID in the log message.

func GetLoggerConfig

func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig

GetLoggerConfig creates and returns a gin.LoggerConfig configured to write logs to a specified io.Writer. The logs will be formatted using the provided gin.LogFormatter. If no formatter is provided, it defaults to a custom log formatter that includes the request ID. gin.DefaultWriter is os.Stdout by default, but can be set to any io.Writer.

func Logger

func Logger() gin.HandlerFunc

Logger instances a Logger middleware that will write the logs to gin.DefaultWriter. By default gin.DefaultWriter = os.Stdout.

func LoggerWithConfig

func LoggerWithConfig(conf gin.LoggerConfig) gin.HandlerFunc

LoggerWithConfig instance a Logger middleware with config.

func LoggerWithFormatter

func LoggerWithFormatter(f gin.LogFormatter) gin.HandlerFunc

LoggerWithFormatter instance a Logger middleware with the specified log format function.

func LoggerWithWriter

func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc

LoggerWithWriter instance a Logger middleware with the specified writer buffer. Example: os.Stdout, a file opened in write mode, a socket...

func NoCache

func NoCache(c *gin.Context)

NoCache is a middleware function that appends headers to prevent the client from caching the HTTP response.

func Options

func Options(c *gin.Context)

Options is a middleware function that appends headers for options requests and aborts then exits the middleware chain and ends the request.

func RateLimiter

func RateLimiter() gin.HandlerFunc

RateLimiter returns a middleware handler function for Gin.

func RequestID

func RequestID() gin.HandlerFunc

RequestID is a middleware that injects a request ID into each request. If the incoming request already has an 'X-Request-ID' header, it will use that one, otherwise, it generates a new UUIDv4 and sets it in both the request header and context.

func ResponseLogger

func ResponseLogger() gin.HandlerFunc

func Secure

func Secure(c *gin.Context)

Secure is a middleware function that appends security and resource access headers.

Types

type AuthOperator

type AuthOperator struct {
	// contains filtered or unexported fields
}

AuthOperator used to switch between different authentication strategy.

func (*AuthOperator) AuthFunc

func (operator *AuthOperator) AuthFunc() gin.HandlerFunc

AuthFunc execute resource authentication.

func (*AuthOperator) SetStrategy

func (operator *AuthOperator) SetStrategy(strategy AuthStrategy)

SetStrategy used to set to another authentication strategy.

type AuthStrategy

type AuthStrategy interface {
	AuthFunc() gin.HandlerFunc
}

AuthStrategy defines the set of methods used to do resource authentication.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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