middleware

package
v0.0.0-...-35aec58 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRequestId

func AddRequestId(next http.Handler) http.Handler

AddRequestId adds a request ID to the request context if it doesn't already exist.

This is useful for logging and tracing. It uses the request ID from the API Gateway event if it exists, otherwise it generates a new one and adds it to the request context.

func Chain

func Chain(f http.HandlerFunc, m ...func(http.Handler) http.Handler) http.Handler

Chain is a variadic function that takes a Handler and a list of functions that return a Handler. It returns a Handler that chains the functions together in the order they are provided. The first function in the list is the outermost function, which is applied first. And the last function in the list is the innermost function, which is applied last.

Example:

	defaultMiddlewares := []func(next http.Handler) http.Handler{
	middleware.JsonResponse,
		middleware.AddRequestId,
		middleware.Log,
 }
	http.NewServeMux().Handle("/endpoint", middleware.Chain(method, defaultMiddlewares...))

func Example

func Example(next http.Handler) http.Handler

Example is a simple example of how middleware can be used to wrap the request handling logic.

func JsonResponse

func JsonResponse(next http.Handler) http.Handler

JsonResponse adds the application/json content type to the response header.

func Log

func Log(next http.Handler) http.Handler

Log is a middleware that logs the request and response to the logger. It also logs some metadata about the request, such as the request ID, source IP, and execution time.

func RegisterLogger

func RegisterLogger(l logger.ILogger)

RegisterLogger registers a logger to be used by the middleware. This should be called once at the beginning of the program before any middleware is used.

If no logger is registered, a default (fallback) logger is used, but it is not recommended to use it. Example:

middleware.RegisterLogger(projectLogger)

Types

This section is empty.

Jump to

Keyboard shortcuts

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