Documentation ¶
Index ¶
- func AddRequestId(next http.Handler) http.Handler
- func Chain(f http.HandlerFunc, m ...func(http.Handler) http.Handler) http.Handler
- func Example(next http.Handler) http.Handler
- func JsonResponse(next http.Handler) http.Handler
- func Log(next http.Handler) http.Handler
- func RegisterLogger(l logger.ILogger)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddRequestId ¶
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 ¶
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 ¶
Example is a simple example of how middleware can be used to wrap the request handling logic.
func JsonResponse ¶
JsonResponse adds the application/json content type to the response header.
func Log ¶
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 ¶
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.