middleware

package
v0.0.0-...-d3a19af Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CorrelationID

func CorrelationID(next http.Handler) http.Handler

CorrelationID middleware adds a unique UUID to the request context and response headers.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID middleware adds a unique UUID to the request context and response headers.

func TraceID

func TraceID(next http.Handler) http.Handler

TraceID middleware adds a unique UUID to the request context and response headers.

Types

type CORSConfig

type CORSConfig struct {

	// AllowedOrigins is the list of origins that are allowed to access the resource.
	// Default: `[]string{"*"}`
	//
	// This field is optional.
	AllowedOrigins []string

	// AllowedMethods is the list of methods that are allowed to access the resource.
	// Default: `[]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}`
	//
	// This field is optional.
	AllowedMethods []string

	// AllowedHeaders is the list of headers that are allowed to access the resource.
	// Default: `[]string{"Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization
	// "accept", "origin", "Cache-Control", "X-Requested-With"}`
	//
	// This field is optional.
	AllowedHeaders []string

	// AllowCredentials is the flag that determines if the resource allows credentials.
	// Default: `false`
	//
	// This field is optional.
	AllowCredentials bool
}

type JWTClaims

type JWTClaims struct {
	jwt.StandardClaims
	XUserID uuid.UUID `json:"x-user-id"`
}

func (JWTClaims) Valid

func (c JWTClaims) Valid() error

type JWTConfig

type JWTConfig struct {

	// Prefix is the type of the JWT.
	// Default: `Bearer`
	//
	// This field is optional.
	Prefix string

	// Algorithm is the algorithm of the key that will be used to validate the JWT.
	// Default: `HS256`
	//
	// This field is optional.
	Algorithm string

	// Issuer is the issuer of the JWT.
	// Default: “
	//
	// This field is optional.
	Issuer string

	// Audience is the audience of the JWT.
	// Default: “
	//
	// This field is optional.
	Audience string

	// Key is the secret key that will be used to validate the JWT.
	//
	// This field is mandatory.
	Key string

	// ExceptionalRoutes is the list of routes that will be excluded from the JWT validation.
	// For example, you can exclude the login route from the JWT validation.
	//
	// Example: []string{
	// 		"/login"
	// 		"/healthz"
	//	}
	//
	// This field is optional.
	ExceptionalRoutes []string

	// Header is the request header that will be used to extract the JWT from.
	// Default: `Authorization`
	//
	// This field is optional.
	Header string
}
JWT is a middleware that can be used to validate the JWTs.

Generate temporary JWTs for testing from here: https://oauth.tools/collection/1712706959493-UZt

type Key

type Key string
const XCorrelationID Key = "X-Correlation-ID"

X-Correlation-ID is the key used to store the correlation ID in the context and the response header.

The correlation ID is used to correlate the request with other requests.

const XJWTClaims Key = "x-jwt-claims"

XJWTClaims is the key used to store the claims of the JWT in the context.

The claims are used to store the information about the authenticated user.

const XRequestID Key = "X-Request-ID"

X-Request-ID is the key used to store the request ID in the context and the response header.

The request ID is used to uniquely identify the request.

const XTraceID Key = "X-Trace-ID"

X-Trace-ID is the key used to store the trace ID in the context and the response header.

The trace ID is used to trace the request through multiple services.

const XWebhookToken Key = "X-Webhook-Token"

X-Webhook-Token is the key used to store the webhook token in the request header.

The webhook token is used to authenticate a webhook request.

type LoggingConfig

type LoggingConfig struct {

	// Logger is the `log/slog` instance that will be used to log messages.
	// Default: `slog.DefaultLogger`
	//
	// This field is optional.
	Logger *slog.Logger

	// LogLatency is the flag that determines if the latency of the request should be logged.
	// Latency is calculated as the difference between the time the request is received and the time the response is sent.
	// Default: `false`
	//
	// This field is optional.
	LogLatency bool

	// LogError is the flag that determines if the response status is 5xx then the error message should be logged.
	// Default: `false`
	//
	// This field is optional.
	LogError bool
}

type Middleware

type Middleware func(http.Handler) http.Handler

func CORS

func CORS(config *CORSConfig) Middleware

CORS middleware adds the CORS headers to the response.

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain is a variadic function that executes multiple middlewares in sequential order.

func JWT

func JWT(config *JWTConfig) Middleware

func Logging

func Logging(config *LoggingConfig) Middleware

func Recover

func Recover(config *RecoverConfig) Middleware

Recover is a middleware that recovers from the panics.

func Webhook

func Webhook(config *WebhookConfig) Middleware

Webhook middleware authenticates the request using a unique webhook token.

type RecoverConfig

type RecoverConfig struct {

	// Logger is the `log/slog` instance that will be used to log messages.
	// Default: nil
	//
	// This field is optional.
	Logger *slog.Logger
}

type WebhookConfig

type WebhookConfig struct {

	// Token is the unique token that will be used to authenticate the request.
	//
	// This field is mandatory.
	Token string
}

Webhook middleware authenticates the request using a unique webhook token.

Jump to

Keyboard shortcuts

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