middleware

package
v5.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware components for the CLI Proxy API server. This file contains the request logging middleware that captures comprehensive request and response data when enabled through configuration.

Package middleware provides Gin HTTP middleware for the CLI Proxy API server. It includes a sophisticated response writer wrapper designed to capture and log request and response data, including support for streaming responses, without impacting latency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestLoggingMiddleware

func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc

RequestLoggingMiddleware creates a Gin middleware that logs HTTP requests and responses. It captures detailed information about the request and response, including headers and body, and uses the provided RequestLogger to record this data. If logging is disabled in the logger, the middleware has minimal overhead.

Types

type RequestInfo

type RequestInfo struct {
	URL     string              // URL is the request URL.
	Method  string              // Method is the HTTP method (e.g., GET, POST).
	Headers map[string][]string // Headers contains the request headers.
	Body    []byte              // Body is the raw request body.
}

RequestInfo holds essential details of an incoming HTTP request for logging purposes.

type ResponseWriterWrapper

type ResponseWriterWrapper struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriterWrapper wraps the standard gin.ResponseWriter to intercept and log response data. It is designed to handle both standard and streaming responses, ensuring that logging operations do not block the client response.

func NewResponseWriterWrapper

func NewResponseWriterWrapper(w gin.ResponseWriter, logger logging.RequestLogger, requestInfo *RequestInfo) *ResponseWriterWrapper

NewResponseWriterWrapper creates and initializes a new ResponseWriterWrapper. It takes the original gin.ResponseWriter, a logger instance, and request information.

Parameters:

  • w: The original gin.ResponseWriter to wrap.
  • logger: The logging service to use for recording requests.
  • requestInfo: The pre-captured information about the incoming request.

Returns:

  • A pointer to a new ResponseWriterWrapper.

func (*ResponseWriterWrapper) Finalize

func (w *ResponseWriterWrapper) Finalize(c *gin.Context) error

Finalize completes the logging process for the request and response. For streaming responses, it closes the chunk channel and the stream writer. For non-streaming responses, it logs the complete request and response details, including any API-specific request/response data stored in the Gin context.

func (*ResponseWriterWrapper) Size

func (w *ResponseWriterWrapper) Size() int

Size returns the size of the response body in bytes for non-streaming responses. For streaming responses, it returns -1, as the total size is unknown.

func (*ResponseWriterWrapper) Status

func (w *ResponseWriterWrapper) Status() int

Status returns the HTTP response status code captured by the wrapper. It defaults to 200 if WriteHeader has not been called.

func (*ResponseWriterWrapper) Write

func (w *ResponseWriterWrapper) Write(data []byte) (int, error)

Write wraps the underlying ResponseWriter's Write method to capture response data. For non-streaming responses, it writes to an internal buffer. For streaming responses, it sends data chunks to a non-blocking channel for asynchronous logging. CRITICAL: This method prioritizes writing to the client to ensure zero latency, handling logging operations subsequently.

func (*ResponseWriterWrapper) WriteHeader

func (w *ResponseWriterWrapper) WriteHeader(statusCode int)

WriteHeader wraps the underlying ResponseWriter's WriteHeader method. It captures the status code, detects if the response is streaming based on the Content-Type header, and initializes the appropriate logging mechanism (standard or streaming).

func (*ResponseWriterWrapper) Written

func (w *ResponseWriterWrapper) Written() bool

Written returns true if the response header has been written (i.e., a status code has been set).

Jump to

Keyboard shortcuts

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