middlewares

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CtxKeyApiKey = CtxKey("X-API-Key")
View Source
var CtxKeyRealIP = CtxKey("X-Real-IP")

Functions

func GetApiKey

func GetApiKey(r *http.Request, paramName string, pathIndex int) (string, bool)

GetApiKey returns the client API key from the specified `url`. If `paramName`specified, parse from query. Otherwise, parse from URL path with specified `pathIndex`. Note, the `paramName` is case sensitive.

func GetApiKeyFromContext

func GetApiKeyFromContext(ctx context.Context) (string, bool)

GetApiKeyFromContext get client API key from the specified `ctx`.

func GetIPAddress

func GetIPAddress(r *http.Request) string

GetIPAddress returns the real remote IP address.

func GetRealIPFromContext

func GetRealIPFromContext(ctx context.Context) (string, bool)

GetRealIPFromContext get real remote IP address from the specified `ctx`.

func Hook

func Hook(handler http.Handler, middlewares ...Middleware) http.Handler

Hook hooks middlewares to the specified HTTP handler. Middlewares will be decorated in reversed order, that is, the first middleware is the outermost one, and the last middleware is the innermost.

func RealIP

func RealIP(next http.Handler) http.Handler

RealIP injects real remote IP address in context.

Types

type ApiKeyOption

type ApiKeyOption struct {
	Required  bool
	ParamName string // case-sensitive, enabled if specified
	PathIndex int    // api key in URL path
}

type CtxKey

type CtxKey string

CtxKey is the key type for injected kv pair in the context of HTTP request.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware decorates HTTP Handler to response HTTP request.

func NewApiKeyMiddleware

func NewApiKeyMiddleware(option ApiKeyOption) Middleware

NewApiKeyMiddleware creates middleware to inject client API key in context.

Jump to

Keyboard shortcuts

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